]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
utf8: Handle `NULL` inputs gracefully.
authorFlorian Forster <octo@collectd.org>
Fri, 22 Dec 2023 14:44:56 +0000 (15:44 +0100)
committerFlorian Forster <octo@collectd.org>
Thu, 28 Dec 2023 08:53:22 +0000 (09:53 +0100)
src/utils/utf8/utf8.c
src/utils/utf8/utf8_test.c

index e00770cc074cdf0b8717cee9ceacbcf505ca3a36..9530ad5b9fcd074318aeac668188b0fa90e4bb6d 100644 (file)
@@ -52,6 +52,10 @@ static uint32_t decode(uint32_t state, uint32_t byte) {
 }
 
 bool utf8_valid(char const *s) {
+  if (s == NULL) {
+    return false;
+  }
+
   uint32_t state = 0;
   for (size_t i = 0; s[i] != 0; i++) {
     state = decode(state, (uint8_t)s[i]);
index dc9197ddd92d89c59fe05c75e724185d94d02533..c90705c0f817d5890e62b2a87a47ec36dcb00862 100644 (file)
@@ -41,6 +41,11 @@ DEF_TEST(utf8_valid) {
           .input = "",
           .want = true,
       },
+      {
+          .name = "null string",
+          .input = NULL,
+          .want = false,
+      },
       {
           .name = "The greek work \"kosme\"",
           .input = (char[]){0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce,