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 <usmanakinyemi202@gmail.com> Signed-off-by: Taylor Blau <me@ttaylorr.com>

Usman Akinyemi committed Oct 24, 2024 at 00:24 UTC cc4023477f3c958b77c64b7f36f91b4f9d7c5a58
2 files changed +33 -4
daemon.c
+8 -4
@@ -4,6 +4,7 @@
4 #include "abspath.h"
5 #include "config.h"
6 #include "environment.h"
7 +#include "gettext.h"
8 #include "path.h"
9 #include "pkt-line.h"
10 #include "protocol.h"
@@ -1308,17 +1309,20 @@ int cmd_main(int argc, const char **argv)
1309 continue;
1310 }
1311 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);
1314 continue;
1315 }
1316 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);
1319 continue;
1320 }
1321 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);
1324 if (max_connections < 0)
1321 - max_connections = 0; /* unlimited */
1325 + max_connections = 0; /* unlimited */
1326 continue;
1327 }
1328 if (!strcmp(arg, "--strict-paths")) {
t/t5570-git-daemon.sh
+25
@@ -8,6 +8,31 @@ TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
9
10 . "$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 +
36 start_git_daemon
37
38 check_verbose_connect () {