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

Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Sep 3, 2015, 14:39 UTC
Message-ID
<CAPig+cRJG7t1M-FyrB84UG4Ar_NBW3J+wFvFeLg599sAEAweHw@mail.gmail.com>
In-Reply-To
<1441131994-13508-8-git-send-email-Karthik.188@gmail.com>
On Tue, Sep 1, 2015 at 2:26 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
Show 28 quoted lines
> In 'tag.c' we can print N lines from the annotation of the tag using
> the '-n<num>' option. Copy code from 'tag.c' to 'ref-filter' and
> modify it to support appending of N lines from the annotation of tags
> to the given strbuf.
>
> Implement %(contents:lines=X) where X lines of the given object are
> obtained.
>
> Add documentation and test for the same.
>
> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
> ---
>  struct atom_value {
>         const char *s;
> -       struct align align;
> +       union {
> +               struct align align;
> +               struct contents contents;
> +       } u;
>         void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
>         unsigned long ul; /* used for sorting when not FIELD_STR */
>  };
> @@ -226,7 +235,7 @@ static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_s
>         push_stack_element(&state->stack);
>         new = state->stack;
>         new->at_end = align_handler;
> -       new->cb_data = &atomv->align;
> +       new->cb_data = &atomv->u.align;

You could declare atom_value with the union from the start, even if it has only a single member ('align', at first). Doing so would make this patch less noisy and wouldn't distract the reader with these seemingly unrelated changes.

