Permalink
Browse files

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>
  • Loading branch information...
psychon committed Jun 13, 2011
1 parent 25ce7de commit 554429a61f432d8cc4ee5ebfee9034008808361f
Showing with 7 additions and 4 deletions.
  1. +7 −4 ZNCString.cpp
View
@@ -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 += ";";
}
}

0 comments on commit 554429a

Please sign in to comment.