From a6229ae0bbeb8fcb7f9ee5d1dddaae98c0c71176 Mon Sep 17 00:00:00 2001 From: Corubba Smith Date: Wed, 12 Mar 2025 15:55:16 +0100 Subject: ulogd: improve integer option parsing The `value` union member in `struct config_entry` is declared as `int` since basically the beginning in e07722e46001 ("config stuff added"). The parsing was switched from the original `atoi()` in 015849995f7f ("Fix hexadecimal parsing in config file") to `strtoul()`. Switch the function for parsing to the signed `strtol()` variant since the result will be stored in a signed int, and it makes sense to support negative numbers. Detect when `strtol()` does not properly consume the whole argument and return a new format error. Also check the numerical value to make sure the signed int does not overflow, in which case a new range error is returned. Unfortunately there is no `strtoi()` which would do the proper range check itself, so the intermediate `long` and range-check for `int` is required. I also considered changing the `value` union member from `int` to `long`, which would make it possible to use the parsed value as-is. But since this is part of the api towards plugins (including third party) such a potentially breaking change felt unwarranted. This also means that still only 16bit integer values are *guaranteed* to work, although most platforms use bigger widths for int. Signed-off-by: Corubba Smith Signed-off-by: Florian Westphal --- include/ulogd/conffile.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include') diff --git a/include/ulogd/conffile.h b/include/ulogd/conffile.h index 1f3d563..fb54dea 100644 --- a/include/ulogd/conffile.h +++ b/include/ulogd/conffile.h @@ -19,6 +19,8 @@ enum { ERRUNKN, /* unknown config key */ ERRSECTION, /* section not found */ ERRTOOLONG, /* string too long */ + ERRINTFORMAT, /* integer format is invalid */ + ERRINTRANGE, /* integer value is out of range */ }; /* maximum line length of config file entries */ -- cgit v1.2.3