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

bcall 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 daa13fc97a Coverity 1497353: Use of 32-bit time_t (#10505)
daa13fc97a is described below

commit daa13fc97ad89d7eb5ea58b0ec97382486a749e7
Author: Bryan Call <bc...@apache.org>
AuthorDate: Mon Sep 25 12:35:07 2023 -0700

    Coverity 1497353: Use of 32-bit time_t (#10505)
    
    *Found a couple bugs when fixing the 32bit time_t issue:
    1. ink_atoui was returning a max value of INT_MAX instead of UINT_MAX.
    2. mime_field_value_set_uint64 had too short of a buffer.
---
 include/tscore/ParseRules.h              |   6 +-
 proxy/hdrs/MIME.cc                       |   2 +-
 proxy/hdrs/MIME.h                        |   3 +
 src/tscore/CMakeLists.txt                |   1 +
 src/tscore/unit_tests/test_ink_string.cc | 170 +++++++++++++++++++++++++++++++
 5 files changed, 178 insertions(+), 4 deletions(-)

diff --git a/include/tscore/ParseRules.h b/include/tscore/ParseRules.h
index ea7c783050..10c26df3f1 100644
--- a/include/tscore/ParseRules.h
+++ b/include/tscore/ParseRules.h
@@ -872,8 +872,8 @@ ink_atoui(const char *str)
 {
   uint64_t val = ink_atoui64(str);
 
-  if (val > INT_MAX)
-    return INT_MAX;
+  if (val > UINT_MAX)
+    return UINT_MAX;
   else
-    return static_cast<int>(val);
+    return static_cast<unsigned int>(val);
 }
diff --git a/proxy/hdrs/MIME.cc b/proxy/hdrs/MIME.cc
index 527bbda556..4062e1f96c 100644
--- a/proxy/hdrs/MIME.cc
+++ b/proxy/hdrs/MIME.cc
@@ -2187,7 +2187,7 @@ mime_field_value_set_uint(HdrHeap *heap, MIMEHdrImpl *mh, 
MIMEField *field, uint
 void
 mime_field_value_set_int64(HdrHeap *heap, MIMEHdrImpl *mh, MIMEField *field, 
int64_t value)
 {
-  char buf[20];
+  char buf[21];
   int len = mime_format_int64(buf, value, sizeof(buf));
   mime_field_value_set(heap, mh, field, buf, len, true);
 }
diff --git a/proxy/hdrs/MIME.h b/proxy/hdrs/MIME.h
index e91b412069..d215772c27 100644
--- a/proxy/hdrs/MIME.h
+++ b/proxy/hdrs/MIME.h
@@ -917,6 +917,7 @@ int mime_field_length_get(MIMEField *field);
 int mime_format_int(char *buf, int32_t val, size_t buf_len);
 int mime_format_uint(char *buf, uint32_t val, size_t buf_len);
 int mime_format_int64(char *buf, int64_t val, size_t buf_len);
+int mime_format_uint64(char *buf, uint64_t val, size_t buf_len);
 
 void mime_days_since_epoch_to_mdy_slowcase(unsigned int days_since_jan_1_1970, 
int *m_return, int *d_return, int *y_return);
 void mime_days_since_epoch_to_mdy(unsigned int days_since_jan_1_1970, int 
*m_return, int *d_return, int *y_return);
@@ -1856,6 +1857,8 @@ MIMEHdr::set_age(time_t value)
     if (sizeof(time_t) > 4) {
       value_set_int64(MIME_FIELD_AGE, MIME_LEN_AGE, value);
     } else {
+      // Only on systems where time_t is 32 bits
+      // coverity[Y2K38_SAFETY]
       value_set_uint(MIME_FIELD_AGE, MIME_LEN_AGE, value);
     }
   }
diff --git a/src/tscore/CMakeLists.txt b/src/tscore/CMakeLists.txt
index ebffeb7e6e..752b5171a5 100644
--- a/src/tscore/CMakeLists.txt
+++ b/src/tscore/CMakeLists.txt
@@ -161,6 +161,7 @@ add_executable(test_tscore
         unit_tests/test_arena.cc
         unit_tests/test_ink_inet.cc
         unit_tests/test_ink_memory.cc
+        unit_tests/test_ink_string.cc
         unit_tests/test_layout.cc
         unit_tests/test_scoped_resource.cc
         unit_tests/unit_test_main.cc
diff --git a/src/tscore/unit_tests/test_ink_string.cc 
b/src/tscore/unit_tests/test_ink_string.cc
new file mode 100644
index 0000000000..d298389f8c
--- /dev/null
+++ b/src/tscore/unit_tests/test_ink_string.cc
@@ -0,0 +1,170 @@
+/** @file
+
+    test ink_string.h - string utility functions
+
+    @section license License
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+*/
+
+#include <tscore/ink_string.h>
+#include <tscore/ParseRules.h>
+#include <catch.hpp>
+#include <string_view>
+
+//-------------------------------------------------------------------------
+// ink_fast_ltoa test
+//-------------------------------------------------------------------------
+struct int64_item {
+  int64_t n;
+  std::string_view s;
+};
+
+constexpr int64_item int64_tests[] = {
+  {{0},                    {"0"}                   },
+  {{1},                    {"1"}                   },
+  {{10},                   {"10"}                  },
+  {{100},                  {"100"}                 },
+  {{1000},                 {"1000"}                },
+  {{10000},                {"10000"}               },
+  {{100000},               {"100000"}              },
+  {{1000000},              {"1000000"}             },
+  {{10000000},             {"10000000"}            },
+  {{100000000},            {"100000000"}           },
+  {{1000000000},           {"1000000000"}          },
+  {{10000000000},          {"10000000000"}         },
+  {{100000000000},         {"100000000000"}        },
+  {{1000000000000},        {"1000000000000"}       },
+  {{10000000000000},       {"10000000000000"}      },
+  {{100000000000000},      {"100000000000000"}     },
+  {{1000000000000000},     {"1000000000000000"}    },
+  {{10000000000000000},    {"10000000000000000"}   },
+  {{100000000000000000},   {"100000000000000000"}  },
+  {{1000000000000000000},  {"1000000000000000000"} },
+  {{-1},                   "-1"                    },
+  {{-10},                  "-10"                   },
+  {{-100},                 "-100"                  },
+  {{-1000},                "-1000"                 },
+  {{-10000},               "-10000"                },
+  {{-100000},              "-100000"               },
+  {{-1000000},             "-1000000"              },
+  {{-10000000},            "-10000000"             },
+  {{-100000000},           "-100000000"            },
+  {{-1000000000},          "-1000000000"           },
+  {{-10000000000},         "-10000000000"          },
+  {{-100000000000},        "-100000000000"         },
+  {{-1000000000000},       "-1000000000000"        },
+  {{-10000000000000},      "-10000000000000"       },
+  {{-100000000000000},     "-100000000000000"      },
+  {{-1000000000000000},    "-1000000000000000"     },
+  {{-10000000000000000},   "-10000000000000000"    },
+  {{-100000000000000000},  "-100000000000000000"   },
+  {{-1000000000000000000}, "-1000000000000000000"  },
+  {{INT64_MAX},            {"9223372036854775807"} },
+  {{INT64_MIN},            {"-9223372036854775808"}},
+};
+
+TEST_CASE("ink_fast_ltoa", "[libts][ink_fast_ltoa]")
+{
+  printf("ink_string\n");
+  char buffer[21];
+  for (auto const &test : int64_tests) {
+    REQUIRE(ink_atoi64(test.s.data()) == test.n);
+    int length = 0;
+    REQUIRE((length = ink_fast_ltoa(test.n, buffer, sizeof(buffer))) == 
(int)test.s.length());
+    REQUIRE(std::string_view(buffer, length) == test.s);
+  }
+}
+
+//-------------------------------------------------------------------------
+// ink_fast_inta test
+//-------------------------------------------------------------------------
+struct int_item {
+  int n;
+  std::string_view s;
+};
+
+constexpr int_item int_tests[] = {
+  {{0},           {"0"}          },
+  {{1},           {"1"}          },
+  {{10},          {"10"}         },
+  {{100},         {"100"}        },
+  {{1000},        {"1000"}       },
+  {{10000},       {"10000"}      },
+  {{100000},      {"100000"}     },
+  {{1000000},     {"1000000"}    },
+  {{10000000},    {"10000000"}   },
+  {{100000000},   {"100000000"}  },
+  {{1000000000},  {"1000000000"} },
+  {{-1},          {"-1"}         },
+  {{-10},         {"-10"}        },
+  {{-100},        {"-100"}       },
+  {{-1000},       {"-1000"}      },
+  {{-10000},      {"-10000"}     },
+  {{-100000},     {"-100000"}    },
+  {{-1000000},    {"-1000000"}   },
+  {{-10000000},   {"-10000000"}  },
+  {{-100000000},  {"-100000000"} },
+  {{-1000000000}, {"-1000000000"}},
+  {{INT_MAX},     {"2147483647"} },
+  {{INT_MIN},     {"-2147483648"}},
+};
+
+TEST_CASE("ink_fast_inta", "[libts][ink_fast_inta]")
+{
+  char buffer[12];
+  for (auto const &test : int_tests) {
+    REQUIRE(ink_atoi(test.s.data()) == test.n);
+    int length = 0;
+    REQUIRE((length = ink_fast_itoa(test.n, buffer, sizeof(buffer))) == 
(int)test.s.length());
+    REQUIRE(std::string_view(buffer, length) == test.s);
+  }
+}
+
+//-------------------------------------------------------------------------
+// ink_fast_uinta test
+//-------------------------------------------------------------------------
+struct uint_item {
+  unsigned int n;
+  std::string_view s;
+};
+
+constexpr uint_item uint_tests[] = {
+  {{0},          {"0"}         },
+  {{1},          {"1"}         },
+  {{10},         {"10"}        },
+  {{100},        {"100"}       },
+  {{1000},       {"1000"}      },
+  {{10000},      {"10000"}     },
+  {{100000},     {"100000"}    },
+  {{1000000},    {"1000000"}   },
+  {{10000000},   {"10000000"}  },
+  {{100000000},  {"100000000"} },
+  {{1000000000}, {"1000000000"}},
+  {{UINT_MAX},   {"4294967295"}},
+};
+
+TEST_CASE("ink_fast_uinta", "[libts][ink_fast_uinta]")
+{
+  char buffer[12];
+  for (auto const &test : uint_tests) {
+    REQUIRE(ink_atoui(test.s.data()) == test.n);
+    int length = 0;
+    REQUIRE((length = ink_fast_uitoa(test.n, buffer, sizeof(buffer))) == 
(int)test.s.length());
+    REQUIRE(std::string_view(buffer, length) == test.s);
+  }
+}

Reply via email to