git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] sideband.c: replace int with size_t for clarity

From
CPChandra Pratap <chandrapratap3519@gmail.com>
Date
Dec 22, 2023, 18:27 UTC
Message-ID
<CA+J6zkQ=+bCsi7URtpTfgawTrmVVALvMi3kdnMi=mUjB_Yxxwg@mail.gmail.com>
In-Reply-To
<20231222175747.GA2880@tb-raspi4>

Thanks for the feedback, Torsten. I was working on improving the rest of sideband.c as you suggested when I encountered this snippet on line 82:

while (0 < n && isspace(*src)) { strbuf_addch(dest, *src); src++; n--; }

Here, we are decreasing the value of an unsigned type to potentially below 0 which may lead to overflow and result in some nasty bug. In this case, is it wise to continue with replacing 'int n' with 'size_t n' as the NEEDSWORK tag suggests or should we improve upon the rest of the file and revert 'size_t n' to 'int n' ?

On Fri, 22 Dec 2023 at 23:27, Torsten Bögershausen <tboegi@web.de> wrote:
Show 62 quoted lines
>
> On Fri, Dec 22, 2023 at 05:01:09PM +0000, Chandra Pratap via GitGitGadget wrote:
> > From: Chandra Pratap <chandrapratap3519@gmail.com>
> >
> > Replace int with size_t for clarity and remove the
> > 'NEEDSWORK' tag associated with it.
> >
> > Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
> > ---
> >     sideband.c: replace int with size_t for clarity
> >
> > Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1625%2FChand-ra%2Fdusra-v1
> > Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1625/Chand-ra/dusra-v1
> > Pull-Request: https://github.com/gitgitgadget/git/pull/1625
> >
> >  sideband.c | 3 +--
> >  1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/sideband.c b/sideband.c
> > index 6cbfd391c47..1599e408d1b 100644
> > --- a/sideband.c
> > +++ b/sideband.c
> > @@ -69,9 +69,8 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
> >   * of the line. This should be called for a single line only, which is
> >   * passed as the first N characters of the SRC array.
> >   *
> > - * NEEDSWORK: use "size_t n" instead for clarity.
> >   */
> > -static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
> > +static void maybe_colorize_sideband(struct strbuf *dest, const char *src, size_t n)
> >  {
> >       int i;
> >
>
> Thanks for working on this.
> There is, however, more potential for improvements.
> First of all: the "int i" from above.
> Further down, we read
>         for (i = 0; i < ARRAY_SIZE(keywords); i++) {
>
> However, a size of an array can never be negative, so that
> an unsigned data type is a better choice than a signed.
> And, arrays can have more elements than an int can address,
> at least in theory.
> For a reader it makes more sense, to replace
> int i;
> with
> size_t i;
>
>
> And further down, there is another place for improvments:
>
>                 int len = strlen(p->keyword);
>                 if (n < len)
>                         continue;
>
> Even here, a strlen is never negative. And a size_t is the choice for len,
> since all "modern" implementations declare strlen() to return size_t
>
> Do you think that you can have a look at these changes ?
>
> I will be happy to do a review, and possibly other people as well.
Previous: Torsten BögershausenNext: Torsten Bögershausen
Message 3 of 12 in “sideband.c: replace int with size_t for clarity”
  1. sideband.c: replace int with size_t for clarityChandra Pratap via GitGitGadget, Dec 22, 2023
  2. Torsten BögershausenDec 22, 2023
  3. Chandra PratapDec 22, 2023
  4. Torsten BögershausenDec 22, 2023
  5. Junio C HamanoDec 22, 2023
  6. Taylor BlauJan 2, 2024
  7. sideband.c: replace int with size_t for clarityChandra Pratap via GitGitGadget, Dec 23, 2023
  8. sideband.c: remove redundant 'NEEDSWORK' tagChandra Pratap via GitGitGadget, Dec 27, 2023
  9. Junio C HamanoDec 27, 2023
  10. sideband.c: remove redundant 'NEEDSWORK' tagChandra Pratap via GitGitGadget, Dec 28, 2023
  11. Junio C HamanoDec 28, 2023
  12. Junio C HamanoDec 26, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.