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

Re: [PATCH v3 2/5] strvec: add a strvec_pushvec()

From
Jeff King <peff@peff.net>
Date
Sep 3, 2021, 11:06 UTC
Message-ID
<YTIBnT8Ue1HZXs82@coredump.intra.peff.net>
In-Reply-To
<87v93i8svd.fsf@evledraar.gmail.com>
On Fri, Sep 03, 2021 at 01:19:33AM +0200, Ævar Arnfjörð Bjarmason wrote:
Show 8 quoted lines
> > One thing that did surprise me: the use of "int" here for iterating,
> > rather than size_t. But it seems that strvec is already storing ints,
> > which is an accident!
> 
> Is it really? If you temporarily try to say convert that to "size_t *nr"
> to have the compiler catch all the cases where we use "nr", and then
> s/size_t/int/g those all, you'll find that e.g. setup_revisions() and
> the like expect to take either "int argc" or the strvec equivalent.

It most definitely is an accident, in the sense that the commit which changed it did not mean to. :)

Show 6 quoted lines
> We can sensibly convert some of those to size_t, but not all, and the
> int v.s. size_t inconsistency as a result feels weird.
> 
> Since the main point of this API is to be a wrapper for what a C main()
> would take, shouldn't its prototype mirror its prototype? I.e. we should
> stick to "int" here?

That isn't the main point of this API. The reason the name changed away from argv_array was so that it would be more obviously a generally useful array of strings.

But even if that were the use, the point isn't that we expect to see 2B or even 4B strings in any reasonable use case. The point is that we don't want to accidentally overflow the integer counter, leading to reads and writes of random bits of memory. And all it takes to do that is some code like:

  while (strbuf_readline(stdin, &buf))
	strvec_push(&foo, buf.buf);

On a system with 32-bit size_t, you are not likely to actually allocate 2B strings before you'd fail. But on most 64-bit systems, you have plenty of address space (and these days, often RAM) to do so, but you still only need 2B strings, because "int" is 32 bits.

So code like setup_revisions() which uses an int is fine. It is reading from a source with that much smaller "int" limit in the first place. Whether strvec can handle bigger arrays or not does not matter either way. And when it later uses ints to operate on such a strvec, it's OK in practice; even if the size_t gets truncated on the fly to an int, we know we did not put more than an int's worth of items into the array in the first place.

But code which has potentially larger inputs (either because it reads iteratively into the strvec as above, or because it is itself using a larger data type) is now also OK, because it's using size_t.

What's not OK is code which operates on a potentially-unbounded strvec using an int. E.g., following the code I showed above with something like:

  int nr = foo.nr;
  foo.v[nr] = xstrdup("replacement string");
which may write to memory outside of foo.v.

Obviously that's nonsense nobody would ever write directly. It is possible for some code to do this inadvertently (say, passing a ptr/int pair through a function interface), but I think it's fairly unlikely in practice. So while I do think more code ought to be using size_t in general, I don't think it's necessary to audit this carefully. The important thing to me is protecting strvec itself.

-Peff
Previous: Junio C HamanoNext: Ævar Arnfjörð Bjarmason
Message 32 of 42 in “bundle: show progress on "unbundle"”
  1. 0/4 bundle: show progress on "unbundle"Ævar Arnfjörð Bjarmason, Jul 27, 2021
  2. 2/4 bundle API: change "flags" to be "extra_index_pack_args"Ævar Arnfjörð Bjarmason, Jul 27, 2021
  3. 1/4 bundle API: start writing API documentationÆvar Arnfjörð Bjarmason, Jul 27, 2021
  4. 3/4 index-pack: add --progress-title optionÆvar Arnfjörð Bjarmason, Jul 27, 2021
  5. 4/4 bundle: show progress on "unbundle"Ævar Arnfjörð Bjarmason, Jul 27, 2021
  6. 0/4 bundle: show progress on "unbundle"Ævar Arnfjörð Bjarmason, Aug 23, 2021
  7. 2/4 bundle API: change "flags" to be "extra_index_pack_args"Ævar Arnfjörð Bjarmason, Aug 23, 2021
  8. Derrick StoleeAug 24, 2021
  9. Ævar Arnfjörð BjarmasonAug 24, 2021
  10. Derrick StoleeAug 24, 2021
  11. 1/4 bundle API: start writing API documentationÆvar Arnfjörð Bjarmason, Aug 23, 2021
  12. Derrick StoleeAug 24, 2021
  13. Ævar Arnfjörð BjarmasonAug 24, 2021
  14. 3/4 index-pack: add --progress-title optionÆvar Arnfjörð Bjarmason, Aug 23, 2021
  15. Derrick StoleeAug 24, 2021
  16. Ævar Arnfjörð BjarmasonAug 24, 2021
  17. 4/4 bundle: show progress on "unbundle"Ævar Arnfjörð Bjarmason, Aug 23, 2021
  18. Derrick StoleeAug 24, 2021
  19. Ævar Arnfjörð BjarmasonAug 24, 2021
  20. 0/5 bundle: show progress on "unbundle"Ævar Arnfjörð Bjarmason, Aug 26, 2021
  21. 1/5 bundle API: start writing API documentationÆvar Arnfjörð Bjarmason, Aug 26, 2021
  22. 3/5 bundle API: change "flags" to be "extra_index_pack_args"Ævar Arnfjörð Bjarmason, Aug 26, 2021
  23. Junio C HamanoAug 28, 2021
  24. 2/5 strvec: add a strvec_pushvec()Ævar Arnfjörð Bjarmason, Aug 26, 2021
  25. Junio C HamanoAug 28, 2021
  26. Junio C HamanoAug 28, 2021
  27. Jeff KingAug 28, 2021
  28. Junio C HamanoAug 29, 2021
  29. Derrick StoleeAug 30, 2021
  30. Ævar Arnfjörð BjarmasonSep 2, 2021
  31. Junio C HamanoSep 3, 2021
  32. Jeff KingSep 3, 2021
  33. 4/5 index-pack: add --progress-title optionÆvar Arnfjörð Bjarmason, Aug 26, 2021
  34. Junio C HamanoAug 28, 2021
  35. 5/5 bundle: show progress on "unbundle"Ævar Arnfjörð Bjarmason, Aug 26, 2021
  36. Junio C HamanoAug 28, 2021
  37. Ævar Arnfjörð BjarmasonSep 2, 2021
  38. 0/4 bundle: show progress on "unbundle"Ævar Arnfjörð Bjarmason, Sep 5, 2021
  39. 1/4 bundle API: start writing API documentationÆvar Arnfjörð Bjarmason, Sep 5, 2021
  40. 2/4 bundle API: change "flags" to be "extra_index_pack_args"Ævar Arnfjörð Bjarmason, Sep 5, 2021
  41. 3/4 index-pack: add --progress-title optionÆvar Arnfjörð Bjarmason, Sep 5, 2021
  42. 4/4 bundle: show progress on "unbundle"Ævar Arnfjörð Bjarmason, Sep 5, 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.