Main Content

CERT C++: EXP56-CPP

Do not call a function with a mismatched language linkage

Since R2023b

Description

Rule Definition

Do not call a function with a mismatched language linkage.1

Polyspace Implementation

This checker checks for Language linkage mismatch between called function type and function definition.

Examples

expand all

Issue

The issue occurs when you call a function and provide a function type with a language linkage that does not match the language linkage of the function definition.

For instance, in this code snippet, the definition of function call_legacy_function_ptr() uses a function pointer legacy_fn_call with FORTRAN language linkage to interact with Fortran code. However, inside the function interact_with_legacy_code() the function call_legacy_function_ptr() is called with a function pointer fn_call with C++ language linkage, which is undefined behavior.

extern "FORTRAN" typedef void (*fortran_type)(int);
extern void call_legacy_function_ptr(fortran_type legacy_fn_call);
void fn_call(int); // fn_call has C++ language linkage

void interact_with_legacy_code()
{
   call_legacy_function_ptr(fn_call);
}
You can specify a language linkage as part of the function type to enable compatibility between C++ and other programming languages such as Java, Ada, or Fortran. The language linkage that you specify affects the way your program calls the function and accesses the data. By default, unless you specify a language linkage, all function types have C++ linkage.

Risk

A function call that uses a function type with a language linkage that does not match the language linkage of the function definition is undefined behavior. Depending on your compiler implementation, the function call might result in runtime issues such as call stack corruption.

Note that some compilers accept code with mismatched language linkage without issuing a warning or error.

Fix

Do not use a function type with a language linkage that does not match the language linkage in the function definition, unless you determine that the mismatch has no effect on the runtime behavior of the program.

Example — Language Linkage Mismatch Between Function Definition and Function Call
#include <iostream>

extern "C" typedef void (*C_type)(int);
void c_code_call(C_type C_func)
{
   std::cout << "Calling C code.\n";
   C_func;
   std::cout << "Returning to  C++ code\n";
}

void caller_func(int) // C++ language linkage
{
   std::cout << "Executing caller function.\n";
}

int main()
{
   c_code_call(caller_func); // Noncompliant
   return 0;
}

In this example, the function c_code_call expects a function with C language linkage as a parameter. However, when the function c_code_call is called inside of main() the call uses function caller_func with C++ language linkage. This mismatch is undefined behavior and might lead to runtime issues or crashes.

Correction — Use a Function with The Same Language Linkage in the Function Call

One Possible correction is to declare the function caller_func() with C language linkage to match the language linkage in the declaration of the function c_code_call.

#include <iostream>

extern "C" typedef void (*C_type)(int);
void c_code_call(C_type C_func)
{
   std::cout << "Calling C code.\n";
   C_func;
   std::cout << "Returning to  C++ code\n";
}

extern "C" void caller_func(int)
{
   std::cout << "Executing caller function.\n";
}

int main()
{
   c_code_call(caller_func); // Compliant
   return 0;
}

Check Information

Group: Rule 02. Expressions (EXP)

Version History

Introduced in R2023b


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.