You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* teststring.cpp: Fix ternary syntax in tests
* stringLiteralWrite: Add tests wide character and utf16 strings
* suspiciousStringCompare: Add test with wide character string
* strPlusChar: Handle wide characters
* incorrectStringCompare: Add test with wide string
* Suspicious string compare: suggest wcscmp for wide strings
* deadStrcmp: Extend to handle wide strings
* sprintfOverlappingData: Print name of strcmp function
* Conversion of char literal to boolean, add wide character tests
* Conversion of char literal to boolean, fix ternary
reportError(tok, Severity::error, "strPlusChar", "Unusual pointer arithmetic. A value of type '" + charType +"' is added to a string literal.", CWE665, false);
@@ -547,6 +609,11 @@ class TestString : public TestFixture {
547
609
"}");
548
610
ASSERT_EQUALS("[test.cpp:2]: (warning) Conversion of string literal \"Hello\" to bool always evaluates to true.\n", errout.str());
549
611
612
+
check("int f() {\n"
613
+
" return \"Hello\" ? 1 : 2;\n"
614
+
"}");
615
+
ASSERT_EQUALS("[test.cpp:2]: (warning) Conversion of string literal \"Hello\" to bool always evaluates to true.\n", errout.str());
616
+
550
617
check("int f() {\n"
551
618
" assert (test || \"Hello\");\n"
552
619
"}");
@@ -582,22 +649,26 @@ class TestString : public TestFixture {
582
649
" if('a'){}\n"
583
650
" if(L'b'){}\n"
584
651
" if(1 && 'c'){}\n"
585
-
" int x = 'd' ? 1 : 2;\n"// <- TODO
652
+
" int x = 'd' ? 1 : 2;\n"
586
653
"}");
587
654
ASSERT_EQUALS("[test.cpp:2]: (warning) Conversion of char literal 'a' to bool always evaluates to true.\n"
588
655
"[test.cpp:3]: (warning) Conversion of char literal 'b' to bool always evaluates to true.\n"
589
656
"[test.cpp:4]: (warning) Conversion of char literal 'c' to bool always evaluates to true.\n"
657
+
"[test.cpp:5]: (warning) Conversion of char literal 'd' to bool always evaluates to true.\n"
590
658
, errout.str());
591
659
592
660
check("void f() {\n"
593
661
" if('\\0'){}\n"
662
+
" if(L'\\0'){}\n"
594
663
"}");
595
664
ASSERT_EQUALS("", errout.str());
596
665
597
666
check("void f() {\n"
598
667
" if('\\0' || cond){}\n"
668
+
" if(L'\\0' || cond){}\n"
599
669
"}");
600
-
ASSERT_EQUALS("[test.cpp:2]: (warning) Conversion of char literal '\\0' to bool always evaluates to false.\n", errout.str());
670
+
ASSERT_EQUALS("[test.cpp:2]: (warning) Conversion of char literal '\\0' to bool always evaluates to false.\n"
671
+
"[test.cpp:3]: (warning) Conversion of char literal '\\0' to bool always evaluates to false.\n", errout.str());
601
672
}
602
673
603
674
voiddeadStrcmp() {
@@ -606,6 +677,11 @@ class TestString : public TestFixture {
606
677
"}");
607
678
ASSERT_EQUALS("[test.cpp:2]: (warning) The expression 'strcmp(str,\"def\") != 0' is suspicious. It overlaps 'strcmp(str,\"abc\") == 0'.\n", errout.str());
608
679
680
+
check("void f(const wchar_t *str) {\n"
681
+
" if (wcscmp(str, L\"abc\") == 0 || wcscmp(str, L\"def\")) {}\n"
682
+
"}");
683
+
ASSERT_EQUALS("[test.cpp:2]: (warning) The expression 'wcscmp(str,L\"def\") != 0' is suspicious. It overlaps 'wcscmp(str,L\"abc\") == 0'.\n", errout.str());
0 commit comments