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

zwoop pushed a commit to branch 8.0.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit ec2d4d827cc65c849d2b86fa4e3f820dda977b82
Author: Alan M. Carroll <a...@apache.org>
AuthorDate: Wed Jun 13 13:31:52 2018 -0500

    BWF: Fix asserts for clang-analyzer.
    The current asserts break under valid use. Unit tests added to verify.
    
    (cherry picked from commit 811092f5ba65d4617052d0e295ae11611c23e4f9)
---
 lib/ts/BufferWriter.h                        | 12 ++++++------
 lib/ts/unit-tests/test_BufferWriterFormat.cc | 24 ++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/lib/ts/BufferWriter.h b/lib/ts/BufferWriter.h
index 3fcffc9..b1c4c9b 100644
--- a/lib/ts/BufferWriter.h
+++ b/lib/ts/BufferWriter.h
@@ -258,12 +258,12 @@ public:
   {
     const size_t newSize = _attempted + length;
 
-    if (newSize <= _capacity) {
-      ink_assert(_buf != nullptr); // make clang-analyzer happy.
-      std::memcpy(_buf + _attempted, data, length);
-    } else if (_attempted < _capacity) {
-      ink_assert(_buf != nullptr); // make clang-analyzer happy.
-      std::memcpy(_buf + _attempted, data, _capacity - _attempted);
+    if (_buf) {
+      if (newSize <= _capacity) {
+        std::memcpy(_buf + _attempted, data, length);
+      } else if (_attempted < _capacity) {
+        std::memcpy(_buf + _attempted, data, _capacity - _attempted);
+      }
     }
     _attempted = newSize;
 
diff --git a/lib/ts/unit-tests/test_BufferWriterFormat.cc 
b/lib/ts/unit-tests/test_BufferWriterFormat.cc
index cda89e7..0f229dc 100644
--- a/lib/ts/unit-tests/test_BufferWriterFormat.cc
+++ b/lib/ts/unit-tests/test_BufferWriterFormat.cc
@@ -281,6 +281,30 @@ TEST_CASE("bwstring", "[bwprint][bwstring]")
   REQUIRE(std::string_view(buff) == "|Deep Silent Complete by Nightwish|");
   snprintf(buff, sizeof(buff), "|%s|", bw.reset().print("Deep Silent Complete 
by {}\0elided junk", "Nightwish"sv).data());
   REQUIRE(std::string_view(buff) == "|Deep Silent Complete by Nightwish|");
+
+  // Special tests for clang analyzer failures - special asserts are needed to 
make it happy but
+  // those can break functionality.
+  fmt = "Did you know? {}{} is {}"sv;
+  s.resize(0);
+  ts::bwprint(s, fmt, "Lady "sv, "Persia"sv, "not mean");
+  REQUIRE(s == "Did you know? Lady Persia is not mean");
+  s.resize(0);
+  ts::bwprint(s, fmt, ""sv, "Phil", "correct");
+  REQUIRE(s == "Did you know? Phil is correct");
+  s.resize(0);
+  ts::bwprint(s, fmt, std::string_view(), "Leif", "confused");
+  REQUIRE(s == "Did you know? Leif is confused");
+
+  {
+    std::string out;
+    ts::bwprint(out, fmt, ""sv, "Phil", "correct");
+    REQUIRE(out == "Did you know? Phil is correct");
+  }
+  {
+    std::string out;
+    ts::bwprint(out, fmt, std::string_view(), "Leif", "confused");
+    REQUIRE(out == "Did you know? Leif is confused");
+  }
 }
 
 TEST_CASE("BWFormat integral", "[bwprint][bwformat]")

Reply via email to