Skip to content

Commit e292b82

Browse files
committed
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]>
1 parent 90fe380 commit e292b82

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

daemon.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,17 +1308,20 @@ int cmd_main(int argc, const char **argv)
13081308
continue;
13091309
}
13101310
if (skip_prefix(arg, "--timeout=", &v)) {
1311-
timeout = atoi(v);
1311+
if (strtoul_ui(v, 10, &timeout))
1312+
die("invalid timeout '%s', expecting a non-negative integer", v);
13121313
continue;
13131314
}
13141315
if (skip_prefix(arg, "--init-timeout=", &v)) {
1315-
init_timeout = atoi(v);
1316+
if (strtoul_ui(v, 10, &init_timeout))
1317+
die("invalid init-timeout '%s', expecting a non-negative integer", v);
13161318
continue;
13171319
}
13181320
if (skip_prefix(arg, "--max-connections=", &v)) {
1319-
max_connections = atoi(v);
1321+
if (strtol_i(v, 10, &max_connections))
1322+
die("invalid max-connections '%s', expecting an integer", v);
13201323
if (max_connections < 0)
1321-
max_connections = 0; /* unlimited */
1324+
max_connections = 0; /* unlimited */
13221325
continue;
13231326
}
13241327
if (!strcmp(arg, "--strict-paths")) {

t/t5570-git-daemon.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@ 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>actual_error &&
16+
test_write_lines "fatal: invalid init-timeout ${SQ}$arg${SQ}, expecting a non-negative integer" >expected &&
17+
test_cmp actual_error expected || 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>actual_error &&
25+
test_write_lines "fatal: invalid timeout ${SQ}$arg${SQ}, expecting a non-negative integer" >expected &&
26+
test_cmp actual_error expected || 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>actual_error &&
33+
test_write_lines "fatal: invalid max-connections ${SQ}$arg${SQ}, expecting an integer" >expected &&
34+
test_cmp actual_error expected
35+
'
36+
1137
start_git_daemon
1238

1339
check_verbose_connect () {

0 commit comments

Comments
 (0)