From b772aa6a5e5ad80c1fa52b5fb881acaef571e847 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 30 Sep 2013 21:32:57 +0900 Subject: [PATCH] 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. --- src/util.cc | 10 ++++++---- test/UtilTest.cc | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/util.cc b/src/util.cc index 4788d631..b10b4547 100644 --- a/src/util.cc +++ b/src/util.cc @@ -458,9 +458,9 @@ std::string percentEncodeMini(const std::string& src) return src; } std::string result; - for (const auto& c: src) { + for (auto c: src) { if(!inPercentEncodeMini(c)) { - result += fmt("%%%02X", c); + result += fmt("%%%02X", static_cast(c)); } else { result += c; } @@ -1725,7 +1725,8 @@ bool detectDirTraversal(const std::string& s) if(s.empty()) { return false; } - for (const auto& ch: s) { + for (auto c : s) { + unsigned char ch = c; if (in(ch, 0x00u, 0x1fu) || ch == 0x7fu) { return true; } @@ -1748,7 +1749,8 @@ std::string escapePath(const std::string& s) { '"', '*', ':', '<', '>', '?', '\\', '|' }; #endif // __MINGW32__ std::string d; - for(const auto& c: s) { + for(auto cc: s) { + unsigned char c = cc; if(in(c, 0x00u, 0x1fu) || c == 0x7fu #ifdef __MINGW32__ || std::find(std::begin(WIN_INVALID_PATH_CHARS), diff --git a/test/UtilTest.cc b/test/UtilTest.cc index c1a0537e..aa70512a 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -64,6 +64,7 @@ class UtilTest:public CppUnit::TestFixture { CPPUNIT_TEST(testUitos); CPPUNIT_TEST(testNtoh64); CPPUNIT_TEST(testPercentEncode); + CPPUNIT_TEST(testPercentEncodeMini); CPPUNIT_TEST(testHtmlEscape); CPPUNIT_TEST(testJoinPath); CPPUNIT_TEST(testParseIndexPath); @@ -132,6 +133,7 @@ public: void testUitos(); void testNtoh64(); void testPercentEncode(); + void testPercentEncodeMini(); void testHtmlEscape(); void testJoinPath(); void testParseIndexPath(); @@ -1879,6 +1881,12 @@ void UtilTest::testPercentEncode() 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() { CPPUNIT_ASSERT_EQUAL(std::string("aria2<>"'util"),