From 554429a61f432d8cc4ee5ebfee9034008808361f Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Mon, 13 Jun 2011 15:19:17 +0200 Subject: [PATCH] Fix a bug in MCString::Encode() For character values above 127, the signed char that could be used here did the wrong thing. That is, *it >> 4 returned a negative value and the array hexdigits was read indexed with that wrong value. Fix this by explicitly using unsigned char. Thanks to crocket for reporting this bug which he found with perform (broken entries after a restart/reload). Signed-off-by: Uli Schlachter --- ZNCString.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ZNCString.cpp b/ZNCString.cpp index 8c511cbc..d8e0e9fa 100644 --- a/ZNCString.cpp +++ b/ZNCString.cpp @@ -1098,12 +1098,15 @@ static const char hexdigits[] = "0123456789abcdef"; CString& MCString::Encode(CString& sValue) const { CString sTmp; for (CString::iterator it = sValue.begin(); it != sValue.end(); ++it) { - if (isalnum(*it)) { - sTmp += *it; + // isalnum() needs unsigned char as argument and this code + // assumes unsigned, too. + unsigned char c = *it; + if (isalnum(c)) { + sTmp += c; } else { sTmp += "%"; - sTmp += hexdigits[*it >> 4]; - sTmp += hexdigits[*it & 0xf]; + sTmp += hexdigits[c >> 4]; + sTmp += hexdigits[c & 0xf]; sTmp += ";"; } }