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

Re: [PATCH] strbuf: stop out-of-boundary warnings from Coverity

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 18, 2015, 16:46 UTC
Message-ID
<xmqqioalezhq.fsf@gitster.dls.corp.google.com>
In-Reply-To
<20150618101353.GA6525@lanh>
Duy Nguyen <pclouds@gmail.com> writes:
Show 17 quoted lines
> The last resort is simply filter out a whole class of warnings.
> Probably good enough if both patches look equally ugly.
>
> -- 8< --
> Subject: [PATCH] strbuf: kill strbuf_slopbuf, in favor of ""
>
> A lot of "out-of-bound access" warnings on scan.coverity.com is because
> it does not realize this strbuf_slopbuf[] is in fact initialized with a
> single and promised to never change. But that promise could be broken if
> some caller attempts to write to strbuf->buf[0] write after STRBUF_INIT.
>
> We really can't do much about it. But we can try to put strbuf_slopbuf
> in .rodata section, where writes will be caught by the OS with memory
> protection support. The only drawback is people can't do
> "buf->buf == strbuf_slopbuf" any more. Luckily nobody does that in the
> current code base.
> ---

Hmph, would declaring slopbuf as "const char [1]" (and sprinkling the "(char *)" cast) have the same effect, I wonder?

Show 5 quoted lines
> +static inline void strbuf_terminate(struct strbuf *sb)
> +{
> +	if (sb->buf[sb->len])
> +		sb->buf[sb->len] = '\0';
> +}

This is so that you can call things like strbuf_rtrim() immediately after running strbuf_init() safely, but I think it needs a comment to save people from wondering what is going on, e.g. "this is not an optimization to avoid assigning NUL to a place that is already NUL; a freshly initialized strbuf points at an unwritable piece of NUL and we do not want to cause a SEGV".

Previous: Duy NguyenNext: Duy Nguyen
Message 7 of 13 in “strbuf: stop out-of-boundary warnings from Coverity”
  1. strbuf: stop out-of-boundary warnings from CoverityNguyễn Thái Ngọc Duy, Jun 17, 2015
  2. Stefan BellerJun 17, 2015
  3. Stefan BellerJun 17, 2015
  4. Jeff KingJun 17, 2015
  5. Stefan BellerJun 17, 2015
  6. Duy NguyenJun 18, 2015
  7. Junio C HamanoJun 18, 2015
  8. Duy NguyenJun 19, 2015
  9. Remi Galan AlfonsoJun 19, 2015
  10. Duy NguyenJun 19, 2015
  11. Junio C HamanoJun 19, 2015
  12. Junio C HamanoJun 17, 2015
  13. Stefan BellerJun 17, 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.