]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
util: fix UTI_BytesToHex() to handle zero-length input
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 30 Sep 2020 12:07:04 +0000 (14:07 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 1 Oct 2020 10:58:17 +0000 (12:58 +0200)
test/unit/util.c
util.c

index e597d97af4aba835fae6af37c38dc26455bf05b0..052eb316db2f80a2c534887fb5cf0386957741d9 100644 (file)
@@ -330,9 +330,12 @@ void test_unit(void) {
   TEST_CHECK(UTI_HexToBytes(buf, buf, sizeof (buf)) == 7);
   TEST_CHECK(memcmp(buf, "\xAB\x12\x34\x56\x78\x00\x01", 7) == 0);
 
+  TEST_CHECK(UTI_BytesToHex("", 0, buf, 0) == 0);
   TEST_CHECK(UTI_BytesToHex("\xAB\x12\x34\x56\x78\x00\x01", 7, buf, 14) == 0);
   TEST_CHECK(UTI_BytesToHex("\xAB\x12\x34\x56\x78\x00\x01", 7, buf, 15) == 1);
   TEST_CHECK(strcmp(buf, "AB123456780001") == 0);
+  TEST_CHECK(UTI_BytesToHex("\xAB\x12\x34\x56\x78\x00\x01", 0, buf, 15) == 1);
+  TEST_CHECK(strcmp(buf, "") == 0);
 
   TEST_CHECK(snprintf(buf, sizeof (buf), "%s", "") < sizeof (buf));
   TEST_CHECK(UTI_SplitString(buf, words, 3) == 0);
diff --git a/util.c b/util.c
index 5831ec4475b0c86f97f5d675e9c1182fdbfbaf15..673ca0694d0224a756df8b3af6fdf0693b235a75 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1417,6 +1417,11 @@ UTI_BytesToHex(const void *buf, unsigned int buf_len, char *hex, unsigned int he
 {
   unsigned int i, l;
 
+  if (hex_len < 1)
+    return 0;
+
+  hex[0] = '\0';
+
   for (i = l = 0; i < buf_len; i++, l += 2) {
     if (l + 2 >= hex_len ||
         snprintf(hex + l, hex_len - l, "%02hhX", ((const char *)buf)[i]) != 2)