فهرست منبع

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.
Tatsuhiro Tsujikawa 12 سال پیش
والد
کامیت
b772aa6a5e
2فایلهای تغییر یافته به همراه14 افزوده شده و 4 حذف شده
  1. 6 4
      src/util.cc
  2. 8 0
      test/UtilTest.cc

+ 6 - 4
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<unsigned char>(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),

+ 8 - 0
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&lt;&gt;&quot;&#39;util"),