fix: bw_tokens initialized to io_bwlimit, overflow check for --bwlimit

This commit is contained in:
2026-07-17 10:07:18 +02:00
parent 115e492a54
commit ecd486a8d0
2 changed files with 12 additions and 4 deletions
+11 -3
View File
@@ -3,6 +3,8 @@
#include "log.h"
#include "protocol.h"
#include "utils.h"
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -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);
+1 -1
View File
@@ -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);
}