Commit a35d4ce9ccb3eb5903df3d221fdfd9a0d1fb5c37

Authored by Jay Berkenbilt
1 parent 23207143

Fix bounds error in utf16_to_utf8 conversion

libqpdf/QUtil.cc
@@ -1955,7 +1955,7 @@ QUtil::utf16_to_utf8(std::string const& val) @@ -1955,7 +1955,7 @@ QUtil::utf16_to_utf8(std::string const& val)
1955 } 1955 }
1956 // If the string has an odd number of bytes, the last byte is 1956 // If the string has an odd number of bytes, the last byte is
1957 // ignored. 1957 // ignored.
1958 - for (size_t i = start; i < len; i += 2) 1958 + for (size_t i = start; i + 1 < len; i += 2)
1959 { 1959 {
1960 // Convert from UTF16-BE. If we get a malformed 1960 // Convert from UTF16-BE. If we get a malformed
1961 // codepoint, this code will generate incorrect output 1961 // codepoint, this code will generate incorrect output
libtests/qtest/qutil/qutil.out
@@ -53,6 +53,8 @@ HAGOOGAMAGOOGLE: 0 @@ -53,6 +53,8 @@ HAGOOGAMAGOOGLE: 0
53 0xdead -> ff fd 53 0xdead -> ff fd
54 0x7fffffff -> ff fd 54 0x7fffffff -> ff fd
55 0x80000000 -> ff fd 55 0x80000000 -> ff fd
  56
  57
56 ---- utf8_to_ascii 58 ---- utf8_to_ascii
57 ¿Does π have fingers? 59 ¿Does π have fingers?
58 ?Does ? have fingers? 60 ?Does ? have fingers?
libtests/qutil.cc
@@ -238,6 +238,10 @@ void to_utf16_test() @@ -238,6 +238,10 @@ void to_utf16_test()
238 print_utf16(0xdeadUL); 238 print_utf16(0xdeadUL);
239 print_utf16(0x7fffffffUL); 239 print_utf16(0x7fffffffUL);
240 print_utf16(0x80000000UL); 240 print_utf16(0x80000000UL);
  241 +
  242 + std::string s(QUtil::utf8_to_utf16("\xcf\x80"));
  243 + std::cout << QUtil::utf16_to_utf8(s) << std::endl;
  244 + std::cout << QUtil::utf16_to_utf8(s + ".") << std::endl;
241 } 245 }
242 246
243 void utf8_to_ascii_test() 247 void utf8_to_ascii_test()