Fix 10908: FP: uninitvar after for-loop (#3942)

This commit is contained in:
Paul Fultz II 2022-03-31 14:24:20 -05:00 committed by GitHub
parent 183969cd4b
commit 5bea50cd36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 18 deletions

View File

@ -1863,6 +1863,7 @@ class MisraChecker:
return following return following
# Zero arguments should be in form ( void ) # Zero arguments should be in form ( void )
# TODO: Use rawTokens or add flag when void is removed
def checkZeroArguments(func, startCall, endCall): def checkZeroArguments(func, startCall, endCall):
if (len(func.argument) == 0): if (len(func.argument) == 0):
voidArg = startCall.next voidArg = startCall.next
@ -1935,7 +1936,7 @@ class MisraChecker:
endCall = startCall.link endCall = startCall.link
if endCall is None or endCall.str != ')': if endCall is None or endCall.str != ')':
continue continue
checkZeroArguments(func, startCall, endCall) # checkZeroArguments(func, startCall, endCall)
checkDefinitionArgumentsViolations(func, startCall, endCall) checkDefinitionArgumentsViolations(func, startCall, endCall)
# Check arguments in function declaration # Check arguments in function declaration
@ -1947,7 +1948,7 @@ class MisraChecker:
endCall = startCall.link endCall = startCall.link
if endCall is None or endCall.str != ')': if endCall is None or endCall.str != ')':
continue continue
checkZeroArguments(func, startCall, endCall) # checkZeroArguments(func, startCall, endCall)
if tokenImpl: if tokenImpl:
checkDeclarationArgumentsViolations(func, startCall, endCall) checkDeclarationArgumentsViolations(func, startCall, endCall)
else: else:

View File

@ -337,7 +337,7 @@ const misra_8_1_a; // 8.1 8.4
static int misra_8_2_a (int n, ...); static int misra_8_2_a (int n, ...);
extern int misra_8_2_b (int n); extern int misra_8_2_b (int n);
extern int misra_8_2_c (int); // 8.2 extern int misra_8_2_c (int); // 8.2
static int misra_8_2_d (); // 8.2 static int misra_8_2_d (); // TODO: 8.2
static int misra_8_2_e (void); static int misra_8_2_e (void);
static int misra_8_2_f (vec, n ) static int misra_8_2_f (vec, n )
int *vec; // 8.2 int *vec; // 8.2
@ -345,13 +345,13 @@ int n; // 8.2
{ {
return vec[ n - 1 ]; return vec[ n - 1 ];
} }
static int misra_8_2_g ( /* comment */ ); // 8.2 static int misra_8_2_g ( /* comment */ ); // TODO: 8.2
static int misra_8_2_h ( /* comment 1 */ /* comment 2 */ ); // 8.2 static int misra_8_2_h ( /* comment 1 */ /* comment 2 */ ); // TODO: 8.2
static int misra_8_2_i ( /* comment */ void); static int misra_8_2_i ( /* comment */ void);
static int misra_8_2_j ( /* comment */ void /* comment */); static int misra_8_2_j ( /* comment */ void /* comment */);
static int misra_8_2_k ( // static int misra_8_2_k ( //
void); void);
static int misra_8_2_l ( // 8.2 static int misra_8_2_l ( // TODO: 8.2
); );
static void misra_8_2_m(uint8_t * const x); static void misra_8_2_m(uint8_t * const x);
static void misra_8_2_m(uint8_t * const x) static void misra_8_2_m(uint8_t * const x)
@ -373,7 +373,7 @@ static int misra_8_2_p(
const uint8_t *const a2 const uint8_t *const a2
); );
static int misra_8_2_q static int misra_8_2_q
(); // 8.2 (); // TODO: 8.2
void misra_8_4_foo(void) {} // 8.4 void misra_8_4_foo(void) {} // 8.4
extern void misra_8_4_func(void); extern void misra_8_4_func(void);

View File

