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

Re: [PATCH v2 0/7] strvec: use size_t to store nr and alloc

From
Jeff King <peff@peff.net>
Date
Sep 12, 2021, 22:19 UTC
Message-ID
<YT586/CO7QsTb3TK@coredump.intra.peff.net>
In-Reply-To
<cover-v2-0.7-00000000000-20210912T001420Z-avarab@gmail.com>
On Sun, Sep 12, 2021 at 02:15:48AM +0200, Ævar Arnfjörð Bjarmason wrote:
Show 11 quoted lines
> This is what I'd been sitting on locally since that recent thread, I
> polished it up a bit since Jeff King posted his version.
> 
> The potential overflow bug I mentioned is in rebase.c. See
> 5/7. "Potential" because it's not a bug now, but that code
> intentionally considers a strvec, and then iterates it from nr-1 to 0,
> and if it reaches 0 intentionally counts down one more to -1 to
> indicate that it's visited all elements.
> 
> We then check that with i >= 0, except of course if it becomes
> unsigned that doesn't become -1, but rather it wraps around.

You can also just use ssize_t, or you can compare against SIZE_MAX to catch the wraparound (there's some prior art in sort_revindex()). That said, I don't mind rewriting loops to count up rather than down. It usually makes them easier to follow (and in your patch 5, I do not see any reason we would need to count down rather than up; we do not even care where we find "-q", only that we found it.

> The rest of this is all changes to have that s/int/size_t/ radiate
> outwards, i.e. when we assign that value to a variable somewhere its
> now a "size_t" instead of an "int" etc.
I'm a little "meh" on some of these, for a few reasons:
 - anything calling into setup_revisions() eventually is just kicking
   the can anyway. And these are generally not buggy in the first place,
   since they're bounded argv creations.
 - passing a strvec instead of the broken-down pair is a less flexible
   interface. It's one thing if the callee benefits from seeing the
   strvec (say, because they may push more items onto it). But I think
   with strbufs, we have a general guideline that if a function _can_
   take the bare pointer, then it should. (Sorry, I don't have a
   succinct reference to CodingGuidelines or anything like that; I feel
   like this is wisdom we came up with on the list in the early days of
   strbufs).
 - if we are going to pass a strvec, it should almost certainly be
   const, to make it clear how we intend to use it.

So if we we wanted to try to reduce the int/size_t conversions here (and I don't mind doing it, but am not altogether sure it is a good use of time, because the rabbit hole runs deep), I think we ought to be switching to size_t everywhere-ish along whole call chains. Or possibly providing a checked size_to_int() which will safely catch and abort. These cases are largely stupid things that real people would never come across. The real goal is making sure we don't get hit with a memory safety bug (under-allocation, converting a big size_t to a negative int, etc).

-Peff
Previous: Eric SunshineNext: Junio C Hamano
Message 16 of 23 in “strvec: use size_t to store nr and alloc”
  1. strvec: use size_t to store nr and allocJeff King, Sep 11, 2021
  2. Ævar Arnfjörð BjarmasonSep 11, 2021
  3. Philip OakleySep 11, 2021
  4. 0/7 strvec: use size_t to store nr and allocÆvar Arnfjörð Bjarmason, Sep 12, 2021
  5. 1/7 remote-curl: pass "struct strvec *" instead of int/char ** pairÆvar Arnfjörð Bjarmason, Sep 12, 2021
  6. Carlo ArenasSep 12, 2021
  7. Ævar Arnfjörð BjarmasonSep 13, 2021
  8. 2/7 pack-objects: pass "struct strvec *" instead of int/char ** pairÆvar Arnfjörð Bjarmason, Sep 12, 2021
  9. 3/7 sequencer.[ch]: pass "struct strvec *" instead of int/char ** pairÆvar Arnfjörð Bjarmason, Sep 12, 2021
  10. 4/7 upload-pack.c: pass "struct strvec *" instead of int/char ** pairÆvar Arnfjörð Bjarmason, Sep 12, 2021
  11. 5/7 rebase: don't have loop over "struct strvec" depend on signed "nr"Ævar Arnfjörð Bjarmason, Sep 12, 2021
  12. Eric SunshineSep 12, 2021
  13. 6/7 strvec: use size_t to store nr and allocÆvar Arnfjörð Bjarmason, Sep 12, 2021
  14. 7/7 strvec API users: change some "int" tracking "nr" to "size_t"Ævar Arnfjörð Bjarmason, Sep 12, 2021
  15. Eric SunshineSep 12, 2021
  16. Jeff KingSep 12, 2021
  17. Junio C HamanoSep 13, 2021
  18. Ævar Arnfjörð BjarmasonSep 13, 2021
  19. Jeff KingSep 13, 2021
  20. Philip OakleySep 13, 2021
  21. Jeff KingSep 12, 2021
  22. Philip OakleySep 13, 2021
  23. Jeff KingSep 12, 2021

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.