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 <psychon@znc.in>
This commit is contained in:
Uli Schlachter
2011-06-13 15:19:17 +02:00
parent 25ce7de020
commit 554429a61f
+7 -4
View File
@@ -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 += ";";
}
}