@ -3108,6 +3108,16 @@ void Tokenizer::simplifyArrayAccessSyntax()
} }
} }
void Tokenizer::simplifyParameterVoid()
{
for (Token* tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "sizeof|decltype|typeof"))
continue;
if (Token::Match(tok, "%name% ( void )"))
tok->next()->deleteNext();
}
}
void Tokenizer::simplifyRedundantConsecutiveBraces() void Tokenizer::simplifyRedundantConsecutiveBraces()
{ {
// Remove redundant consecutive braces, i.e. '.. { { .. } } ..' -> '.. { .. } ..'. // Remove redundant consecutive braces, i.e. '.. { { .. } } ..' -> '.. { .. } ..'.
@ -5273,6 +5283,8 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
removeRedundantSemicolons(); removeRedundantSemicolons();
simplifyParameterVoid();
simplifyRedundantConsecutiveBraces(); simplifyRedundantConsecutiveBraces();
simplifyEmptyNamespaces(); simplifyEmptyNamespaces();

View File

@ -217,6 +217,7 @@ private:
TEST_CASE(template172); // #10258 crash TEST_CASE(template172); // #10258 crash
TEST_CASE(template173); // #10332 crash TEST_CASE(template173); // #10332 crash
TEST_CASE(template174); // #10506 hang TEST_CASE(template174); // #10506 hang
TEST_CASE(template175); // #10908
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..}; TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..}; TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template) TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
@ -1138,7 +1139,7 @@ private:
"return f1<B<A>> ( 0 , reinterpret_cast < B<A> * > ( E<void*> :: Int ( -1 ) ) ) ; " "return f1<B<A>> ( 0 , reinterpret_cast < B<A> * > ( E<void*> :: Int ( -1 ) ) ) ; "
"} " "} "
"} ; " "} ; "
"int main ( void ) { " "int main ( ) { "
"C<A> ca ; " "C<A> ca ; "
"return 0 ; " "return 0 ; "
"} " "} "
@ -4456,6 +4457,16 @@ private:
ASSERT_EQUALS(exp, tok(code)); ASSERT_EQUALS(exp, tok(code));
} }
void template175()
{
const char code[] = "template <typename T, int value> T Get() {return value;}\n"
"char f() { Get<int,10>(); }\n";
const char exp[] = "int Get<int,10> ( ) ; "
"char f ( ) { Get<int,10> ( ) ; } "
"int Get<int,10> ( ) { return 10 ; }";
ASSERT_EQUALS(exp, tok(code));
}
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..}; void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
const char code[] = "template <typename T> struct C {};\n" const char code[] = "template <typename T> struct C {};\n"
"template <typename T> struct S {a};\n" "template <typename T> struct S {a};\n"

View File

@ -4832,7 +4832,7 @@ private:
ASSERT(functok); ASSERT(functok);
ASSERT(functok->function()); ASSERT(functok->function());
ASSERT(functok->function()->name() == "foo1"); ASSERT(functok->function()->name() == "foo1");
functok = Token::findsimplematch(tokenizer.tokens(), "foo2 ( void ) { }"); functok = Token::findsimplematch(tokenizer.tokens(), "foo2 ( ) { }");
ASSERT(functok); ASSERT(functok);
ASSERT(functok->function()); ASSERT(functok->function());
ASSERT(functok->function()->name() == "foo2"); ASSERT(functok->function()->name() == "foo2");

View File

