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

Re: [PATCH] strbuf: use designated initializers in STRBUF_INIT

From
Jeff King <peff@peff.net>
Date
Jul 14, 2017, 17:36 UTC
Message-ID
<20170714173658.2q24oxhatwh5qrqk@sigill.intra.peff.net>
In-Reply-To
<xmqqlgnrq9qi.fsf@gitster.mtv.corp.google.com>
On Fri, Jul 14, 2017 at 09:11:33AM -0700, Junio C Hamano wrote:
Show 10 quoted lines
> As to other things that we currently not allow in our codebase that
> newer compilers can grok, here is what *I* think.  It is *not* meant
> to be an exhaustive "what's new in C99 that is not in C89? what is
> the final verdict on each of them?":
> 
>  - There were occasional cases where we wished if variable-length
>    arrays, flexible array members and variadic macros were available
>    in our codebase during the course of this project.  We would
>    probably want to add a similar test baloon patch for each of
>    them to this series that is currently two-patch long.

I think variable-length arrays are potentially dangerous. They're allocated on the stack, which creates two issues:

  1. You can run out of stack space and segfault, whereas the same
     operation with a heap buffer would be fine. You can say "but this
     VLA will only be used for small things". But then, you can just as
     easily declare a small stack buffer.
  2. My understanding of the recent "Stack Clash" class of
     vulnerabilities[1] is that VLAs make the attacker's job much easier
     (since they can often just send a large input to get you to
     allocate a large stack).

I think variadic macros are a good candidate, though. There have been a number of times where we've had to sacrifice functionality or readability in our helper functions. E.g., the case mentioned in 368953912 (add helpers for allocating flex-array structs, 2016-02-22).

The weather-balloon patch for that should be easy, too: just drop the fallback macros from BUG() or the trace code.

[1] https://www.qualys.com/2017/06/19/stack-clash/stack-clash.txt
Show 11 quoted lines
>  - I prefer to keep decl-after-statement out of our codebase.  I
>    view it as a big plus in code-readability to be able to see a
>    complete list of variables that will be used in a block upfront
>    before starting to read the code that uses them.
> 
>  - Corollary to the above, I do not mind to have a variable
>    declaration in the initialization clause of a for() statement
>    (e.g. "for (int i = 0; i < 4; i++) { ... }"), as the scoping rule
>    is very sensible.  Some of our "for()" statements use the value
>    of the variable after iteration, for which this new construct
>    cannot be used, though.

I agree with both of those points. I think the decl-in-for is nice exactly because it highlights those cases where the iteration variable's value is relevant after the loop ends.

Show 8 quoted lines
>  - This may be showing I am not just old fashioned but also am
>    ignorant, but I do not see much point in using the following in
>    our codebase (iow, I am not aware of places in the existing code
>    that they can be improved by employing these features):
> 
>    . // comments
>    . restricted pointers
>    . static and type qualifiers in parameter array declarators

Agreed, though I think the comment thing is a personal taste issue (just not my taste).

Show 9 quoted lines
> +static int clean_use_color = -1;
> +static char clean_colors[][COLOR_MAXLEN] = {
> +	[CLEAN_COLOR_RESET] = GIT_COLOR_RESET,
> +	[CLEAN_COLOR_PLAIN] = GIT_COLOR_NORMAL,
> +	[CLEAN_COLOR_PROMPT] = GIT_COLOR_BOLD_BLUE,
> +	[CLEAN_COLOR_HEADER] = GIT_COLOR_BOLD,
> +	[CLEAN_COLOR_HELP] = GIT_COLOR_BOLD_RED,
> +	[CLEAN_COLOR_ERROR] = GIT_COLOR_BOLD_RED,
> +};

I think this is much nicer to read. I assume if we have a "hole" in our numbering that the hole is initialized in the usual static way (a COLOR_MAXLEN array full of NULs in this case, I guess)?

-Peff
Previous: Stefan BellerNext: Junio C Hamano
Message 20 of 39 in “strbuf: use designated initializers in STRBUF_INIT”
  1. strbuf: use designated initializers in STRBUF_INITJeff King, Jul 10, 2017
  2. Ben PeartJul 10, 2017
  3. Jeff KingJul 10, 2017
  4. Ben PeartJul 10, 2017
  5. Mike HommeyJul 11, 2017
  6. Junio C HamanoJul 11, 2017
  7. Ævar Arnfjörð BjarmasonJul 12, 2017
  8. Junio C HamanoJul 12, 2017
  9. Johannes SixtJul 13, 2017
  10. Junio C HamanoJul 10, 2017
  11. Stefan BellerJul 10, 2017
  12. Junio C HamanoJul 10, 2017
  13. Andreas SchwabJul 10, 2017
  14. Johannes SixtJul 10, 2017
  15. Junio C HamanoJul 10, 2017
  16. Johannes SixtJul 10, 2017
  17. Junio C HamanoJul 10, 2017
  18. Junio C HamanoJul 14, 2017
  19. Stefan BellerJul 14, 2017
  20. Jeff KingJul 14, 2017
  21. Junio C HamanoJul 14, 2017
  22. Junio C HamanoJul 14, 2017
  23. objects: scope count variable to loopStefan Beller, Jul 19, 2017
  24. Brandon WilliamsJul 19, 2017
  25. Jeff KingJul 24, 2017
  26. Stefan BellerJul 24, 2017
  27. Jeff KingJul 24, 2017
  28. Ævar Arnfjörð BjarmasonJul 14, 2017
  29. Junio C HamanoJul 14, 2017
  30. Mike HommeyJul 14, 2017
  31. Jeff KingJul 15, 2017
  32. Jeff KingJul 11, 2017
  33. brian m. carlsonJul 11, 2017
  34. Stefan BellerJul 11, 2017
  35. brian m. carlsonJul 11, 2017
  36. Johannes SixtJul 11, 2017
  37. brian m. carlsonJul 12, 2017
  38. Johannes SixtJul 12, 2017
  39. Brandon WilliamsJul 10, 2017

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.