Re: [PATCH 04/44] wrapper: add function to compare strings with different NUL termination
- From
- Martin Ågren <martin.agren@gmail.com>
- Date
- May 13, 2020, 19:32 UTC
- Message-ID
- <CAN0heSqBeW86yyH6vUATX+Az=4qG0gW_QhgsbWnTKXX=Q9hiig@mail.gmail.com>
- In-Reply-To
- <20200513005424.81369-5-sandals@crustytoothpaste.net>
On Wed, 13 May 2020 at 02:56, brian m. carlson <sandals@crustytoothpaste.net> wrote:
Show 25 quoted lines
> diff --git a/git-compat-util.h b/git-compat-util.h > index 8ba576e81e..6503deb171 100644 > --- a/git-compat-util.h > +++ b/git-compat-util.h > @@ -868,6 +868,8 @@ char *xgetcwd(void); > FILE *fopen_for_writing(const char *path); > FILE *fopen_or_warn(const char *path, const char *mode); > > +int xstrncmpz(const char *s, const char *t, size_t len); > + > /* > * FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note > * that ptr is used twice, so don't pass e.g. ptr++. > diff --git a/wrapper.c b/wrapper.c > index 3a1c0e0526..15a09740e7 100644 > --- a/wrapper.c > +++ b/wrapper.c > @@ -430,6 +430,18 @@ int xmkstemp(char *filename_template) > return fd; > } > > +/* > + * Like strncmp, but only return zero if s is NUL-terminated and exactly len > + * characters long. If it is not, consider it greater than t. > + */
I think this comment would be easier to find in the .h file.
And since I'm already commenting...
Show 11 quoted lines
> +int xstrncmpz(const char *s, const char *t, size_t len)
> +{
> + int res = strncmp(s, t, len);
> + if (res)
> + return res;
> + return s[len] == '\0' ? 0 : 1;
> +}
> +
> /* Adapted from libiberty's mkstemp.c. */
>
> #undef TMP_MAXIt's not entirely obvious from the context, but this function is inserted between some "tmp" stuff and some other "tmp" stuff. I don't think we need to bikeshed its exact home, but maybe "close to other string stuff", or at least not in the middle of the "tmp" section.
Martin