@ -2508,7 +2508,7 @@ private:
" unsigned short const int x = 1;\n" " unsigned short const int x = 1;\n"
" return x;\n" " return x;\n"
"}"; "}";
ASSERT_EQUALS("unsigned short f ( void ) {\n" ASSERT_EQUALS("unsigned short f ( ) {\n"
"const unsigned short x ; x = 1 ;\n" "const unsigned short x ; x = 1 ;\n"
"return x ;\n" "return x ;\n"
"}", "}",
@ -3571,11 +3571,11 @@ private:
void simplifyFunctionPointers6() { void simplifyFunctionPointers6() {
const char code1[] = "void (*fp(void))(int) {}"; const char code1[] = "void (*fp(void))(int) {}";
const char expected1[] = "1: void * fp ( void ) { }\n"; const char expected1[] = "1: void * fp ( ) { }\n";
ASSERT_EQUALS(expected1, tokenizeDebugListing(code1)); ASSERT_EQUALS(expected1, tokenizeDebugListing(code1));
const char code2[] = "std::string (*fp(void))(int);"; const char code2[] = "std::string (*fp(void))(int);";
const char expected2[] = "1: std :: string * fp ( void ) ;\n"; const char expected2[] = "1: std :: string * fp ( ) ;\n";
ASSERT_EQUALS(expected2, tokenizeDebugListing(code2)); ASSERT_EQUALS(expected2, tokenizeDebugListing(code2));
} }
@ -4071,7 +4071,7 @@ private:
{ {
const char code[] = "class S { int function(void); };"; const char code[] = "class S { int function(void); };";
ASSERT_EQUALS("class S { int function ( void ) ; } ;", tokenizeAndStringify(code)); ASSERT_EQUALS("class S { int function ( ) ; } ;", tokenizeAndStringify(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -4083,7 +4083,7 @@ private:
{ {
const char code[] = "int function(void);"; const char code[] = "int function(void);";
ASSERT_EQUALS("int function ( void ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int function ( ) ;", tokenizeAndStringify(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -4095,13 +4095,13 @@ private:
{ {
const char code[] = "extern int function(void);"; const char code[] = "extern int function(void);";
ASSERT_EQUALS("extern int function ( void ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("extern int function ( ) ;", tokenizeAndStringify(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "int function1(void); int function2(void);"; const char code[] = "int function1(void); int function2(void);";
ASSERT_EQUALS("int function1 ( void ) ; int function2 ( void ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int function1 ( ) ; int function2 ( ) ;", tokenizeAndStringify(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -4746,7 +4746,7 @@ private:
} }
void simplifyCAlternativeTokens() { void simplifyCAlternativeTokens() {
ASSERT_EQUALS("void or ( void ) ;", tokenizeAndStringify("void or(void);", true, Settings::Native, "test.c")); ASSERT_EQUALS("void or ( ) ;", tokenizeAndStringify("void or(void);", true, Settings::Native, "test.c"));
ASSERT_EQUALS("void f ( ) { if ( a && b ) { ; } }", tokenizeAndStringify("void f() { if (a and b); }", true, Settings::Native, "test.c")); ASSERT_EQUALS("void f ( ) { if ( a && b ) { ; } }", tokenizeAndStringify("void f() { if (a and b); }", true, Settings::Native, "test.c"));
ASSERT_EQUALS("void f ( ) { if ( a && b ) { ; } }", tokenizeAndStringify("void f() { if (a and b); }", true, Settings::Native, "test.cpp")); ASSERT_EQUALS("void f ( ) { if ( a && b ) { ; } }", tokenizeAndStringify("void f() { if (a and b); }", true, Settings::Native, "test.cpp"));
ASSERT_EQUALS("void f ( ) { if ( a || b ) { ; } }", tokenizeAndStringify("void f() { if (a or b); }", true, Settings::Native, "test.c")); ASSERT_EQUALS("void f ( ) { if ( a || b ) { ; } }", tokenizeAndStringify("void f() { if (a or b); }", true, Settings::Native, "test.c"));

View File

@ -6167,6 +6167,15 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
valueFlowUninit("template <typename T, int value> T Get() {return value;}\n"
"char f() {\n"
" char buf[10];\n"
" for(int i = 0; i < Get<int,10>() ; ++i) \n"
" buf[i] = 0;\n"
" return buf[0];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
valueFlowUninit("static void Foo(double* p) {\n" valueFlowUninit("static void Foo(double* p) {\n"
" p[0] = 0;\n" " p[0] = 0;\n"
" p[1] = 0;\n" " p[1] = 0;\n"

View File

@ -1735,7 +1735,7 @@ private:
"};"; "};";
ASSERT_EQUALS("1: class Foo {\n" ASSERT_EQUALS("1: class Foo {\n"
"2: private:\n" "2: private:\n"
"3: void f ( void ) ;\n" "3: void f ( ) ;\n"
"4: } ;\n", "4: } ;\n",
tokenize(code)); tokenize(code));
} }