HP Code Advisor Diagnostics Reference Guide HP Part Number: 5900-1865 Published: July 2011 Edition: 4
© Copyright 2011 Hewlett-Packard Development Company, L.P. Confidential computer software. Valid license from HP required for possession, use or copying. Consistent with FAR 12.211 and 12.212, Commercial Computer Software, Computer Software Documentation, and Technical Data for Commercial Items are licensed to the U.S. Government under vendor's standard commercial license. The information contained herein is subject to change without notice.
Contents About This Document.....................................................................................7 Intended Audience....................................................................................................................7 HP Encourages Your Comments..................................................................................................7 1 Diagnostics Categorization..........................................................................8 1.1 Default Diagnostics.......
2.36 2224 the format string requires additional arguments...........................................................34 2.37 2225 the format string ends before this argument................................................................34 2.38 2226 invalid format string conversion................................................................................34 2.39 2228 trailing comma is nonstandard.................................................................................34 2.
2.89 3051 standard requires that %n be given a type by a subsequent declaration (\"int\" assumed)...............................................................................................................................49 2.90 3055 types cannot be declared in anonymous unions..........................................................49 2.91 3056 returning pointer to local variable.............................................................................49 2.92 3105 #warning directive: %s.............
2.135 4290 endian porting: the initialization of char array may be endian dependent.....................63 2.136 4292 endian porting: the dereference of cast pointer may be endian dependent....................64 2.137 4295 abstract function type declared with empty parentheses, consider replacing with parameter list or void. ............................................................................................................64 2.
About This Document This document provides a brief description for a partial list of diagnostics emitted by Cadvise. There are four sections for each description as mentioned below: • The Cause section Discusses the possible reasons for the diagnostic to be emitted. • The Example section Provides relevant sample code segments that result in the specific diagnostic.
1 Diagnostics Categorization This chapter addresses the diagnostics categorization for HP Code Advisor. NOTE: • Some of the diagnostic numbers given below do not have a corresponding diagnostic description. Incase you need a diagnostic description for a diagnostic number, please send an email to: cadvise-help@lists.hp.com • The diagnostics with number greater than 20000 are more effective when cross-file analysis is enabled. For more information on cross-file analysis, see HP Code Advisor User's Guide.
Table 1 Default diagnostics (continued) Diagnostic number Diagnostic message 2149 a global-scope declaration may not have this storage class 2155 old-fashioned assignment operator 2157 expression must be an integral constant expression 2161 unrecognized #pragma 2165 too few arguments in function call 2167 argument of type %t1 is incompatible with parameter of type %t2 2174 expression has no effect 2175 subscript out of range 2178 \"&\" applied to an array has no effect 2181 argument is
Table 1 Default diagnostics (continued) 10 Diagnostic number Diagnostic message 2460 declaration of %sq hides function parameter 2472 member function typedef (allowed for cfront compatibility) 2490 %n cannot be instantiated -- it has been explicitly specialized 2495 global %n1 used instead of %n2 (cfront compatibility) 2497 declaration of %sq hides template parameter 2514 pointless comparison of unsigned integer with a negative constant 2524 non-const function called for const object (anach
Table 1 Default diagnostics (continued) Diagnostic number Diagnostic message 2836 returning reference to local variable 2855 return type is not identical to return type %t of overridden virtual function %n 2867 declaration of \"size_t\" does not match the expected type %t 2902 type qualifier ignored 2925 type qualifiers on function types are ignored 2959 declared size for bit field is larger than the size of the bit field type; ; truncated to %s bits 2991 extra braces are nonstandard 2997 %
Table 1 Default diagnostics (continued) 12 Diagnostic number Diagnostic message 3197 the prototype declaration of %nfd is ignored after this unprototyped redeclaration 3202 call of zero constant ignored 3210 declaration of %sq hides handler parameter 3211 nonstandard cast to array type ignored 3213 field uses tail padding of a base class 3214 GNU C++ compilers may use bit field padding 3215 %nd was declared \"deprecated\" 3217 unrecognized format function type %sq ignored 3218 base clas
Table 1 Default diagnostics (continued) Diagnostic number Diagnostic message 3346 function returns incomplete class type %t 3347 %n has already been initialized; the out-of-class initializer will be ignored 3359 variable attributes appearing after a parenthesized initializer are ignored 3360 the result of this cast cannot be used as an lvalue 3361 negation of an unsigned fixed-point value 3377 friend specifier is not allowed in a class definition; friend specifier is ignored 3386 storage spec
Table 1 Default diagnostics (continued) Diagnostic number Diagnostic message 3627 attribute \"cleanup\" requires automatic storage duration 3628 attribute \"cleanup\" does not apply to parameters 3644 definition at end of file not followed by a semicolon or a declarator 3647 extra arguments ignored 3650 result of call is not used 3651 attribute is ignored for void return type 3652 attribute requires pointer-to-function type 3653 dllimport/dllexport is ignored on redeclaration using a qualif
Table 1 Default diagnostics (continued) Diagnostic number Diagnostic message 4162 This feature, within an OpenMP directive region, is not supported by this version of C++. All surrounding OpenMP processing pragmas are being ignored.
Table 1 Default diagnostics (continued) Diagnostic number Diagnostic message 4288 Object %sq in pragma noptrs_to_globals may not have its address taken, pragma ignored 4294 the use of pragma %s is not allowed in this environment 4301 expression has no effect 4308 case label constant out of range (%s) for switch expression 4313 no prototype or definition in scope for call to memory allocation routine %sq 4321 One or more source files were inaccessible (error: %s) while emitting the source contex
Table 3 +wlint diagnostics (continued) Diagnostic number Diagnostic message 2550 %n was set but never used 2767 conversion from pointer to smaller integer 2108 signed bit field of length 1 2815 type qualifier on return type is meaningless 2940 missing return statement at end of non-void 3051 standard requires that %n be given a type by a subsequent declaration ("int" assumed) 3053 conversion from integer to smaller pointer 3348 declaration hides %nd 4324 declaration hides %nd 3373 impli
Table 3 +wlint diagnostics (continued) Diagnostic number Diagnostic message 4357 octal escape sequence "%s" is followed by decimal character '%s2' 20200 Potential null pointer dereference %s%s is detected %s 20201 Memory leak is detected 20202 Allocated memory may potentially be leaked %s 20203 Potential out of scope use of local %s %s 20204 Potential out of scope use of alloca return address %s %s 20205 Pointer is used after free 20206 Out of bound access (%s) 20207 Possible out of bound
Table 4 +w64bit diagnostics (continued) Diagnostic number Diagnostic message 4230 64 bit migration: conversion from %t1 to %t2 may cause target of pointers to have a different size. 4231 64 bit migration: conversion between types of different sizes has occurred (from %t1 to %t2 ) 4249 64 bit migration: value could be truncated before cast to bigger sized type.
1.7 +wlock Diagnostics Following table lists diagnostic messages that cadvise emits when you use the +wlock command line option.
Table 8 +wperfadvice diagnostics (continued) Diagnostic number Diagnostic message 20314 performance advice: The data being accessed is less strictly aligned than the type of the address. Certain optimizations may be prevented. 20315 performance advice: Frequently called routine '%s' cannot be inlined because it is not defined in the current load module. 20316 performance advice: Parallelizing this loop would require %s. Consider using +Oautopar to do this automatically. 1.
Table 9 +w/+w1 diagnostics (continued) Diagnostic number Diagnostic message 4163 Conversion of string literal from const to non-const has been deprecated 4217 comparison of enum (represented as an unsigned integer) with zero 4218 comparison of unsigned integer with an enumerated value of zero 4227 padding struct with %s1 bytes to align member %sq2 4241 redeclaration, function %nod was previously called without a prototype 4242 no prototype or definition in scope for call to %sq 4243 function
Table 10 Diagnostic messages that are turned off by default (continued) Diagnostic number Diagnostic message 4363 possible mismatch in parameters passed to %n, previously seen %p 4365 endian porting: the definition of the union may be endian dependent 4370 control flows into the switch case from the previous case value 4372 potential overflow in arithmetic expression involving time_t/clock_t values 4373 non arithmetic integer conversion resulted in a change of sign 1.
2 Diagnostics Details This chapter provides the detailed description for a subset of diagnostics emitted by HP Code Advisor. 2.1 2009 nested comment is not allowed Cause: Opening comment delimiter, /*, found inside delimited comment. A previous comment may be missing its closing delimiter. C comments delimited by /* */ do not nest.
Action: Use valid/compatible operand types or use a cast operator to convert one operand type to the other type. Reference: 2.4 2047 incompatible redefinition of macro %nod Cause: A #define preprocessing directive has redefined a macro whose previous definition contained an error or warning. Normally, the compiler will issue a warning if a macro is redefined to something other than the previous definition.
2.7 2064 declaration does not declare anything Cause: The declaration does not declare anything. The C standard requires that a declaration must declare at least a tag, an enumeration constant or a declarator. Example: int; Action: Correct or remove the declaration. Reference: C99 6.7.2, ANSI/ISO C++ 6.7(2) 2.8 2068 integer conversion resulted in a change of sign Cause: Conversion of integer resulted in sign change.
Example: struct X { i; }; Action: Provide a declaration specifier. Reference: C99 6.7.2 2.11 2082 storage class is not first Cause: The placement of a storage-class specifier other than at the beginning of the declaration specifier in a declaration is an obsolescent feature. Example: int static i = 0; Action: Place the storage-class specifier first in the declaration. Reference: C99 6.7, 6.7.1 2.
2.14 2111 statement is unreachable Cause: Code at this location will never be executed. Often unreachable statements represent a real coding error such as a wrongly inserted return, goto, throw or call to non-returning functions etc. Example: return 0; i++; // warning 2111 here Action: Check for incorrectly placed unconditional control transfer statements. Reference: 2.15 2117 non-void %n should return a value Cause: A function that returns a value does not end with a return statement.
Action: If the loop is necessary, change the code such that the loop can be reached. Otherwise, remove the loop. Reference: 2.18 2144 a value of type %t1 cannot be used to initialize an entity of type %t2 Cause: Initializing an entity of some type with incompatible types. Example: typedef char* addr; int main() { addr data = "0x1"; int bar = data; return 0; } Action: Initialize an entity of some type with the same type or compatible types.
Action: Avoid such incompatible type conversions. Reference: C99 6.3.2.3 2.21 2167 argument of type %t1 is incompatible with parameter of type %t2 Cause: The parameter type passed is incompatible with the function argument type. Example: typedef char * addr; int foo(int a) { return a; } int main() { addr data = "0x1"; foo(data); return 0; } Action: Pass the parameter type that is compatible with the function argument type. Reference: 2.
int main() { int index = 0; for(index; index < 10; index++){ } } Action: Modify the expression so that it has some effect or just remove it. 2.25 2175 subscript out of range Cause: The compiler has detected an array access that is outside the bounds of the array. The array access might cause unpredictable behavior. Example: int main() { int arr[10]; arr[10] = 20; } Action: Specify an array subscript that is within the bounds of the array type. Reference: 2.
2.28 2181 argument is incompatible with corresponding format string conversion Cause: The compiler has detected a format specifier whose data type does not match its corresponding argument. Example: printf("%d", 2.0); Action: Modify either the argument or the conversion specifier so that they match. Reference:C99 6.2.2.7 2.29 2186 pointless comparison of unsigned integer with zero Cause: An unsigned expression is being tested to see if it is greater than zero.
Action: Make sure that you do not mix enums and non-enums. Reference: 2.32 2191 type qualifier is meaningless on cast type Cause: Casting to a qualified type, though valid, is pointless. This is reported only when the cv-qualifiers are explicit in the cast,not, for example, when they are hidden in a typedef or template parameter type. Example: int i = (const volatile int) 10; Action: Remove the unnecessary qualifiers in the cast. Reference: 2.
foo(10); } Action: Declare the function before it is referenced. Reference: C99 6.5.2.2 2.36 2224 the format string requires additional arguments Cause: The number of conversion specifiers is more than the number of values to be converted as specified in the parameter list. Example:printf("%d %d", 2); Action: Make sure the number of conversion specifiers match the number of values to be converted. Reference: 2.
Remove the trailing comma. Reference: C99 6.7.2.2 ANSI/ISO C++ 7.2 2.40 2231 declaration is not visible outside of function Cause: A type is declared within a function prototype. The type is local to the function prototype and will not be visible outside the prototype. This might cause unexpected errors later in the compilation. Example: void foo(struct X { int i; } ); Action: Declare the type before the function prototype. Reference: 2.
2.43 2245 a nonstatic member reference must be relative to a specific object Cause: Only static members can be accessed using the class name. For accessing non-static members, specific object must be used. Example: struct Dummy { int i; }; Dummy::i = 10; } Action: Access non-static members through objects only. For example: Dummy d; d.i = 10; Reference: 2.44 2248 pointer to reference is not allowed Cause: Pointer to reference is incorrect and is not allowed.
Reference: ANSI/ISO C++ 8.3.2 (4) 2.47 2251 array of reference is not allowed Cause: Array of reference is incorrect and is not allowed. Example:int & iarr[5]; } Action: Remove the & and make it a plain array. For example: int iarr[5]; Reference: ANSI/ISO C++ 8.3.2 (4) 2.
2.51 2265 %nd is inaccessible Cause: An attempt is made to access a private or protected member in the context where the member is not accessible. Example: class A { private: int x; } c; c.x = 0; Action: Fix the access specifier of the member or the statement that is accessing the member. Reference: 2.52 2267 old-style parameter list (anachronism) Cause: The given function is defined using the old style K&R syntax. The C standard has marked this syntax as obsolescent, and it is not supported in C++.
Action: Make sure that the declaration or definition of the class or the namespace is defined before it is used. Check if the necessary header files containing the declaration/definition are included. Reference: 2.55 2278 a constructor or destructor may not return a value Cause: Constructors and destructors do not have a return type and they do not return any value. It is incorrect to provide a return statement for constructors and destructors.
The cv qualifiers ( const and volatile qualifiers) of the object should be compatible with the cv qualifiers of member functions that it calls. For example, any attempt to call a non-const member function on a const object will result in an error. Example: int main() { struct X { void foo(); }; const X s; s.foo(); } Action: Ensure that the cv qualifiers of the object are not stricter than cv qualifiers of the methods that called through it. Reference: ANSI/ISO C++ 9.3.2(4) 2.
Only static const members of integral or enumeration type can be initialized inline in a class definition. For other types, provide the initialization outside the class definition. Example: int i = 100; class Init { static const int *p_i = }; Action: Initialize the member outside the class: int i = 100; class Init { static const int *p_i; }; const int* Init::p_i = Reference: ANSI/ISO C++ 9.4.2 9.2(4) 2.
The inline keyword is meaningful for function declarations only. Example: inline int i; Action: Remove the inline keyword. For example: int i; Reference: ANSI/ISO C++ 10.4(3) 2.65 2329 local class member %n requires a definition Cause: Member functions of local classes must be defined within the class definition. Example: int main() { struct Struct { void foo(); }; Struct s; s.foo(); } Action: Define all required member functions of the local classes inside the class itself. Reference: ANSI/ISO C++ 9.
Reference: ANSI/ISO C++ 8.5.3 12.2 2.68 2363 invalid anonymous union -- nonpublic member is not allowed Cause: Anonynous unions can have only public non-static data members, private or protected members are not allowed. Example: static union { private: int i; protected: float f; }; Action: Provide public access to all the anonymous union members. For example: static union { public: int i; float f; }; Reference: ANSI/ISO C++ 9.5(3) 2.
Add static storage class to the anonymous union declared at global or namespace scope. For example: static union { int i; }; Reference: ANSI/ISO C++ 9.5(3) 2.71 2375 declaration requires a typedef name Cause: In a typedef declaration, the typedef name is missing. This case can occur when a declaration tries to declare both a tag and a typedef, but the name of the typedef is not included. Example: typedef int; Action: Either remove the typedef keyword or add a typedef name. Reference: 2.
2.74 2513 a value of type %t1 cannot be assigned to an entity of type %t2 Cause: The types of variables used in the assignment are incompatible. Example: long double d = 0.0; char *c = 0; c = Action: Check if the assignment is required and if it is valid. If valid, use an explicit cast to force the assignment. Reference: 2.75 2546 transfer of control bypasses initialization of: "variable" Cause: Compiler has detected transfer of control was attempted that bypasses initialization of a variable.
Example: int main() { int var; var = 20; } Action: Examine your code to determine if this definition is needed in this module. Reference: 2.78 2656 transfer of control into a try block Cause: Goto statements cannot be used to transfer control inside the try blocks. Example: goto LABEL; try { LABEL: throw 0; } catch(...) { } Action: Remove the goto statement that transfers the control inside the try block. Reference: ANSI/ISO C++ 15(2) 2.
2.81 2826 %n was never referenced Cause: This function definition contains a parameter n that was never referenced. Example: int foo( int i) { return 0; } Action: Examine your code to determine if this function parameter is needed in this function. Reference: 2.
extern i; a(); b(){} extern c(); extern d(){} Action: Add an explicit type for the declaration or definition. Reference: C99 6.7.2(2), ANSI/ISO C++ 7.1.5(2) 2.85 2940 missing return statement at end of non-void function "function" Cause: A function with an explicit return type does not contain a return statement. Example:int foo() { } Action: If the function is supposed to return a value, add a return statement with appropriate value, otherwise declare the return type as void. Reference: 2.
Remove the unnecessary storage class specifier. Reference: ARM 7.1.1. 2.89 3051 standard requires that %n be given a type by a subsequent declaration (\"int\" assumed) Cause: The parameter of an old-style function definition was not declared. It will default to int type. Omitting the type specifier is not valid in C99. Example:int foo(arg) { return arg; } Action: Declare the parameter. Preferable old-style function definitions should be replaced by prototype-format definitions. Reference: C99 6.9.1 2.
2.92 3105 #warning directive: %s Cause: The preprocessor has encountered a #warning preprocessor directive. Example: #if VERSION_10 #define VERSION 10 #else #warning "undefined version" #endif Action: #warning directives are usually added to warn against fallthrus in conditionally compiled code. check if you need to handle a new condition. Reference: 2.
Example: int foo(int x); int main() { int foo(); // warning 3197 here } Action: Remove the unprototyped declaration. Reference: 2.96 3290 Passing a non-POD object to a function with variable arguments has undefined behavior. Object will be copied onto the stack instead of using a constructor. Cause: A non-POD (POD stands for "plain old data") object is being passed to a function with variable arguments. Object will be copied onto the stack instead of using a constructor.
2.98 3353 %n has no corresponding member operator delete%s (to be called if an exception is thrown during initialization of an allocated object) Cause: For member operator new, a corresponding member operator delete needs to be provided so that it can be called for proper destruction in case an exception is thrown during the initialization of the allocated object.
The bitwise operator | has lower precedence than the comparison operator, possibly changing the semantics of the expression. This can be caused by other combinations of comparison and bitwise operators as well. Example: bool check(unsigned p1, unsigned p2, unsigned mask) { return ( p1 == p2 | mask ); } Action: Disambiguate the expression by using explicit parenthesis around the bitwise operator, thus return ( p1 == (p2 | mask) ); Reference: 2.
The compiler has detected conversion between pointers to data types having different sizes. This usually happens when assigning an int pointer to a long pointer or vice versa. This is not a problem in 32 bit mode since int, long and pointer all are of same size (32 bit) but in 64 bit mode int is 32 bit whereas long and pointer are 64 bit. Because of the difference in size in 64 bit mode this conversion might lead to incorrect behavior in 64 bit mode.
For 64 bit portability ensure that your code does not convert from int to pointer or long and vice versa. Reference: 2.107 4232 conversion from %t1 to a more strictly aligned type %t2 may cause misaligned access Cause: A pointer is being cast from a lesser aligned type to a more strictly aligned type. Accesses using the new type may cause misaligned access. Example: int foo(int* p, long long* l) { l = (long long*) p; // warning 4232 here } Action: Correct the code that uses such conversions.
The switch expression type does not match with the type of case labels. Example: enum BOOLEAN { TRUE = 0, FALSE = 1 }; BOOLEAN a_bool = TRUE; switch(a_bool) { case 0: printf("true\n"); case 1: printf("false\n"); default: printf("no match found \n"); } Action: Fix your source code to ensure the type of the switch expression and case labels is same. Reference: 2.
Action: The recommended way to declare a function that takes no parameters is to use "void" in the parameter list. Reference: 2.114 4244 extern storage class used with a function definition Cause: The compiler has detected the usage of an extern storage class specifier with a function definition. Example: extern int foo() { return 0; } Action: Remove the extern storage class specifier in function definition. Reference: 2.
if (a < b) return a; Action: To resolve this problem, either cast the integer to unsigned if you know it can never be less than zero or cast the unsigned to a signed int if you know it can never exceed the maximum value of int. Reference: 2.118 4249 64 bit migration: value could be truncated before cast to bigger sized type. Cause: The size of the result of an operation is determined based on the size of the operands and not based on the type into which the resultant value is being castled.
return 1; } Action: Cast (or otherwise rewrite) one of the operands of the compare to match the signedness of the other operand, or compare for equality with zero. Reference: 2.121 4255 padding size of struct %sq1 with %s2 bytes to alignment boundary Cause: Padding bytes have been inserted for proper alignment of the struct.
int main() { typedef struct { uint32_t data_size; uint8_t status; } str; } Action: Insertion of padding bytes themselves is not a problem but you need to ensure that you do not use hard coded offsets for accessing fields of the struct through pointers, use offset of macro instead. In some cases the number of padding bytes being inserted can be reduced by reordering the fields. Reference: ANSI/ISO C++ 18.1(5) 2.
char* ptr = 0; if(ptr >= 0) return 1; } Action: Rewrite the expression to use == or !=. Reference: 2.127 4275 constant out of range (%s) for the operator Cause: For a given relational operation the constant is out of range specified by the other non-constant operand. Example: void foo(bool b, char c) { if (b == 3) { } // warning 4275 here if (c < 200) { } // warning 4275 here if ((unsigned char)c < 200) { } // no warning here } Action: Check the value of the constant being used in the operation.
2.130 4278 the sub expression in logical expression is a constant Cause: Logical operators follow short-circuit evaluation - if the first operand of a logical || or && operator determines the result of the expression, the second operand will not be evaluated. In the given expression the first sub expression is a constant that determines the result of the expression and hence the rest of the expression will never be evaluated.
2.133 4286 return non-const handle to non-public data member may undermine encapsulation Cause: A less-restrictive member function is returning a non-const reference or pointer to a more-restrictive data member. Since the handle thus returned is not a const, the caller will be able to modify the restrictive data member, thus violating the norms of encapsulation.
2.136 4292 endian porting: the dereference of cast pointer may be endian dependent Cause: A pointer of integral type is being cast to a pointer of char or smaller integer type. Based on endianness of a machine on which code is executed the value accessed using will differ.
2.139 4299 64 bit migration: multiply result could be truncated before cast to bigger sized type Cause: The size of the result of an operation is determined based on the size of the operands and not based on size of variable in which the resultant value is stored. If the result exceeds the size of the operands, in this case 32 bits, it will be truncated even before the assignment takes place.
Example: int main() { if (1) ; } Action: Check if you inadvertently inserted a ';'. Reference: 2.143 4315 %s loop without body, did you insert an extra ';'? Cause: The loop statement does not have a body. Example: for (i=0; i<10; i++) ; // warning 4315 here Action: Check if you inadvertently inserted a ';'. Reference: 2.
printf("%u\n",(unsigned) sizeof(CONST)); return 0; // Issue warning } Action: Using sizeof() on a constant is unusual, and might be done by mistake - check if this is what you intended. Reference: 2.147 4357 octal escape sequence "%s" is followed by decimal character '%s2' Cause: When a non-octal decimal character follows an octal sequence, you may assume that its part of the escape sequence.
In a 32–bit to 64–bit migration scenario, this warning is issued during bitfield packing when the types in the structure are different. Example: #include enum name {first, middle, last}; struct node2 { long l:1; name n:1; }head2; int main() { printf ("sizeof struct node2 is : %lu\n", sizeof(head2)); return 0; } Action: It is good to use similar types for bitfield packing. This is because, bitfield packing behavior might be different across compilers. Reference: 2.
int main() { int *p; short q = 0x0123; p = (int *)&q; printf("%d\n", *p); } On a big-endian system, the output is: 19070976. Whereas, on a little-endian system, the output is: 291 Action: Pointer type casting to different data size is unusual, and might be done by mistake - check if this is what you intended. Reference: 2.
case 2: printf("Two\n"); break; case 3: printf("Three\n"); case 4: printf("Four\n"); default: printf("Bye\n"); } return 0; } Action: This warning is issued to ensure that the missing breaks between the different case values is expected. Reference: 2.154 4372 Potential overflow in arithmetic expression involving time_t/clock_t values Cause: When arithmetic expressions that involve time_t/clock_t values overflow, the code may behave incorrectly. Example: #include
2.156 20035 variable %s is used before its value is set Cause: The compiler has detected use of a variable's value before it is set. Example: int func(int b) { int a; a = a + b; // warning 20035 for use of variable a return a; } Action: Initialize the reported variable with a value before it is used. This may not be the only use of the uninitialized value. So it is best to initialize variables as close as possible to the point of declaration. Reference: 2.
Check if all the execution paths leading to the usage of reported variable initialize it. To avoid such issues it is best to initialize the variable close to the point of declaration. Reference: 2.159 20048 %s "%s" has incompatible type with previous declaration at line %s in file "%s" Cause: The declaration of a global variable or function does not match the corresponding declaration on a different file. Example: -- file1.
An element in an array or an unnamed field of a struct may be referenced prior to its initialization. Example: int a[2]; a[0] = 10; if (cond) a[1] = 12; foo(a); // warning 20073: element 1 may be uninitialized Action: Ensure that all elements/fields of an array/struct are unconditionally initialized prior to accessing them. Reference: 2.162 20074 variable %s (field "%s") may be used before its value is set Cause: A field of a struct may be referenced prior to its initialization.
If you are assigning NULL to a pointer, make sure that no path in the program can dereference that pointer before it is assigned a different value. For routines that may return a NULL, check or assert that the value returned is not NULL. char * pc = malloc(20); if (pc != NULL) strcpy(pc, "hello"); or: char * pc = malloc(20); assert(pc != NULL); strcpy(pc, "hello"); Reference: 2.
A local variable's address is being returned and dereferenced in the caller, or allocated memory is being returned and used in the caller or a variable defined in the inner scope is being accessed indirectly in the enclosing scope. Use of local variable outside its scope can lead to unexpected behavior. Example: #include int foo() { int *p; { int q; scanf("%d", &q); p = &q; } // out of scope reference to q return *p; } int main() { int result = foo(); return result; } "oos.
Reference: 2.168 20208 Forming out of bound address(%s) Cause: The expression is forming an address which is out of object's memory boundary. Example: char buf[12]; char *p; void foo() { int i = 20; p = &buf[0] + i; // LINE 7 } line 7, procedure foo: warning #20208-D: Forming out of bound address (In expression "&buf[0]+20", variable "buf" [test.c:1] (type: char [12]) has byte range [0 .. 11], forming address byte [20].) Action: Fix the forming out of bounds access if it is a real bug. Reference: 2.
#include
Example: #include struct S { int v; int c;}; int foo2(int n) { struct S var; for (int i=10; i