Fixed bug #1656547: segfault in urlUtils::decode() if the string ends with '%'.

This commit is contained in:
Vincent Richard 2007-05-21 16:01:12 +00:00
parent 28483d2ef8
commit a25333888d
2 changed files with 28 additions and 19 deletions

View File

@ -79,14 +79,16 @@ const string urlUtils::decode(const string& s)
{ {
case '%': case '%':
{ {
const char_t p = (++it != s.end() ? *it : 0); ++it; // skip '%'
const char_t q = (++it != s.end() ? *it : 0);
const char_t p = (it != s.end() ? *(it++) : 0);
const char_t q = (it != s.end() ? *(it++) : 0);
unsigned char r = 0; unsigned char r = 0;
switch (p) switch (p)
{ {
case 0: r = '?'; break; case 0: r = '%'; break;
case 'a': case 'A': r = 10; break; case 'a': case 'A': r = 10; break;
case 'b': case 'B': r = 11; break; case 'b': case 'B': r = 11; break;
case 'c': case 'C': r = 12; break; case 'c': case 'C': r = 12; break;
@ -96,25 +98,23 @@ const string urlUtils::decode(const string& s)
default: r = p - '0'; break; default: r = p - '0'; break;
} }
r *= 16; if (q != 0)
switch (q)
{ {
case 0: r = '?'; break; r *= 16;
case 'a': case 'A': r += 10; break;
case 'b': case 'B': r += 11; break; switch (q)
case 'c': case 'C': r += 12; break; {
case 'd': case 'D': r += 13; break; case 'a': case 'A': r += 10; break;
case 'e': case 'E': r += 14; break; case 'b': case 'B': r += 11; break;
case 'f': case 'F': r += 15; break; case 'c': case 'C': r += 12; break;
default: r += q - '0'; break; case 'd': case 'D': r += 13; break;
case 'e': case 'E': r += 14; break;
case 'f': case 'F': r += 15; break;
default: r += q - '0'; break;
}
} }
result += static_cast <string::value_type>(r); result += static_cast <string::value_type>(r);
if (it != s.end())
++it;
break; break;
} }
default: default:

View File

@ -42,6 +42,7 @@ VMIME_TEST_SUITE_BEGIN
VMIME_TEST(testGenerate) VMIME_TEST(testGenerate)
VMIME_TEST(testUtilsEncode) VMIME_TEST(testUtilsEncode)
VMIME_TEST(testUtilsDecode) VMIME_TEST(testUtilsDecode)
VMIME_TEST(testUtilsDecodeSpecialCases)
VMIME_TEST(testUtilsEncodeReservedChars) VMIME_TEST(testUtilsEncodeReservedChars)
VMIME_TEST(testUtilsEncodeUnsafeChars) VMIME_TEST(testUtilsEncodeUnsafeChars)
VMIME_TEST_LIST_END VMIME_TEST_LIST_END
@ -239,7 +240,7 @@ VMIME_TEST_SUITE_BEGIN
{ {
std::ostringstream ossTest; std::ostringstream ossTest;
ossTest << "%" << "0123456789ABCDEF"[i / 16] ossTest << "%" << "0123456789ABCDEF"[i / 16]
<< "0123456789ABCDEF"[i % 16]; << "0123456789ABCDEF"[i % 16];
std::ostringstream ossNum; std::ostringstream ossNum;
ossNum << i; ossNum << i;
@ -253,6 +254,14 @@ VMIME_TEST_SUITE_BEGIN
} }
void testUtilsDecodeSpecialCases()
{
// Bug #1656547: segfault with '%' at the end of the string
VASSERT_EQ("1.1", "sadfsda%", vmime::utility::urlUtils::decode("sadfsda%"));
VASSERT_EQ("1.2", "sadfsda\x05", vmime::utility::urlUtils::decode("sadfsda%5"));
VASSERT_EQ("1.3", "sadfsda\x42", vmime::utility::urlUtils::decode("sadfsda%42"));
}
void testUtilsEncodeReservedChars() void testUtilsEncodeReservedChars()
{ {
VASSERT_EQ("1", "%24", vmime::utility::urlUtils::encode("$")); VASSERT_EQ("1", "%24", vmime::utility::urlUtils::encode("$"));