]> git.ipfire.org Git - thirdparty/git.git/commitdiff
merge: replace atoi() with strtol_i() for marker size validation
authorUsman Akinyemi <usmanakinyemi202@gmail.com>
Thu, 24 Oct 2024 00:24:57 +0000 (00:24 +0000)
committerTaylor Blau <me@ttaylorr.com>
Thu, 24 Oct 2024 18:03:44 +0000 (14:03 -0400)
Replace atoi() with strtol_i() for parsing conflict-marker-size to
improve error handling. Invalid values, such as those containing letters
now trigger a clear error message.
Update the test to verify invalid input handling.

Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
merge-ll.c
t/t6406-merge-attr.sh

index 8e63071922b41cbd2de9920ea24f2b4365130bd2..62fc625552dcd3a598bd2de80ae93c96ea5c98cf 100644 (file)
@@ -15,6 +15,7 @@
 #include "merge-ll.h"
 #include "quote.h"
 #include "strbuf.h"
+#include "gettext.h"
 
 struct ll_merge_driver;
 
@@ -427,7 +428,10 @@ enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
        git_check_attr(istate, path, check);
        ll_driver_name = check->items[0].value;
        if (check->items[1].value) {
-               marker_size = atoi(check->items[1].value);
+               if (strtol_i(check->items[1].value, 10, &marker_size)) {
+                       marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
+                       warning(_("invalid marker-size '%s', expecting an integer"), check->items[1].value);
+               }
                if (marker_size <= 0)
                        marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
        }
@@ -454,7 +458,10 @@ int ll_merge_marker_size(struct index_state *istate, const char *path)
                check = attr_check_initl("conflict-marker-size", NULL);
        git_check_attr(istate, path, check);
        if (check->items[0].value) {
-               marker_size = atoi(check->items[0].value);
+               if (strtol_i(check->items[0].value, 10, &marker_size)) {
+                       marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
+                       warning(_("invalid marker-size '%s', expecting an integer"), check->items[0].value);
+               }
                if (marker_size <= 0)
                        marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
        }
index 9bf952493471e570644d30e7f21fbc901a33f5f6..2dfc9a873d41c0fc010576eeda14ea6588d8de39 100755 (executable)
@@ -118,6 +118,14 @@ test_expect_success 'retry the merge with longer context' '
        grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
 '
 
+test_expect_success 'invalid conflict-marker-size 3a' '
+       cp .gitattributes .gitattributes.bak &&
+       echo "text conflict-marker-size=3a" >>.gitattributes &&
+       test_when_finished "mv .gitattributes.bak .gitattributes" &&
+       git checkout -m text 2>err &&
+       test_grep "warning: invalid marker-size ${SQ}3a${SQ}, expecting an integer" err
+'
+
 test_expect_success 'custom merge backend' '
 
        echo "* merge=union" >.gitattributes &&