summaryrefslogtreecommitdiffstats
path: root/src/client.c
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-10-25 11:53:22 +0200
committerAlejandro Sirgo Rica <asirgo@soleta.eu>2024-10-25 15:09:31 +0200
commite679925bd0c8608ebe24f34917347ad939c6506d (patch)
tree9076d0ff5d251bc0a0051589796b9f81cb86b11c /src/client.c
parente960063a137c8fe760a40a73ccd081e457b23952 (diff)
src: add safe_strtoull for safe string to number conversion
Add safe_strtoull to validate the execution of strtoull. Definining the base of the number is required becase partition codes are base 16 but they lack the 0x prefix. Replace uses of atoi and strtoull/strtoul and log the conversion errors.
Diffstat (limited to 'src/client.c')
-rw-r--r--src/client.c22
1 files changed, 19 insertions, 3 deletions
diff --git a/src/client.c b/src/client.c
index bc66b4b..d1288c4 100644
--- a/src/client.c
+++ b/src/client.c
@@ -14,6 +14,7 @@
#include "list.h"
#include "rest.h"
#include "json.h"
+#include <stdint.h>
#include <syslog.h>
#include <sys/ioctl.h>
#include <ifaddrs.h>
@@ -557,6 +558,7 @@ static bool og_update_client_disk_info(struct og_dbi *dbi,
int disk_part_len = 0;
char disk_part[1024];
const char *msglog;
+ uint64_t part_code;
int i;
if (serial_number && strlen(serial_number) > 0) {
@@ -617,7 +619,12 @@ static bool og_update_client_disk_info(struct og_dbi *dbi,
continue;
}
- reported_disk.size = strtoull(disks[i].size, NULL, 0);
+ if (safe_strtoull(disks[i].size, &reported_disk.size, 10, UINT64_MAX) < 0) {
+ syslog(LOG_ERR, "failed to parse disk size for disk %d (%s:%d)\n",
+ i + 1, __func__, __LINE__);
+ return false;
+ }
+
cur_disk.size = dbi_result_get_longlong(result, "tamano");
dbi_result_free(result);
@@ -661,8 +668,17 @@ static bool og_update_client_disk_info(struct og_dbi *dbi,
return false;
}
- reported_part.size = strtoull(partitions[i].size, NULL, 0);
- reported_part.code = strtoul(partitions[i].code, NULL, 16);
+ if (safe_strtoull(partitions[i].size, &reported_part.size, 10, UINT64_MAX) < 0) {
+ syslog(LOG_ERR, "failed to parse partition size %s for partition %d (%s:%d)\n",
+ partitions[i].size, i + 1, __func__, __LINE__);
+ return false;
+ }
+ if (safe_strtoull(partitions[i].code, &part_code, 32, UINT32_MAX) < 0) {
+ syslog(LOG_ERR, "failed to parse partition code %s for partition %d (%s:%d)\n",
+ partitions[i].code, i + 1, __func__, __LINE__);
+ return false;
+ }
+ reported_part.code = part_code;
reported_part.filesystem = get_filesystem_id(partitions[i].filesystem);
reported_part.used_size = partitions[i].used_size;
reported_part.free_size = partitions[i].free_size;