From d985a94248b0b3c978f943e1ce5223ed802ed9ec Mon Sep 17 00:00:00 2001 From: Donat Zenichev Date: Tue, 16 May 2023 10:29:45 +0200 Subject: [PATCH] MT#57436 intimeToXml: properly manage the buf size and `snprintf()` Give an exact size of the buf to the `snprintf()` and also treat the returned value from it to eliminate this warning: src/XmlRpcValue.cpp: In member function 'std::string XmlRpc::XmlRpcValue::timeToXml() const': src/XmlRpcValue.cpp:404:53: warning: '%02d' directive output may be truncated writing between 2 and 11 bytes into a region of size between 0 and 7 [-Wformat-truncation=] 404 | snprintf(buf, sizeof(buf)-1, "%04d%02d%02dT%02d:%02d:%02d", | ^~~~ In file included from /usr/include/stdio.h:867, from /usr/include/c++/10/cstdio:42, from /usr/include/c++/10/ext/string_conversions.h:43, from /usr/include/c++/10/bits/basic_string.h:6545, from /usr/include/c++/10/string:55, from src/XmlRpcValue.h:14, from src/XmlRpcValue.cpp:2: /usr/include/x86_64-linux-gnu/bits/stdio2.h:67:35: note: '__builtin___snprintf_chk' output between 18 and 70 bytes into a destination of size 19 67 | return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1, | ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 68 | __bos (__s), __fmt, __va_arg_pack ()); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Change-Id: Ic545bbc942715f433610f49286ee9d0f92ee25f6 --- apps/xmlrpc2di/xmlrpc++/src/XmlRpcValue.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/apps/xmlrpc2di/xmlrpc++/src/XmlRpcValue.cpp b/apps/xmlrpc2di/xmlrpc++/src/XmlRpcValue.cpp index 5585356e..f9f2352a 100644 --- a/apps/xmlrpc2di/xmlrpc++/src/XmlRpcValue.cpp +++ b/apps/xmlrpc2di/xmlrpc++/src/XmlRpcValue.cpp @@ -400,10 +400,13 @@ namespace XmlRpc { std::string XmlRpcValue::timeToXml() const { struct tm* t = _value.asTime; - char buf[20]; - snprintf(buf, sizeof(buf)-1, "%4d%02d%02dT%02d:%02d:%02d", - t->tm_year, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); - buf[sizeof(buf)-1] = 0; + char buf[18]; + + if (snprintf(buf, sizeof(buf), "%04d%02d%02dT%02d:%02d:%02d", + (1900 + t->tm_year), t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec) < 0) + { + XmlRpcUtil::log(2,"timeToXml: issues while trying to write data."); + } std::string xml = VALUE_TAG; xml += DATETIME_TAG;