This is an automated email from the ASF dual-hosted git repository.

amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 9d90a51  TextView: Fix bug in rtrim_if when entire view is cleared.
9d90a51 is described below

commit 9d90a51148411effb5f337067829f87c9b8e4795
Author: Alan M. Carroll <[email protected]>
AuthorDate: Fri Jun 14 17:43:21 2019 -0500

    TextView: Fix bug in rtrim_if when entire view is cleared.
---
 include/tscpp/util/TextView.h          | 12 +++--
 proxy/hdrs/unit_tests/test_HdrUtils.cc | 84 ++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+), 5 deletions(-)

diff --git a/include/tscpp/util/TextView.h b/include/tscpp/util/TextView.h
index 96c56f9..894b166 100644
--- a/include/tscpp/util/TextView.h
+++ b/include/tscpp/util/TextView.h
@@ -1082,7 +1082,7 @@ TextView::rtrim(super_type const &delimiters)
   const char *spot  = this->data_end();
   const char *limit = this->data();
 
-  while (limit < spot && valid[static_cast<uint8_t>(*--spot)])
+  while (limit < spot-- && valid[static_cast<uint8_t>(*spot)])
     ;
 
   this->remove_suffix(this->data_end() - (spot + 1));
@@ -1102,7 +1102,9 @@ TextView::trim(super_type const &delimiters)
     ;
   this->remove_prefix(spot - this->data());
 
-  for (spot = this->data_end(), limit = this->data(); limit < spot && 
valid[static_cast<uint8_t>(*--spot)];)
+  spot  = this->data_end();
+  limit = this->data();
+  while (limit < spot-- && valid[static_cast<uint8_t>(*spot)])
     ;
   this->remove_suffix(this->data_end() - (spot + 1));
 
@@ -1131,9 +1133,9 @@ template <typename F>
 inline TextView &
 TextView::rtrim_if(F const &pred)
 {
-  const char *spot;
-  const char *limit;
-  for (spot = this->data_end(), limit = this->data(); limit < spot && 
pred(*--spot);)
+  const char *spot  = this->data_end();
+  const char *limit = this->data();
+  while (limit < spot-- && pred(*spot))
     ;
   this->remove_suffix(this->data_end() - (spot + 1));
   return *this;
diff --git a/proxy/hdrs/unit_tests/test_HdrUtils.cc 
b/proxy/hdrs/unit_tests/test_HdrUtils.cc
index fe085c9..50cca3a 100644
--- a/proxy/hdrs/unit_tests/test_HdrUtils.cc
+++ b/proxy/hdrs/unit_tests/test_HdrUtils.cc
@@ -120,3 +120,87 @@ TEST_CASE("HdrUtils", "[proxy][hdrutils]")
   value = iter.get_next();
   REQUIRE(value.empty());
 }
+
+TEST_CASE("HdrUtils 2", "[proxy][hdrutils]")
+{
+  // Test empty field.
+  static constexpr ts::TextView text{"Host: example.one\r\n"
+                                     "Connection: keep-alive\r\n"
+                                     "Vary:\r\n"
+                                     "After: value\r\n"
+                                     "\r\n"};
+  static constexpr ts::TextView connection_tag{"Connection"};
+  static constexpr ts::TextView vary_tag{"Vary"};
+  static constexpr ts::TextView after_tag{"After"};
+
+  char buff[text.size() + 1];
+
+  HdrHeap *heap = new_HdrHeap(HdrHeap::DEFAULT_SIZE + 64);
+  MIMEParser parser;
+  char const *real_s = text.data();
+  char const *real_e = text.data_end();
+  MIMEHdr mime;
+
+  mime.create(heap);
+  mime_parser_init(&parser);
+  auto result = mime_parser_parse(&parser, heap, mime.m_mime, &real_s, real_e, 
false, true);
+  REQUIRE(PARSE_RESULT_DONE == result);
+
+  MIMEField *field{mime.field_find(connection_tag.data(), 
int(connection_tag.size()))};
+  REQUIRE(mime_hdr_fields_count(mime.m_mime) == 4);
+  REQUIRE(field != nullptr);
+  field = mime.field_find(vary_tag.data(), static_cast<int>(vary_tag.size()));
+  REQUIRE(field != nullptr);
+  REQUIRE(field->m_len_value == 0);
+  field = mime.field_find(after_tag.data(), 
static_cast<int>(after_tag.size()));
+  REQUIRE(field != nullptr);
+
+  int idx    = 0;
+  int skip   = 0;
+  auto parse = mime_hdr_print(heap, mime.m_mime, buff, 
static_cast<int>(sizeof(buff)), &idx, &skip);
+  REQUIRE(parse != 0);
+  REQUIRE(idx == text.size());
+  REQUIRE(0 == memcmp(ts::TextView(buff, idx), text));
+};
+
+TEST_CASE("HdrUtils 3", "[proxy][hdrutils]")
+{
+  // Test empty field.
+  static constexpr ts::TextView text{"Host: example.one\r\n"
+                                     "Connection: keep-alive\r\n"
+                                     "Before: value\r\n"
+                                     "Vary: \r\n"
+                                     "\r\n"};
+  static constexpr ts::TextView connection_tag{"Connection"};
+  static constexpr ts::TextView vary_tag{"Vary"};
+  static constexpr ts::TextView before_tag{"Before"};
+
+  char buff[text.size() + 1];
+
+  HdrHeap *heap = new_HdrHeap(HdrHeap::DEFAULT_SIZE + 64);
+  MIMEParser parser;
+  char const *real_s = text.data();
+  char const *real_e = text.data_end();
+  MIMEHdr mime;
+
+  mime.create(heap);
+  mime_parser_init(&parser);
+  auto result = mime_parser_parse(&parser, heap, mime.m_mime, &real_s, real_e, 
false, true);
+  REQUIRE(PARSE_RESULT_DONE == result);
+
+  MIMEField *field{mime.field_find(connection_tag.data(), 
int(connection_tag.size()))};
+  REQUIRE(mime_hdr_fields_count(mime.m_mime) == 4);
+  REQUIRE(field != nullptr);
+  field = mime.field_find(vary_tag.data(), static_cast<int>(vary_tag.size()));
+  REQUIRE(field != nullptr);
+  REQUIRE(field->m_len_value == 0);
+  field = mime.field_find(before_tag.data(), 
static_cast<int>(before_tag.size()));
+  REQUIRE(field != nullptr);
+
+  int idx    = 0;
+  int skip   = 0;
+  auto parse = mime_hdr_print(heap, mime.m_mime, buff, 
static_cast<int>(sizeof(buff)), &idx, &skip);
+  REQUIRE(parse != 0);
+  REQUIRE(idx == text.size());
+  REQUIRE(0 == memcmp(ts::TextView(buff, idx), text));
+};

Reply via email to