Show 19 quoted lines
>  }
>
>  static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
> @@ -624,6 +633,33 @@ static void find_subpos(const char *buf, unsigned long sz,
>         *nonsiglen = *sig - buf;
>  }
>
> +/*
> + * If 'lines' is greater than 0, append that many lines from the given
> + * 'buf' of length 'size' to the given strbuf.
> + */
> +static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
> +{
> +       int i;
> +       const char *sp, *eol;
> +       size_t len;
> +
> +       if ((sp = strstr(buf, "\n\n")) && (sp <= buf + size))
> +               size += 2;

Aside from the +2 which Matthieu already questioned, this code has a much more serious problem. strstr() assumes that 'buf' is NUL-terminated, however, the fact that buf's size is also being passed to the function, implies that it may not be NUL-terminated. Consequently, strstr() could zip well past the end of 'buf', trying to consult memory not part of 'buf', which is a violation of the C standard. Even with the band-aid (sp <= buf + size) which tries to detect this situation, it's still a violation, and could crash if strstr() attempts to consult memory which hasn't even been allocated to the process or is otherwise protected in some fashion.

It's possible that 'buf' may actually always be NUL-terminated, but this code does not convey that fact. If it is known to be NUL-terminated, then it is safe to use strstr(), however, that should be shown more clearly either by revising the code or adding an appropriate comment.

Show 21 quoted lines
> +       sp = buf;
> +
> +       for (i = 0; i < lines && sp < buf + size; i++) {
> +               if (i)
> +                       strbuf_addstr(out, "\n    ");
> +               eol = memchr(sp, '\n', size - (sp - buf));
> +               len = eol ? eol - sp : size - (sp - buf);
> +               strbuf_add(out, sp, len);
> +               if (!eol)
> +                       break;
> +               sp = eol + 1;
> +       }
> +}
> @@ -663,6 +701,13 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
>                         v->s = xmemdupz(sigpos, siglen);
>                 else if (!strcmp(name, "contents"))
>                         v->s = xstrdup(subpos);
> +               else if (skip_prefix(name, "contents:lines=", &valp)) {
> +                       struct strbuf s = STRBUF_INIT;
> +                       if (strtoul_ui(valp, 10, &v->u.contents.lines))
> +                               die(_("positive width expected contents:lines=%s"), valp);

This error message is still too tightly coupled to %(align:), from which it was copied. "width"?

Show 5 quoted lines
> +                       append_lines(&s, subpos, sublen + bodylen - siglen, v->u.contents.lines);
> +                       v->s = strbuf_detach(&s, NULL);
> +               }
>         }
>  }
Previous: Karthik NayakNext: Eric Sunshine
Message 39 of 58 in “port builtin/tag.c to use ref-filter APIs.”
  1. 00/13 port builtin/tag.c to use ref-filter APIs.Karthik Nayak, Sep 1, 2015
  2. 01/13 ref-filter: move `struct atom_value` to ref-filter.cKarthik Nayak, Sep 1, 2015
  3. 02/13 ref-filter: introduce ref_formatting_state and ref_formatting_stackKarthik Nayak, Sep 1, 2015
  4. 03/13 utf8: add function to align a string into given strbufKarthik Nayak, Sep 1, 2015
  5. 04/13 ref-filter: introduce handler function for each atomKarthik Nayak, Sep 1, 2015
  6. 05/13 ref-filter: implement an `align` atomKarthik Nayak, Sep 1, 2015
  7. Junio C HamanoSep 1, 2015
  8. Karthik NayakSep 2, 2015
  9. Junio C HamanoSep 2, 2015
  10. Karthik NayakSep 2, 2015
  11. Junio C HamanoSep 2, 2015
  12. Karthik NayakSep 2, 2015
  13. Matthieu MoySep 2, 2015
  14. Junio C HamanoSep 2, 2015
  15. Karthik NayakSep 3, 2015
  16. Matthieu MoySep 2, 2015
  17. Matthieu MoySep 2, 2015
  18. Karthik NayakSep 2, 2015
  19. Matthieu MoySep 2, 2015
  20. Karthik NayakSep 2, 2015
  21. Matthieu MoySep 2, 2015
  22. Eric SunshineSep 3, 2015
  23. Karthik NayakSep 3, 2015
  24. Junio C HamanoSep 3, 2015
  25. Karthik NayakSep 4, 2015
  26. 06/13 ref-filter: add option to filter out tags, branches and remotesKarthik Nayak, Sep 1, 2015
  27. Junio C HamanoSep 1, 2015
  28. Karthik NayakSep 2, 2015
  29. Junio C HamanoSep 2, 2015
  30. Karthik NayakSep 2, 2015
  31. 07/13 ref-filter: add support for %(contents:lines=X)Karthik Nayak, Sep 1, 2015
  32. Matthieu MoySep 2, 2015
  33. Karthik NayakSep 2, 2015
  34. Matthieu MoySep 2, 2015
  35. Karthik NayakSep 3, 2015
  36. Karthik NayakSep 3, 2015
  37. Matthieu MoySep 3, 2015
  38. Karthik NayakSep 3, 2015
  39. Eric SunshineSep 3, 2015
  40. Eric SunshineSep 3, 2015
  41. Matthieu MoySep 3, 2015
  42. Karthik NayakSep 3, 2015
  43. Junio C HamanoSep 3, 2015
  44. Karthik NayakSep 4, 2015
  45. Matthieu MoySep 3, 2015
  46. Karthik NayakSep 3, 2015
  47. 08/13 ref-filter: add support to sort by versionKarthik Nayak, Sep 1, 2015
  48. 09/13 ref-filter: add option to match literal patternKarthik Nayak, Sep 1, 2015
  49. 10/13 tag.c: use 'ref-filter' data structuresKarthik Nayak, Sep 1, 2015
  50. 11/13 tag.c: use 'ref-filter' APIsKarthik Nayak, Sep 1, 2015
  51. Matthieu MoySep 2, 2015
  52. Junio C HamanoSep 2, 2015
  53. Karthik NayakSep 2, 2015
  54. Matthieu MoySep 2, 2015
  55. Junio C HamanoSep 2, 2015
  56. Karthik NayakSep 3, 2015
  57. 12/13 tag.c: implement '--format' optionKarthik Nayak, Sep 1, 2015
  58. 13/13 tag.c: implement '--merged' and '--no-merged' optionsKarthik Nayak, Sep 1, 2015

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.