moonchen commented on code in PR #13404:
URL: https://github.com/apache/trafficserver/pull/13404#discussion_r3611027531


##########
src/tscore/unit_tests/test_Encoding.cc:
##########
@@ -57,6 +57,29 @@ TEST_CASE("Encoding pure escapify url", "[pure_esc_url]")
   }
 }
 
+TEST_CASE("Encoding escapify url without a terminator", 
"[esc_url_unterminated]")
+{
+  // The source is a counted string, not a C string, so nothing may be read 
past len_in.
+  // Sized exactly so that a read past the end is caught by a sanitizer.
+  constexpr std::string_view src{"abcdef"};
+
+  char *unterminated = static_cast<char *>(std::malloc(src.size()));
+  std::memcpy(unterminated, src.data(), src.size());
+
+  char output[128];
+  int  output_len;
+
+  REQUIRE(Encoding::pure_escapify_url(nullptr, unterminated, src.size(), 
&output_len, output, sizeof(output)) != nullptr);
+  CHECK(output_len == static_cast<int>(src.size()));
+  CHECK(std::string_view(output, output_len) == src);
+
+  REQUIRE(Encoding::escapify_url(nullptr, unterminated, src.size(), 
&output_len, output, sizeof(output)) != nullptr);
+  CHECK(output_len == static_cast<int>(src.size()));
+  CHECK(std::string_view(output, output_len) == src);
+
+  std::free(unterminated);

Review Comment:
   The most common way to handle this in ATS unit tests is with a 
`std::vector<char>` (see `test_Huffmancode.cc` for the same exact-size-buffer 
pattern) — RAII handles the leak-on-`REQUIRE`-failure case, and the allocator 
throws on failure instead of needing a null check.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to