diff mbox series

[v5,2/3] merge: replace atoi() with strtol_i() for marker size validation

Message ID da9ea10e4e115777c16b32f1cde96a3df8c5cbb1.1729669221.git.gitgitgadget@gmail.com (mailing list archive)
State Superseded
Headers show
Series parse: replace atoi() with strtoul_ui() and strtol_i() | expand

Commit Message

Usman Akinyemi Oct. 23, 2024, 7:40 a.m. UTC
From: Usman Akinyemi <usmanakinyemi202@gmail.com>

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>
---
 merge-ll.c            | 11 +++++++++--
 t/t6406-merge-attr.sh |  6 ++++++
 2 files changed, 15 insertions(+), 2 deletions(-)

Comments

Taylor Blau Oct. 23, 2024, 8:32 p.m. UTC | #1
On Wed, Oct 23, 2024 at 07:40:19AM +0000, Usman Akinyemi via GitGitGadget wrote:
> diff --git a/t/t6406-merge-attr.sh b/t/t6406-merge-attr.sh
> index 9bf95249347..c2a9cf03808 100755
> --- a/t/t6406-merge-attr.sh
> +++ b/t/t6406-merge-attr.sh
> @@ -118,6 +118,12 @@ test_expect_success 'retry the merge with longer context' '
>  	grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
>  '
>
> +test_expect_success 'invalid conflict-marker-size 3a' '
> +    echo "text conflict-marker-size=3a" >>.gitattributes &&
> +    git checkout -m text 2>error &&
> +    test_grep "warning: invalid marker-size ${SQ}3a${SQ}, expecting an integer" error
> +'
> +

Do subsequent tests further down in this script depend on .gitattributes
not having invalid lines? If so, you may want to instead write:

    cp .gitattributes .gitattributes.bak &&
    echo "text conflict-marker-size=3a" >>.gitattributes &&
    test_when_finished "mv .gitattributes.bak .gitattributes" &&

instead.

Thanks,
Taylor
Usman Akinyemi Oct. 24, 2024, 12:23 a.m. UTC | #2
On Wed, Oct 23, 2024 at 8:32 PM Taylor Blau <me@ttaylorr.com> wrote:
>
> On Wed, Oct 23, 2024 at 07:40:19AM +0000, Usman Akinyemi via GitGitGadget wrote:
> > diff --git a/t/t6406-merge-attr.sh b/t/t6406-merge-attr.sh
> > index 9bf95249347..c2a9cf03808 100755
> > --- a/t/t6406-merge-attr.sh
> > +++ b/t/t6406-merge-attr.sh
> > @@ -118,6 +118,12 @@ test_expect_success 'retry the merge with longer context' '
> >       grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
> >  '
> >
> > +test_expect_success 'invalid conflict-marker-size 3a' '
> > +    echo "text conflict-marker-size=3a" >>.gitattributes &&
> > +    git checkout -m text 2>error &&
> > +    test_grep "warning: invalid marker-size ${SQ}3a${SQ}, expecting an integer" error
> > +'
> > +
>
> Do subsequent tests further down in this script depend on .gitattributes
> not having invalid lines? If so, you may want to instead write:
>
>     cp .gitattributes .gitattributes.bak &&
>     echo "text conflict-marker-size=3a" >>.gitattributes &&
>     test_when_finished "mv .gitattributes.bak .gitattributes" &&
>
> instead.
Thanks for the review.
Usman
>
> Thanks,
> Taylor
diff mbox series

Patch

diff --git a/merge-ll.c b/merge-ll.c
index 8e63071922b..62fc625552d 100644
--- a/merge-ll.c
+++ b/merge-ll.c
@@ -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;
 	}
diff --git a/t/t6406-merge-attr.sh b/t/t6406-merge-attr.sh
index 9bf95249347..c2a9cf03808 100755
--- a/t/t6406-merge-attr.sh
+++ b/t/t6406-merge-attr.sh
@@ -118,6 +118,12 @@  test_expect_success 'retry the merge with longer context' '
 	grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
 '
 
+test_expect_success 'invalid conflict-marker-size 3a' '
+    echo "text conflict-marker-size=3a" >>.gitattributes &&
+    git checkout -m text 2>error &&
+    test_grep "warning: invalid marker-size ${SQ}3a${SQ}, expecting an integer" error
+'
+
 test_expect_success 'custom merge backend' '
 
 	echo "* merge=union" >.gitattributes &&