From fcba5b88c287efb3bbe8f74a8f25d312ccf24f4f Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Mon, 9 Oct 2017 18:50:24 +0300 Subject: [PATCH] Improve test coverage for string literals passed as read target into scanf --- test/testio.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/testio.cpp b/test/testio.cpp index 9e4b356c0..3590c9725 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -934,6 +934,13 @@ private: TEST_SCANF_WARN_AKA("%u", "unsigned int", "std::intptr_t", "signed long", "signed long long"); TEST_SCANF_WARN_AKA("%u", "unsigned int", "std::uintptr_t", "unsigned long", "unsigned long long"); + check("void foo() {\n" + " scanf(\"%u\", \"s3\");\n" + " scanf(\"%u\", L\"s5W\");\n" + "}", true); + ASSERT_EQUALS("[test.cpp:2]: (warning) %u in format string (no. 1) requires 'unsigned int *' but the argument type is 'const char *'.\n" + "[test.cpp:3]: (warning) %u in format string (no. 1) requires 'unsigned int *' but the argument type is 'const wchar_t *'.\n", errout.str()); + TEST_SCANF_WARN("%lu","unsigned long","bool"); TEST_SCANF_WARN("%lu","unsigned long","char"); TEST_SCANF_WARN("%lu","unsigned long","signed char"); @@ -1203,6 +1210,13 @@ private: TEST_SCANF_WARN_AKA("%d", "int", "std::intptr_t", "signed long", "signed long long"); TEST_SCANF_WARN_AKA("%d", "int", "std::uintptr_t", "unsigned long", "unsigned long long"); + check("void foo() {\n" + " scanf(\"%d\", \"s3\");\n" + " scanf(\"%d\", L\"s5W\");\n" + "}", true); + ASSERT_EQUALS("[test.cpp:2]: (warning) %d in format string (no. 1) requires 'int *' but the argument type is 'const char *'.\n" + "[test.cpp:3]: (warning) %d in format string (no. 1) requires 'int *' but the argument type is 'const wchar_t *'.\n", errout.str()); + TEST_SCANF_WARN("%x", "unsigned int", "bool"); TEST_SCANF_WARN("%x", "unsigned int", "char"); TEST_SCANF_WARN("%x", "unsigned int", "signed char"); @@ -1230,6 +1244,13 @@ private: TEST_SCANF_WARN_AKA("%x", "unsigned int", "std::intptr_t", "signed long", "signed long long"); TEST_SCANF_WARN_AKA("%x", "unsigned int", "std::uintptr_t", "unsigned long", "unsigned long long"); + check("void foo() {\n" + " scanf(\"%x\", \"s3\");\n" + " scanf(\"%x\", L\"s5W\");\n" + "}", true); + ASSERT_EQUALS("[test.cpp:2]: (warning) %x in format string (no. 1) requires 'unsigned int *' but the argument type is 'const char *'.\n" + "[test.cpp:3]: (warning) %x in format string (no. 1) requires 'unsigned int *' but the argument type is 'const wchar_t *'.\n", errout.str()); + TEST_SCANF_WARN("%f", "float", "bool"); TEST_SCANF_WARN("%f", "float", "char"); TEST_SCANF_WARN("%f", "float", "signed char"); @@ -1257,6 +1278,13 @@ private: TEST_SCANF_WARN_AKA("%f", "float", "std::intptr_t", "signed long", "signed long long"); TEST_SCANF_WARN_AKA("%f", "float", "std::uintptr_t", "unsigned long", "unsigned long long"); + check("void foo() {\n" + " scanf(\"%f\", \"s3\");\n" + " scanf(\"%f\", L\"s5W\");\n" + "}", true); + ASSERT_EQUALS("[test.cpp:2]: (warning) %f in format string (no. 1) requires 'float *' but the argument type is 'const char *'.\n" + "[test.cpp:3]: (warning) %f in format string (no. 1) requires 'float *' but the argument type is 'const wchar_t *'.\n", errout.str()); + TEST_SCANF_WARN("%lf", "double", "bool"); TEST_SCANF_WARN("%lf", "double", "char"); TEST_SCANF_WARN("%lf", "double", "signed char"); @@ -1338,6 +1366,13 @@ private: TEST_SCANF_WARN_AKA("%n", "int", "std::intptr_t", "signed long", "signed long long"); TEST_SCANF_WARN_AKA("%n", "int", "std::uintptr_t", "unsigned long", "unsigned long long"); + check("void foo() {\n" + " scanf(\"%n\", \"s3\");\n" + " scanf(\"%n\", L\"s5W\");\n" + "}", true); + ASSERT_EQUALS("[test.cpp:2]: (warning) %n in format string (no. 1) requires 'int *' but the argument type is 'const char *'.\n" + "[test.cpp:3]: (warning) %n in format string (no. 1) requires 'int *' but the argument type is 'const wchar_t *'.\n", errout.str()); + check("void g() {\n" // #5104 " myvector v1(1);\n" " scanf(\"%d\",&v1[0]);\n"