]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
src: fix memory leak in config parser in write_syslog.c 4361/head
authorAntonMoryakov <ant.v.moryakov@gmail.com>
Wed, 14 May 2025 16:11:04 +0000 (19:11 +0300)
committerAntonMoryakov <ant.v.moryakov@gmail.com>
Wed, 14 May 2025 16:11:12 +0000 (19:11 +0300)
Static analyzer detected (write_syslog.c:562):
- Memory allocated for `cb` via `calloc()` was lost when returning -1
  due to invalid config options (before line 594)

Root cause:
- The function `ws_config_tsd()` could return early on config errors
  without freeing `cb`, while successful path used `user_data.free_func`
  for cleanup.

Fix:
- Added explicit cleanup with `ws_callback_free(cb)` before all error returns
- Only one error return path needed fixing (invalid option case)
- Success path still uses automatic cleanup via `user_data.free_func`

Impact:
- Fixes memory leak that could occur on every invalid configuration
- No behavioral changes for valid configurations
- Maintains existing cleanup strategy

Triggers found by static analyzer Svace.

Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
src/write_syslog.c

index 9b35ceceb3e5ef9a9c4083a99b5c146d609b1ccd..51c7184a0c900fc04a434c3b0371177d93cb2c4a 100644 (file)
@@ -591,6 +591,7 @@ static int ws_config_tsd(oconfig_item_t *ci) {
       ERROR("write_syslog plugin: Invalid configuration "
             "option: %s.",
             child->key);
+      ws_callback_free(cb);
       return -1;
     }
   }