From e679925bd0c8608ebe24f34917347ad939c6506d Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Fri, 25 Oct 2024 11:53:22 +0200 Subject: 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. --- src/core.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/core.c') diff --git a/src/core.c b/src/core.c index 270bd9c..759e923 100644 --- a/src/core.c +++ b/src/core.c @@ -367,6 +367,7 @@ void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events) int og_socket_server_init(const char *addr, const char *port) { struct sockaddr_in local; + uint64_t port_num; uint32_t s_addr; int sd, on = 1; @@ -384,7 +385,13 @@ int og_socket_server_init(const char *addr, const char *port) local.sin_addr.s_addr = s_addr; local.sin_family = AF_INET; - local.sin_port = htons(atoi(port)); + + if (safe_strtoull(port, &port_num, 10, UINT16_MAX) < 0) { + syslog(LOG_ERR, "failed to parse port %s (%s:%d)\n", + port, __func__, __LINE__); + return -1; + } + local.sin_port = htons(port_num); if (bind(sd, (struct sockaddr *) &local, sizeof(local)) < 0) { close(sd); -- cgit v1.2.3-18-g5258