Skip to content

Commit cc40234

Browse files
Unique-Usmanttaylorr
authored andcommitted
daemon: replace atoi() with strtoul_ui() and strtol_i()
Replace atoi() with strtoul_ui() for --timeout and --init-timeout (non-negative integers) and with strtol_i() for --max-connections (signed integers). This improves error handling and input validation by detecting invalid values and providing clear error messages. Update tests to ensure these arguments are properly validated. Signed-off-by: Usman Akinyemi <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 34b6ce9 commit cc40234

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

daemon.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "abspath.h"
55
#include "config.h"
66
#include "environment.h"
7+
#include "gettext.h"
78
#include "path.h"
89
#include "pkt-line.h"
910
#include "protocol.h"
@@ -1308,17 +1309,20 @@ int cmd_main(int argc, const char **argv)
13081309
continue;
13091310
}
13101311
if (skip_prefix(arg, "--timeout=", &v)) {
1311-
timeout = atoi(v);
1312+
if (strtoul_ui(v, 10, &timeout))
1313+
die(_("invalid timeout '%s', expecting a non-negative integer"), v);
13121314
continue;
13131315
}
13141316
if (skip_prefix(arg, "--init-timeout=", &v)) {
1315-
init_timeout = atoi(v);
1317+
if (strtoul_ui(v, 10, &init_timeout))
1318+
die(_("invalid init-timeout '%s', expecting a non-negative integer"), v);
13161319
continue;
13171320
}
13181321
if (skip_prefix(arg, "--max-connections=", &v)) {
1319-
max_connections = atoi(v);
1322+
if (strtol_i(v, 10, &max_connections))
1323+
die(_("invalid max-connections '%s', expecting an integer"), v);
13201324
if (max_connections < 0)
1321-
max_connections = 0; /* unlimited */
1325+
max_connections = 0; /* unlimited */
13221326
continue;
13231327
}
13241328
if (!strcmp(arg, "--strict-paths")) {

t/t5570-git-daemon.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,31 @@ TEST_PASSES_SANITIZE_LEAK=true
88
. ./test-lib.sh
99

1010
. "$TEST_DIRECTORY"/lib-git-daemon.sh
11+
12+
test_expect_success 'daemon rejects invalid --init-timeout values' '
13+
for arg in "3a" "-3"
14+
do
15+
test_must_fail git daemon --init-timeout="$arg" 2>err &&
16+
test_grep "fatal: invalid init-timeout ${SQ}$arg${SQ}, expecting a non-negative integer" err ||
17+
return 1
18+
done
19+
'
20+
21+
test_expect_success 'daemon rejects invalid --timeout values' '
22+
for arg in "3a" "-3"
23+
do
24+
test_must_fail git daemon --timeout="$arg" 2>err &&
25+
test_grep "fatal: invalid timeout ${SQ}$arg${SQ}, expecting a non-negative integer" err ||
26+
return 1
27+
done
28+
'
29+
30+
test_expect_success 'daemon rejects invalid --max-connections values' '
31+
arg='3a' &&
32+
test_must_fail git daemon --max-connections=3a 2>err &&
33+
test_grep "fatal: invalid max-connections ${SQ}$arg${SQ}, expecting an integer" err
34+
'
35+
1136
start_git_daemon
1237

1338
check_verbose_connect () {

0 commit comments

Comments
 (0)