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

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

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jun 17, 2015, 10:16 UTC
Message-ID
<1434536209-31350-1-git-send-email-pclouds@gmail.com>
It usually goes like this
    strbuf sb = STRBUF_INIT;
    if (!strncmp(sb.buf, "foo", 3))
       printf("%s", sb.buf + 3);

Coverity thinks that printf() can be executed, and because initial sb.buf only has one character (from strbuf_slopbuf), sb.buf + 3 is out of bound. What it does not recognize is strbuf_slopbuf[0] is always (*) zero. We always do some string comparison before jumping ahead to "sb.buf + 3" and those operations will stop out of bound accesses.

Just make strbuf_slopbuf[] large enough to keep Coverity happy. If it's happy, we'll have cleaner defect list and better chances of spotting true defects.

(*) It's not entirely wrong though. Somebody may do sb.buf[0] = 'f'
    right after variable declaration and ruin all unused strbuf.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 There are lots of false warnings like this from Coverity. I just
 wanted to kill them off so we can spot more serious problems easier.
 I can't really verify that this patch shuts off those warnings
 because scan.coverity.com policy does not allow forks.
 I had another patch that avoids corrupting strbuf_slopbuf, by putting
 it to .rodata section. The patch is more invasive though, because
 this statement buf.buf[buf.len] = '\0' now has to make sure buf.buf
 is not strbuf_slopbuf. It feels safer, but probably not enough to
 justify the change.
 strbuf.c | 5 +++++
 1 file changed, 5 insertions(+)
diff --git a/strbuf.c b/strbuf.c
index 0d4f4e5..0d7c3cf 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -16,7 +16,12 @@ int starts_with(const char *str, const char *prefix)
  * buf is non NULL and ->buf is NUL terminated even for a freshly
  * initialized strbuf.
  */
+#ifndef __COVERITY__
 char strbuf_slopbuf[1];
+#else
+/* Stop so many incorrect out-of-boundary warnings from Coverity */
+char strbuf_slopbuf[64];
+#endif
 
 void strbuf_init(struct strbuf *sb, size_t hint)
 {
-- 
2.3.0.rc1.137.g477eb31
Next: Stefan Beller
Message 1 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.