Skip to content

Commit 481ab0f

Browse files
committed
Tests for passing non-pointers as scanf read targets
1 parent eb6bf1b commit 481ab0f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

test/testio.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,11 @@ class TestIO : public TestFixture {
972972
ASSERT_EQUALS("[test.cpp:2]: (warning) %u in format string (no. 1) requires 'unsigned int *' but the argument type is 'const char *'.\n"
973973
"[test.cpp:3]: (warning) %u in format string (no. 1) requires 'unsigned int *' but the argument type is 'const wchar_t *'.\n", errout.str());
974974

975+
check("void foo(long l) {\n"
976+
" scanf(\"%u\", l);\n"
977+
"}", true);
978+
ASSERT_EQUALS("[test.cpp:2]: (warning) %u in format string (no. 1) requires 'unsigned int *' but the argument type is 'signed long'.\n", errout.str());
979+
975980
TEST_SCANF_WARN("%lu","unsigned long","bool");
976981
TEST_SCANF_WARN("%lu","unsigned long","char");
977982
TEST_SCANF_WARN("%lu","unsigned long","signed char");
@@ -1213,6 +1218,11 @@ class TestIO : public TestFixture {
12131218
ASSERT_EQUALS("[test.cpp:2]: (warning) %Ld in format string (no. 1) requires 'long long *' but the argument type is 'const char *'.\n"
12141219
"[test.cpp:3]: (warning) %Ld in format string (no. 1) requires 'long long *' but the argument type is 'const wchar_t *'.\n", errout.str());
12151220

1221+
check("void foo(int i) {\n"
1222+
" scanf(\"%Ld\", i);\n"
1223+
"}", true);
1224+
ASSERT_EQUALS("[test.cpp:2]: (warning) %Ld in format string (no. 1) requires 'long long *' but the argument type is 'signed int'.\n", errout.str());
1225+
12161226
TEST_SCANF_WARN("%ju", "uintmax_t", "bool");
12171227
TEST_SCANF_WARN("%ju", "uintmax_t", "char");
12181228
TEST_SCANF_WARN("%ju", "uintmax_t", "signed char");
@@ -1493,6 +1503,11 @@ class TestIO : public TestFixture {
14931503
ASSERT_EQUALS("[test.cpp:2]: (warning) %d in format string (no. 1) requires 'int *' but the argument type is 'const char *'.\n"
14941504
"[test.cpp:3]: (warning) %d in format string (no. 1) requires 'int *' but the argument type is 'const wchar_t *'.\n", errout.str());
14951505

1506+
check("void foo(long l) {\n"
1507+
" scanf(\"%d\", l);\n"
1508+
"}", true);
1509+
ASSERT_EQUALS("[test.cpp:2]: (warning) %d in format string (no. 1) requires 'int *' but the argument type is 'signed long'.\n", errout.str());
1510+
14961511
TEST_SCANF_WARN("%x", "unsigned int", "bool");
14971512
TEST_SCANF_WARN("%x", "unsigned int", "char");
14981513
TEST_SCANF_WARN("%x", "unsigned int", "signed char");
@@ -1527,6 +1542,11 @@ class TestIO : public TestFixture {
15271542
ASSERT_EQUALS("[test.cpp:2]: (warning) %x in format string (no. 1) requires 'unsigned int *' but the argument type is 'const char *'.\n"
15281543
"[test.cpp:3]: (warning) %x in format string (no. 1) requires 'unsigned int *' but the argument type is 'const wchar_t *'.\n", errout.str());
15291544

1545+
check("void foo(long l) {\n"
1546+
" scanf(\"%x\", l);\n"
1547+
"}", true);
1548+
ASSERT_EQUALS("[test.cpp:2]: (warning) %x in format string (no. 1) requires 'unsigned int *' but the argument type is 'signed long'.\n", errout.str());
1549+
15301550
TEST_SCANF_WARN("%f", "float", "bool");
15311551
TEST_SCANF_WARN("%f", "float", "char");
15321552
TEST_SCANF_WARN("%f", "float", "signed char");
@@ -1561,6 +1581,11 @@ class TestIO : public TestFixture {
15611581
ASSERT_EQUALS("[test.cpp:2]: (warning) %f in format string (no. 1) requires 'float *' but the argument type is 'const char *'.\n"
15621582
"[test.cpp:3]: (warning) %f in format string (no. 1) requires 'float *' but the argument type is 'const wchar_t *'.\n", errout.str());
15631583

1584+
check("void foo(float f) {\n"
1585+
" scanf(\"%f\", f);\n"
1586+
"}", true);
1587+
ASSERT_EQUALS("[test.cpp:2]: (warning) %f in format string (no. 1) requires 'float *' but the argument type is 'float'.\n", errout.str());
1588+
15641589
TEST_SCANF_WARN("%lf", "double", "bool");
15651590
TEST_SCANF_WARN("%lf", "double", "char");
15661591
TEST_SCANF_WARN("%lf", "double", "signed char");
@@ -1649,6 +1674,11 @@ class TestIO : public TestFixture {
16491674
ASSERT_EQUALS("[test.cpp:2]: (warning) %n in format string (no. 1) requires 'int *' but the argument type is 'const char *'.\n"
16501675
"[test.cpp:3]: (warning) %n in format string (no. 1) requires 'int *' but the argument type is 'const wchar_t *'.\n", errout.str());
16511676

1677+
check("void foo(long l) {\n"
1678+
" scanf(\"%n\", l);\n"
1679+
"}", true);
1680+
ASSERT_EQUALS("[test.cpp:2]: (warning) %n in format string (no. 1) requires 'int *' but the argument type is 'signed long'.\n", errout.str());
1681+
16521682
check("void g() {\n" // #5104
16531683
" myvector<int> v1(1);\n"
16541684
" scanf(\"%d\",&v1[0]);\n"

0 commit comments

Comments
 (0)