@@ -96,6 +96,30 @@ class TestBufferOverrun : public TestFixture {
9696 checkBufferOverrun.writeOutsideBufferSize ();
9797 }
9898
99+ void checkposix (const char code[], const char filename[] = " test.cpp" ) {
100+ static bool init;
101+ static Settings settings;
102+ if (!init) {
103+ init = true ;
104+ LOAD_LIB_2 (settings.library , " posix.cfg" );
105+ settings.addEnabled (" warning" );
106+ }
107+
108+ Tokenizer tokenizer (&settings, this );
109+ std::istringstream istr (code);
110+ tokenizer.tokenize (istr, filename);
111+
112+ // Clear the error buffer..
113+ errout.str (" " );
114+
115+ // Check for buffer overruns..
116+ CheckBufferOverrun checkBufferOverrun (&tokenizer, &settings, this );
117+ checkBufferOverrun.bufferOverrun ();
118+ checkBufferOverrun.bufferOverrun2 ();
119+ checkBufferOverrun.arrayIndexThenCheck ();
120+ checkBufferOverrun.writeOutsideBufferSize ();
121+ }
122+
99123
100124 void run () {
101125 TEST_CASE (noerr1);
@@ -169,6 +193,7 @@ class TestBufferOverrun : public TestFixture {
169193 TEST_CASE (array_index_valueflow);
170194
171195 TEST_CASE (buffer_overrun_1_standard_functions);
196+ TEST_CASE (buffer_overrun_1_posix_functions);
172197 TEST_CASE (buffer_overrun_2_struct);
173198 TEST_CASE (buffer_overrun_3);
174199 TEST_CASE (buffer_overrun_4);
@@ -2097,63 +2122,63 @@ class TestBufferOverrun : public TestFixture {
20972122 }
20982123
20992124 void buffer_overrun_1_posix_functions () {
2100- check (" void f(int fd)\n "
2125+ checkposix (" void f(int fd)\n "
21012126 " {\n "
21022127 " char str[3];\n "
21032128 " read(fd, str, 3);\n "
21042129 " }" );
21052130 ASSERT_EQUALS (" " , errout.str ());
21062131
2107- check (" void f(int fd)\n "
2132+ checkposix (" void f(int fd)\n "
21082133 " {\n "
21092134 " char str[3];\n "
21102135 " read(fd, str, 4);\n "
21112136 " }" );
21122137 ASSERT_EQUALS (" [test.cpp:4]: (error) Buffer is accessed out of bounds: str\n " , errout.str ());
21132138
2114- check (" void f(int fd)\n "
2139+ checkposix (" void f(int fd)\n "
21152140 " {\n "
21162141 " char str[3];\n "
21172142 " write(fd, str, 3);\n "
21182143 " }" );
21192144 ASSERT_EQUALS (" " , errout.str ());
21202145
2121- check (" void f(int fd)\n "
2146+ checkposix (" void f(int fd)\n "
21222147 " {\n "
21232148 " char str[3];\n "
21242149 " write(fd, str, 4);\n "
21252150 " }" );
21262151 ASSERT_EQUALS (" [test.cpp:4]: (error) Buffer is accessed out of bounds: str\n " , errout.str ());
21272152
2128- check (" void f()\n "
2153+ checkposix (" void f()\n "
21292154 " {\n "
21302155 " long bb[2];\n "
21312156 " write(stdin, bb, sizeof(bb));\n "
2132- " }" , false , " test.cpp " , false );
2157+ " }" );
21332158 ASSERT_EQUALS (" " , errout.str ());
21342159
2135- check (" void f()\n "
2160+ checkposix (" void f()\n "
21362161 " {\n "
21372162 " char str[3];\n "
21382163 " recv(s, str, 4, 0);\n "
21392164 " }" );
21402165 ASSERT_EQUALS (" [test.cpp:4]: (error) Buffer is accessed out of bounds: str\n " , errout.str ());
21412166
2142- check (" void f()\n "
2167+ checkposix (" void f()\n "
21432168 " {\n "
21442169 " char str[3];\n "
21452170 " recvfrom(s, str, 4, 0, 0x0, 0x0);\n "
21462171 " }" );
21472172 ASSERT_EQUALS (" [test.cpp:4]: (error) Buffer is accessed out of bounds: str\n " , errout.str ());
21482173
2149- check (" void f()\n "
2174+ checkposix (" void f()\n "
21502175 " {\n "
21512176 " char str[3];\n "
21522177 " send(s, str, 4, 0);\n "
21532178 " }" );
21542179 ASSERT_EQUALS (" [test.cpp:4]: (error) Buffer is accessed out of bounds: str\n " , errout.str ());
21552180
2156- check (" void f()\n "
2181+ checkposix (" void f()\n "
21572182 " {\n "
21582183 " char str[3];\n "
21592184 " sendto(s, str, 4, 0, 0x0, 0x0);\n "
0 commit comments