util::percentEncodeMini: Fix regression bug removed unsignedness

srange-based for around std::string is convenient but several
functions depend unsigned char for correctness and readability.
pull/138/head
Tatsuhiro Tsujikawa 2013-09-30 21:32:57 +09:00
parent 9768aa9fad
commit b772aa6a5e
2 changed files with 14 additions and 4 deletions

View File

@ -458,9 +458,9 @@ std::string percentEncodeMini(const std::string& src)
return src; return src;
} }
std::string result; std::string result;
for (const auto& c: src) { for (auto c: src) {
if(!inPercentEncodeMini(c)) { if(!inPercentEncodeMini(c)) {
result += fmt("%%%02X", c); result += fmt("%%%02X", static_cast<unsigned char>(c));
} else { } else {
result += c; result += c;
} }
@ -1725,7 +1725,8 @@ bool detectDirTraversal(const std::string& s)
if(s.empty()) { if(s.empty()) {
return false; return false;
} }
for (const auto& ch: s) { for (auto c : s) {
unsigned char ch = c;
if (in(ch, 0x00u, 0x1fu) || ch == 0x7fu) { if (in(ch, 0x00u, 0x1fu) || ch == 0x7fu) {
return true; return true;
} }
@ -1748,7 +1749,8 @@ std::string escapePath(const std::string& s)
{ '"', '*', ':', '<', '>', '?', '\\', '|' }; { '"', '*', ':', '<', '>', '?', '\\', '|' };
#endif // __MINGW32__ #endif // __MINGW32__
std::string d; std::string d;
for(const auto& c: s) { for(auto cc: s) {
unsigned char c = cc;
if(in(c, 0x00u, 0x1fu) || c == 0x7fu if(in(c, 0x00u, 0x1fu) || c == 0x7fu
#ifdef __MINGW32__ #ifdef __MINGW32__
|| std::find(std::begin(WIN_INVALID_PATH_CHARS), || std::find(std::begin(WIN_INVALID_PATH_CHARS),

View File

@ -64,6 +64,7 @@ class UtilTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testUitos); CPPUNIT_TEST(testUitos);
CPPUNIT_TEST(testNtoh64); CPPUNIT_TEST(testNtoh64);
CPPUNIT_TEST(testPercentEncode); CPPUNIT_TEST(testPercentEncode);
CPPUNIT_TEST(testPercentEncodeMini);
CPPUNIT_TEST(testHtmlEscape); CPPUNIT_TEST(testHtmlEscape);
CPPUNIT_TEST(testJoinPath); CPPUNIT_TEST(testJoinPath);
CPPUNIT_TEST(testParseIndexPath); CPPUNIT_TEST(testParseIndexPath);
@ -132,6 +133,7 @@ public:
void testUitos(); void testUitos();
void testNtoh64(); void testNtoh64();
void testPercentEncode(); void testPercentEncode();
void testPercentEncodeMini();
void testHtmlEscape(); void testHtmlEscape();
void testJoinPath(); void testJoinPath();
void testParseIndexPath(); void testParseIndexPath();
@ -1879,6 +1881,12 @@ void UtilTest::testPercentEncode()
CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::percentEncode("1^A ")); CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::percentEncode("1^A "));
} }
void UtilTest::testPercentEncodeMini()
{
CPPUNIT_ASSERT_EQUAL(std::string("%80"),
util::percentEncodeMini({(char)0x80}));
}
void UtilTest::testHtmlEscape() void UtilTest::testHtmlEscape()
{ {
CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"), CPPUNIT_ASSERT_EQUAL(std::string("aria2&lt;&gt;&quot;&#39;util"),