From 5a08b7b7d6df5ac45e6a987dfa59fe2e18544ff5 Mon Sep 17 00:00:00 2001 From: psychon Date: Mon, 30 Nov 2009 18:16:16 +0000 Subject: [PATCH] Really check the return value of strftime() If strftime() returns 0, the buffer we passed to it shouldn't be touched at all, because it's not guaranteed to be null-terminated. Someone (*cough*) already tried to fix this in r1394, but failed badly. Thanks to DarthGandalf for spotting this and providing a patch. git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1671 726aef4b-f618-498e-8847-2d620e286838 --- User.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/User.cpp b/User.cpp index a9bcf2cc..5db4bf31 100644 --- a/User.cpp +++ b/User.cpp @@ -198,17 +198,16 @@ CString CUser::AddTimestamp(const CString& sStr) const { CString& CUser::AddTimestamp(const CString& sStr, CString& sRet) const { char szTimestamp[1024]; time_t tm; + sRet = sStr; - if (GetTimestampFormat().empty() || (!m_bAppendTimestamp && !m_bPrependTimestamp)) { - sRet = sStr; - } else { + if (!GetTimestampFormat().empty() && (m_bAppendTimestamp || m_bPrependTimestamp)) { time(&tm); tm += (time_t)(m_fTimezoneOffset * 60 * 60); // offset is in hours size_t i = strftime(szTimestamp, sizeof(szTimestamp), GetTimestampFormat().c_str(), localtime(&tm)); - if (i != 0) { - sRet = sStr; - } else { - sRet.clear(); + // If strftime returns 0, an error occured in format, or result is empty + // In both cases just don't prepend/append anything to our string + if (0 == i) { + return sRet; } if (m_bPrependTimestamp) {