]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Write MongoDB plugin: fix format trunction errors. 4158/head
authorFlorian Forster <octo@collectd.org>
Fri, 24 Nov 2023 08:15:04 +0000 (09:15 +0100)
committerFlorian Forster <octo@collectd.org>
Fri, 24 Nov 2023 08:19:34 +0000 (09:19 +0100)
When a `size_t` is a 64bit integer, the 16 byte buffer could potentially
overflow. Fix this by using a larger buffer and also collectd's own
`ssnprintf()` function which guarantees to result in a null terminated
string.

```
In function 'wm_create_bson',
    inlined from 'wm_write' at src/write_mongodb.c:229:17:
src/write_mongodb.c:99:32: error: '%zu' directive output may be truncated writing between 1 and 20 bytes into a region of size 16 [-Werror=format-truncation=]
   99 |     snprintf(key, sizeof(key), "%" PRIsz, i);
      |                                ^~~
src/write_mongodb.c: In function 'wm_write':
src/write_mongodb.c:99:33: note: format string is defined here
   99 |     snprintf(key, sizeof(key), "%" PRIsz, i);
```

src/write_mongodb.c

index b43906e6326c949b12ec46ac4561e3278c621f3f..7a3bedc9e29c76c765ec96213ce1bc2cc1d58945 100644 (file)
@@ -94,9 +94,9 @@ static bson_t *wm_create_bson(const data_set_t *ds, /* {{{ */
 
   BSON_APPEND_ARRAY_BEGIN(ret, "values", &subarray); /* {{{ */
   for (size_t i = 0; i < ds->ds_num; i++) {
-    char key[16];
+    char key[32] = {0};
 
-    snprintf(key, sizeof(key), "%" PRIsz, i);
+    ssnprintf(key, sizeof(key), "%" PRIsz, i);
 
     if (ds->ds[i].type == DS_TYPE_GAUGE)
       BSON_APPEND_DOUBLE(&subarray, key, vl->values[i].gauge);
@@ -119,9 +119,9 @@ static bson_t *wm_create_bson(const data_set_t *ds, /* {{{ */
 
   BSON_APPEND_ARRAY_BEGIN(ret, "dstypes", &subarray); /* {{{ */
   for (size_t i = 0; i < ds->ds_num; i++) {
-    char key[16];
+    char key[32] = {0};
 
-    snprintf(key, sizeof(key), "%" PRIsz, i);
+    ssnprintf(key, sizeof(key), "%" PRIsz, i);
 
     if (store_rates)
       BSON_APPEND_UTF8(&subarray, key, "gauge");
@@ -132,9 +132,9 @@ static bson_t *wm_create_bson(const data_set_t *ds, /* {{{ */
 
   BSON_APPEND_ARRAY_BEGIN(ret, "dsnames", &subarray); /* {{{ */
   for (size_t i = 0; i < ds->ds_num; i++) {
-    char key[16];
+    char key[32] = {0};
 
-    snprintf(key, sizeof(key), "%" PRIsz, i);
+    ssnprintf(key, sizeof(key), "%" PRIsz, i);
     BSON_APPEND_UTF8(&subarray, key, ds->ds[i].name);
   }
   bson_append_array_end(ret, &subarray); /* }}} dsnames */