From 6580e0eb21e7d1e4ddf46cbbe588380cf6d7ffee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 24 Mar 2009 18:43:39 +0100 Subject: [PATCH] fixed snprintf false positive (#210) --- src/checkbufferoverrun.cpp | 2 +- test/testbufferoverrun.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/checkbufferoverrun.cpp b/src/checkbufferoverrun.cpp index 09b29b177..d90ec2643 100644 --- a/src/checkbufferoverrun.cpp +++ b/src/checkbufferoverrun.cpp @@ -314,7 +314,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co } // snprintf.. - if (varid > 0 && Token::Match(tok, "snprintf ( %varid% , %num%", varid)) + if (varid > 0 && Token::Match(tok, "snprintf ( %varid% , %num% ,", varid)) { int n = std::atoi(tok->strAt(4)); if (n > size) diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 14be167b7..01457daff 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -91,6 +91,7 @@ private: TEST_CASE(snprintf1); TEST_CASE(snprintf2); TEST_CASE(snprintf3); + TEST_CASE(snprintf4); TEST_CASE(strncat1); TEST_CASE(strncat2); @@ -470,6 +471,16 @@ private: ASSERT_EQUALS(std::string(""), errout.str()); } + void snprintf4() + { + check("void f(int x)\n" + "{\n" + " char str[5];\n" + " snprintf(str, 8 - x, \"abcdefghijkl\");\n" + "}\n"); + ASSERT_EQUALS(std::string(""), errout.str()); + } +