]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
common: Fix compiler warning in unit test.
authorFlorian Forster <octo@collectd.org>
Thu, 21 Dec 2023 11:05:45 +0000 (12:05 +0100)
committerFlorian Forster <octo@collectd.org>
Thu, 21 Dec 2023 11:05:48 +0000 (12:05 +0100)
Some compilers complained about using a value that depends on the input for the
size argument passed to `strncpy`. They missed that the size of `buf` also
depends on the source argument and therefore this use is fine.

```
  CC       src/utils/common/common_test.o
../../src/utils/common/common_test.c: In function 'test_parse_values':
../../src/utils/common/common_test.c:316:5: error: 'strncpy' specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
  316 |     strncpy(buf, cases[i].buffer, buf_sz);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../src/utils/common/common_test.c:314:21: note: length computed here
  314 |     size_t buf_sz = strlen(cases[i].buffer) + 1;
      |                     ^~~~~~~~~~~~~~~~~~~~~~~
```

src/utils/common/common_test.c

index 7da7c0138d9132ea69e41cbdf51c9db3faba7b28..7a1d5393132458094dd5f708ae23d2aeac58e4f6 100644 (file)
@@ -313,7 +313,7 @@ DEF_TEST(parse_values) {
 
     size_t buf_sz = strlen(cases[i].buffer) + 1;
     char buf[buf_sz];
-    strncpy(buf, cases[i].buffer, buf_sz);
+    strncpy(buf, cases[i].buffer, sizeof(buf));
 
     int status = parse_values(cases[i].buffer, &vl, &ds);
     EXPECT_EQ_INT(cases[i].status, status);