diff mbox series

[v2,08/10] object.c: don't go past "len" under die() in type_from_string_gently()

Message ID patch-08.10-f652d0fb5c-20210420T124428Z-avarab@gmail.com (mailing list archive)
State New, archived
Headers show
Series object.c et al: tests, small bug fixes etc. | expand

Commit Message

Ævar Arnfjörð Bjarmason April 20, 2021, 12:50 p.m. UTC
Fix a bug that's been with us ever since type_from_string_gently() was
split off from type_from_string() in fe8e3b71805 (Refactor
type_from_string() to allow continuing after detecting an error,
2014-09-10).

When the type was invalid and we were in the non-gently mode we'd die,
and then proceed to run off past the "len" of the buffer we were
provided with.

Luckily, I think that nothing ever used this function in that way. Any
non-gentle invocation came via type_from_string(), which was passing a
buffer with a NIL at the same place as the "len" would take us (we got
it via strlen()).

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 object.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Junio C Hamano April 29, 2021, 4:55 a.m. UTC | #1
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> Fix a bug that's been with us ever since type_from_string_gently() was
> split off from type_from_string() in fe8e3b71805 (Refactor
> type_from_string() to allow continuing after detecting an error,
> 2014-09-10).
>
> When the type was invalid and we were in the non-gently mode we'd die,
> and then proceed to run off past the "len" of the buffer we were
> provided with.
>
> Luckily, I think that nothing ever used this function in that way. Any
> non-gentle invocation came via type_from_string(), which was passing a
> buffer with a NIL at the same place as the "len" would take us (we got
> it via strlen()).

NIL???


>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  object.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/object.c b/object.c
> index 70af833ca1..bad9e17f25 100644
> --- a/object.c
> +++ b/object.c
> @@ -50,7 +50,7 @@ int type_from_string_gently(const char *str, ssize_t len, int gentle)
>  	if (gentle)
>  		return -1;
>  
> -	die(_("invalid object type \"%s\""), str);
> +	die(_("invalid object type \"%.*s\""), (int)len, str);
>  }

This makes total sense.  This is one of the reasons why I hate to
review your topics---many patches in them seem unwarranted churn,
but there are clear gems like this commit buried in late steps in
them so I need to read through them to find these anyway :-)
diff mbox series

Patch

diff --git a/object.c b/object.c
index 70af833ca1..bad9e17f25 100644
--- a/object.c
+++ b/object.c
@@ -50,7 +50,7 @@  int type_from_string_gently(const char *str, ssize_t len, int gentle)
 	if (gentle)
 		return -1;
 
-	die(_("invalid object type \"%s\""), str);
+	die(_("invalid object type \"%.*s\""), (int)len, str);
 }
 
 /*