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

Re: [PATCH] diff: avoid stack-buffer-read-overrun for very long name

From
Jim Meyering <jim@meyering.net>
Date
Apr 26, 2012, 16:22 UTC
Message-ID
<874ns6xy30.fsf@rho.meyering.net>
In-Reply-To
<xmqq62cma2uo.fsf@junio.mtv.corp.google.com>
Junio C Hamano wrote:
Show 19 quoted lines
> Jim Meyering <jim@meyering.net> writes:
>
>> What do you think about replacing those two append-if-needed two-liners:
>>
>>     if (buffer2.len && buffer2.buf[buffer2.len - 1] != '/')
>>             strbuf_addch(&buffer2, '/');
>>
>> by something that readably encapsulates the idiom:
>>
>>     strbuf_append_if_absent (&buffer2, '/');
>>
>> (though the name isn't particularly apt, because you might
>> take "absent" to mean "not anywhere in the string," so maybe
>>   strbuf_append_if_not_already_at_end (ugly) or
>>   strbuf_append_uniq
>> )
>
> I am not good at names, but strbuf_terminate_with(&buffer2, '/')
> perhaps?

Maybe, but it still doesn't evoke the conditional nature of don't-append-if-already-there the operation. i.e., one might wonder how it's different from "strbuf_append".

How about one of these?
  strbuf_ensure_suffix  // but might make you think suffix==more than 1 byte
  strbuf_ensure_last_byte    // maybe?
  strbuf_ensure_last_byte_is // rather long, but apt
Show 11 quoted lines
>> There are several other uses that would benefit from such a transformation:
>> To find the easy ones, I ran this:
>>
>>   git grep -B1 "strbuf_addch.*'"|grep -A1 '!='
>>
>> I've manually marked/separated the ones that don't apply.
>>
>> Note how only 2 of the 6 candidates ensure that length is positive
>> before using ".len - 1":
>
> Yikes, that is embarrasing ;-)
Knowing you/git, each is because the buffer is known to be non-empty.
Previous: Jim MeyeringNext: Andreas Ericsson
Message 11 of 13 in “diff: avoid stack-buffer-read-overrun for very long name”
  1. diff: avoid stack-buffer-read-overrun for very long nameJim Meyering, Apr 16, 2012
  2. Marcus KarlssonApr 16, 2012
  3. Jim MeyeringApr 24, 2012
  4. Junio C HamanoApr 25, 2012
  5. Jim MeyeringApr 26, 2012
  6. Junio C HamanoApr 26, 2012
  7. Bert WesargApr 26, 2012
  8. Jim MeyeringApr 26, 2012
  9. Bert WesargApr 26, 2012
  10. Jim MeyeringApr 26, 2012
  11. Jim MeyeringApr 26, 2012
  12. Andreas EricssonApr 27, 2012
  13. Junio C HamanoApr 27, 2012

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.