merge: replace atoi() with strtol_i() for marker size validation

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>

Usman Akinyemi committed Oct 24, 2024 at 00:24 UTC e36f009e69b68a82f216f9633f99198421a67f12
2 files changed +17 -2
merge-ll.c
+9 -2
@@ -15,6 +15,7 @@
15 #include "merge-ll.h"
16 #include "quote.h"
17 #include "strbuf.h"
18 +#include "gettext.h"
19
20 struct ll_merge_driver;
21
@@ -427,7 +428,10 @@ enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
428 git_check_attr(istate, path, check);
429 ll_driver_name = check->items[0].value;
430 if (check->items[1].value) {
430 - marker_size = atoi(check->items[1].value);
431 + if (strtol_i(check->items[1].value, 10, &marker_size)) {
432 + marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
433 + warning(_("invalid marker-size '%s', expecting an integer"), check->items[1].value);
434 + }
435 if (marker_size <= 0)
436 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
437 }
@@ -454,7 +458,10 @@ int ll_merge_marker_size(struct index_state *istate, const char *path)
458 check = attr_check_initl("conflict-marker-size", NULL);
459 git_check_attr(istate, path, check);
460 if (check->items[0].value) {
457 - marker_size = atoi(check->items[0].value);
461 + if (strtol_i(check->items[0].value, 10, &marker_size)) {
462 + marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
463 + warning(_("invalid marker-size '%s', expecting an integer"), check->items[0].value);
464 + }
465 if (marker_size <= 0)
466 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
467 }
t/t6406-merge-attr.sh
+8
@@ -118,6 +118,14 @@ test_expect_success 'retry the merge with longer context' '
118 grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
119 '
120
121 +test_expect_success 'invalid conflict-marker-size 3a' '
122 + cp .gitattributes .gitattributes.bak &&
123 + echo "text conflict-marker-size=3a" >>.gitattributes &&
124 + test_when_finished "mv .gitattributes.bak .gitattributes" &&
125 + git checkout -m text 2>err &&
126 + test_grep "warning: invalid marker-size ${SQ}3a${SQ}, expecting an integer" err
127 +'
128 +
129 test_expect_success 'custom merge backend' '
130
131 echo "* merge=union" >.gitattributes &&