diff mbox series

[1/6] object.c: stop supporting len == -1 in type_from_string_gently()

Message ID patch-1.6-820f3aed21-20210409T082935Z-avarab@gmail.com (mailing list archive)
State Superseded
Headers show
Series {tag,object}*.c: refactorings + prep for a larger change | expand

Commit Message

Ævar Arnfjörð Bjarmason April 9, 2021, 8:32 a.m. UTC
Change the type_from_string() macro into a function and drop the
support for passing len < 0.

Support for len < 0 was added in fe8e3b71805 (Refactor
type_from_string() to allow continuing after detecting an error,
2014-09-10), but no callers use that form. Let's drop it to simplify
this, and in preparation for simplifying these even further.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 object.c | 10 +++++++---
 object.h |  2 +-
 2 files changed, 8 insertions(+), 4 deletions(-)

Comments

Jeff King April 9, 2021, 6:06 p.m. UTC | #1
On Fri, Apr 09, 2021 at 10:32:49AM +0200, Ævar Arnfjörð Bjarmason wrote:

> diff --git a/object.c b/object.c
> index 7fdca3ed1e..88de01e5ac 100644
> --- a/object.c
> +++ b/object.c
> @@ -39,9 +39,6 @@ int type_from_string_gently(const char *str, ssize_t len, int gentle)
>  {
>  	int i;
>  
> -	if (len < 0)
> -		len = strlen(str);
> -

The "ssize_t len" in the parameters could become a size_t now, right?

Not strictly necessary, but in theory it may help static analysis catch
a caller who mistakenly tries to pass -1 (though in practice I suspect
it does not help that much, because any of gcc's sign-conversion
warnings generate far too much noise to be useful with our current
codebase).

-Peff
Jeff King April 9, 2021, 6:10 p.m. UTC | #2
On Fri, Apr 09, 2021 at 02:06:51PM -0400, Jeff King wrote:

> On Fri, Apr 09, 2021 at 10:32:49AM +0200, Ævar Arnfjörð Bjarmason wrote:
> 
> > diff --git a/object.c b/object.c
> > index 7fdca3ed1e..88de01e5ac 100644
> > --- a/object.c
> > +++ b/object.c
> > @@ -39,9 +39,6 @@ int type_from_string_gently(const char *str, ssize_t len, int gentle)
> >  {
> >  	int i;
> >  
> > -	if (len < 0)
> > -		len = strlen(str);
> > -
> 
> The "ssize_t len" in the parameters could become a size_t now, right?
> 
> Not strictly necessary, but in theory it may help static analysis catch
> a caller who mistakenly tries to pass -1 (though in practice I suspect
> it does not help that much, because any of gcc's sign-conversion
> warnings generate far too much noise to be useful with our current
> codebase).

Actually, seeing patch 2, which changes the signature, mostly deals with
this. The compiler would complain about any existing calls because of
dropping the "gentle" parameter (it is up to the human to realize that
they need to make sure we are not passing a negative len, but hopefully
they would look at the other commits at that point).

-Peff
diff mbox series

Patch

diff --git a/object.c b/object.c
index 7fdca3ed1e..88de01e5ac 100644
--- a/object.c
+++ b/object.c
@@ -39,9 +39,6 @@  int type_from_string_gently(const char *str, ssize_t len, int gentle)
 {
 	int i;
 
-	if (len < 0)
-		len = strlen(str);
-
 	for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
 		if (!strncmp(str, object_type_strings[i], len) &&
 		    object_type_strings[i][len] == '\0')
@@ -53,6 +50,13 @@  int type_from_string_gently(const char *str, ssize_t len, int gentle)
 	die(_("invalid object type \"%s\""), str);
 }
 
+int type_from_string(const char *str)
+{
+	size_t len = strlen(str);
+	int ret = type_from_string_gently(str, len, 0);
+	return ret;
+}
+
 /*
  * Return a numerical hash value between 0 and n-1 for the object with
  * the specified sha1.  n must be a power of 2.  Please note that the
diff --git a/object.h b/object.h
index 59daadce21..3ab3eb193d 100644
--- a/object.h
+++ b/object.h
@@ -94,7 +94,7 @@  struct object {
 
 const char *type_name(unsigned int type);
 int type_from_string_gently(const char *str, ssize_t, int gentle);
-#define type_from_string(str) type_from_string_gently(str, -1, 0)
+int type_from_string(const char *str);
 
 /*
  * Return the current number of buckets in the object hashmap.