V2641. MISRA. Types should be explicitly specified.
This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guidelines.
This diagnostic rule is relevant only for C.
The C language enables the declaration of entities without explicitly specifying their type. In such a case, the standard states that the entity type is int
. Using such a language feature can lead to confusion or an error.
The example:
// TU1.c
#include <stddef.h>
void *my_malloc (size_t n) { /* Implementation */ }
// TU2.c
#include <stddef.h>
extern my_malloc (size_t n);
In the TU1.c
file, the my_malloc
allocating function, which takes a size in bytes and returns a pointer to the allocated memory is defined. To use this function, developers manually wrote a forward function declaration in the TU2.c
file but forgot to specify the return type. As a result, the compiler assumes that the function returns int
. This may cause errors during program execution if the pointer size and int
size do not match.
To fix the error, specify the data type:
// TU2.c
#include <stddef.h>
extern void *my_malloc (size_t n);
The analyzer also issues warnings on the following entity declarations:
extern var1; // variable declaration of 'int' type
func1() {} // function declaration with the no return type
static var2; // variable declaration of 'double' type
The fixed examples:
extern int var1; // variable declaration of 'int' type
void func1() {} // function declaration with the no return type
static double var2; // variable declaration of 'double' type
This diagnostic is classified as:
|