Skip to content

Commit e36f009

Browse files
Unique-Usmanttaylorr
authored andcommitted
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 <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent cc40234 commit e36f009

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

merge-ll.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "merge-ll.h"
1616
#include "quote.h"
1717
#include "strbuf.h"
18+
#include "gettext.h"
1819

1920
struct ll_merge_driver;
2021

@@ -427,7 +428,10 @@ enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
427428
git_check_attr(istate, path, check);
428429
ll_driver_name = check->items[0].value;
429430
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+
}
431435
if (marker_size <= 0)
432436
marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
433437
}
@@ -454,7 +458,10 @@ int ll_merge_marker_size(struct index_state *istate, const char *path)
454458
check = attr_check_initl("conflict-marker-size", NULL);
455459
git_check_attr(istate, path, check);
456460
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+
}
458465
if (marker_size <= 0)
459466
marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
460467
}

t/t6406-merge-attr.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ test_expect_success 'retry the merge with longer context' '
118118
grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
119119
'
120120

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+
121129
test_expect_success 'custom merge backend' '
122130
123131
echo "* merge=union" >.gitattributes &&

0 commit comments

Comments
 (0)