]> git.ipfire.org Git - thirdparty/git.git/commitdiff
daemon: replace atoi() with strtoul_ui() and strtol_i()
authorUsman Akinyemi <usmanakinyemi202@gmail.com>
Thu, 24 Oct 2024 00:24:56 +0000 (00:24 +0000)
committerTaylor Blau <me@ttaylorr.com>
Thu, 24 Oct 2024 18:03:43 +0000 (14:03 -0400)
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>
daemon.c
t/t5570-git-daemon.sh

index cb946e3c95f16d51b99614b9d8bdb88fcddfaf3f..a40e435c6370cf1cfd2cef082184445f31748643 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -4,6 +4,7 @@
 #include "abspath.h"
 #include "config.h"
 #include "environment.h"
+#include "gettext.h"
 #include "path.h"
 #include "pkt-line.h"
 #include "protocol.h"
@@ -1308,17 +1309,20 @@ int cmd_main(int argc, const char **argv)
                        continue;
                }
                if (skip_prefix(arg, "--timeout=", &v)) {
-                       timeout = atoi(v);
+                       if (strtoul_ui(v, 10, &timeout))
+                               die(_("invalid timeout '%s', expecting a non-negative integer"), v);
                        continue;
                }
                if (skip_prefix(arg, "--init-timeout=", &v)) {
-                       init_timeout = atoi(v);
+                       if (strtoul_ui(v, 10, &init_timeout))
+                               die(_("invalid init-timeout '%s', expecting a non-negative integer"), v);
                        continue;
                }
                if (skip_prefix(arg, "--max-connections=", &v)) {
-                       max_connections = atoi(v);
+                       if (strtol_i(v, 10, &max_connections))
+                               die(_("invalid max-connections '%s', expecting an integer"), v);
                        if (max_connections < 0)
-                               max_connections = 0;            /* unlimited */
+                               max_connections = 0;  /* unlimited */
                        continue;
                }
                if (!strcmp(arg, "--strict-paths")) {
index c5f08b67996b0dd7057b0b12a0d006839038ab4e..e3df7d864109a0e0e64cc0b30fba7c9737a1fb9d 100755 (executable)
@@ -8,6 +8,31 @@ TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 . "$TEST_DIRECTORY"/lib-git-daemon.sh
+
+test_expect_success 'daemon rejects invalid --init-timeout values' '
+       for arg in "3a" "-3"
+       do
+               test_must_fail git daemon --init-timeout="$arg" 2>err &&
+               test_grep "fatal: invalid init-timeout ${SQ}$arg${SQ}, expecting a non-negative integer" err ||
+               return 1
+       done
+'
+
+test_expect_success 'daemon rejects invalid --timeout values' '
+       for arg in "3a" "-3"
+       do
+               test_must_fail git daemon --timeout="$arg" 2>err &&
+               test_grep "fatal: invalid timeout ${SQ}$arg${SQ}, expecting a non-negative integer" err ||
+               return 1
+       done
+'
+
+test_expect_success 'daemon rejects invalid --max-connections values' '
+       arg='3a' &&
+       test_must_fail git daemon --max-connections=3a 2>err &&
+       test_grep "fatal: invalid max-connections ${SQ}$arg${SQ}, expecting an integer" err
+'
+
 start_git_daemon
 
 check_verbose_connect () {