From 4f8ae5bb70bf2ccfc7b0a9b3cd4543649fd68c71 Mon Sep 17 00:00:00 2001 From: TapTap Date: Fri, 17 Jul 2026 10:07:18 +0200 Subject: [PATCH] fix: bw_tokens initialized to io_bwlimit, overflow check for --bwlimit --- src/client/client_cli.c | 14 +++++++++++--- src/shared/protocol.c | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/client/client_cli.c b/src/client/client_cli.c index b7d4495..44fca4b 100644 --- a/src/client/client_cli.c +++ b/src/client/client_cli.c @@ -3,6 +3,8 @@ #include "log.h" #include "protocol.h" #include "utils.h" +#include +#include #include #include #include @@ -130,9 +132,15 @@ int main(int argc, char *argv[]) { } else if (strcmp(argv[i], "--server-port") == 0 && i + 1 < argc) { server_port = atoi(argv[++i]); } else if (strcmp(argv[i], "--bwlimit") == 0 && i + 1 < argc) { - unsigned long long kbps = strtoull(argv[++i], NULL, 10); - if (kbps == 0) { - fprintf(stderr, "Error: --bwlimit must be greater than 0\n"); + char *end; + errno = 0; + unsigned long long kbps = strtoull(argv[++i], &end, 10); + if (errno != 0 || *end != '\0' || kbps == 0) { + fprintf(stderr, "Error: --bwlimit must be a positive integer\n"); + return 1; + } + if (kbps > ULLONG_MAX / 1024) { + fprintf(stderr, "Error: --bwlimit value too large\n"); return 1; } io_set_bwlimit(kbps * 1024); diff --git a/src/shared/protocol.c b/src/shared/protocol.c index 09927e9..4f2b82a 100644 --- a/src/shared/protocol.c +++ b/src/shared/protocol.c @@ -21,7 +21,7 @@ void io_set_fds(int read_fd, int write_fd) { void io_set_bwlimit(unsigned long long bytes_per_sec) { io_bwlimit = bytes_per_sec; - bw_tokens = 0; + bw_tokens = (long long)io_bwlimit; clock_gettime(CLOCK_MONOTONIC, &bw_last_refill); }