sbi: prevent integer overflow in URL port parsing
Some checks are pending
CIFuzz / Fuzzing (push) Waiting to run
Meson Continuous Integration / Build and Test on MacOS Latest (push) Waiting to run
Meson Continuous Integration / Build and Test on Ubuntu Latest (push) Waiting to run

Replace natoi() and atoi() with a shared checked port parser.
Reject empty, non-numeric, and out-of-range ports before accumulation,
while preserving port 0 and leading zeros.

Issues: #4867
This commit is contained in:
Sukchan Lee 2026-10-14 09:32:19 +09:00
parent a075524106
commit f770382184

View file

@ -25,18 +25,30 @@
#include "yuarel.h"
/**
* Parse a non null terminated string into an integer.
* Parse a null terminated port string.
*
* str: the string containing the number.
* len: Number of characters to parse.
* Returns the port on success, otherwise -1.
*/
static inline int
natoi(const char *str, size_t len)
parse_port(const char *str)
{
int i, r = 0;
for (i = 0; i < len; i++) {
r *= 10;
r += str[i] - '0';
int r = 0;
if ('\0' == *str) {
return -1;
}
while (*str) {
int digit;
if (*str < '0' || *str > '9') {
return -1;
}
digit = *str++ - '0';
if (r > (65535 - digit) / 10) {
return -1;
}
r = r * 10 + digit;
}
return r;
@ -217,15 +229,10 @@ yuarel_parse(struct yuarel *url, char *u)
}
if (NULL != u && (NULL == url->path || u < url->path)) {
*(u++) = '\0';
if ('\0' == *u) {
url->port = parse_port(u);
if (url->port < 0) {
return -1;
}
if (url->path) {
url->port = natoi(u, url->path - u - 1);
} else {
url->port = atoi(u);
}
}
/* Missing hostname? */