Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#181 sprintf(3) is not defined when building with g++ 3.4.6

open
nobody
None
5
2007-09-03
2007-09-03
No

I'm checking Squid-3 and, aside from other issues related to that software, I noticed that cppunit/TestAssert.h:82 fresh out of CVS fails because sprintf(3) is (correctly) not defined. snprintf(3) should be used instead, much like sprintf_s(3) is used two lines above with Visual Studio. Applying the patch

Index: include/cppunit/TestAssert.h

RCS file: /cvsroot/cppunit/cppunit/include/cppunit/TestAssert.h,v
retrieving revision 1.30
diff -u -r1.30 TestAssert.h
--- include/cppunit/TestAssert.h 24 Feb 2007 21:13:04 -0000 1.30
+++ include/cppunit/TestAssert.h 3 Sep 2007 17:46:48 -0000
@@ -79,7 +79,7 @@
#ifdef __STDC_SECURE_LIB__ // Use secure version with visual studio 2005 to avoid warning.
sprintf_s(buffer, sizeof(buffer), "%.*g", precision, x);
#else
- sprintf(buffer, "%.*g", precision, x);
+ snprintf(buffer, sizeof(buffer), "%.*g", precision, x);
#endif
return buffer;
}

solves the problem.

I suggest availability of snprintf() and sprintf() be checked at configure time, and the former preferred over the latter if both are available. Please let me know if you want me to submit a complete patch.

Sincerely, p.

Discussion