From ad89a84796294f0cb0ebece148ec396213a2cf1a Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Sun, 26 Dec 2010 21:40:58 +0200 Subject: [PATCH] Improve error message about overlapping buffers for s[n]printf(). See forum thread: https://sourceforge.net/apps/phpbb/cppcheck/viewtopic.php?f=3&t=192&start=0 --- lib/checkother.cpp | 8 +++++++- test/testother.cpp | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4127327f0..0b3f21b7b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2581,7 +2581,13 @@ void CheckOther::dangerousUsageStrtolError(const Token *tok) void CheckOther::sprintfOverlappingDataError(const Token *tok, const std::string &varname) { - reportError(tok, Severity::error, "sprintfOverlappingData", "Undefined behaviour: " + varname + " is used wrong in call to sprintf or snprintf. Quote: If copying takes place between objects that overlap as a result of a call to sprintf() or snprintf(), the results are undefined."); + reportError(tok, Severity::error, "sprintfOverlappingData", + "Undefined behavior: variable is used as parameter and destination in s[n]printf().\n" + "The variable '" + varname + "' is used both as parameter and destination in " + "and destination buffer overlap. Quote from glibc (C-library) documentation " + "(http://www.gnu.org/software/libc/manual/html_mono/libc.html#Formatted-Output-Functions): " + "'If copying takes place between objects that overlap as a result of a call " + "to sprintf() or snprintf(), the results are undefined.'"); } void CheckOther::udivError(const Token *tok) diff --git a/test/testother.cpp b/test/testother.cpp index bec5f925e..8d484707e 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -265,7 +265,7 @@ private: " char buf[100];\n" " sprintf(buf,\"%s\",buf);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (error) Undefined behaviour: buf is used wrong in call to sprintf or snprintf. Quote: If copying takes place between objects that overlap as a result of a call to sprintf() or snprintf(), the results are undefined.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (error) Undefined behavior: variable is used as parameter and destination in s[n]printf().\n", errout.str()); } void sprintf2()