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

Re: Fwd: coverity problems in reftable code

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Dec 7, 2021, 17:46 UTC
Message-ID
<211207.86o85snvvv.gmgdl@evledraar.gmail.com>
In-Reply-To
<CAFQ2z_OrN+RkwnMyrJHdh5xN6ueOP8KKBVQ7-U4kEkA3ApcuNg@mail.gmail.com>
On Tue, Dec 07 2021, Han-Wen Nienhuys wrote:
Show 20 quoted lines
> On Sat, Dec 4, 2021 at 3:13 AM Jeff King <peff@peff.net> wrote:
>> We're not doing project-wide analysis with Coverity right now, but I've
>> been doing builds of my personal repo, which I usually build off of
>> next. And since hn/reftable just hit next, it got included in my latest
>> build.
>>
>> It came up with several complaints. Some of them are dumb and can be
>> ignored (e.g., using rand() in a test harness, oh no!) but I poked at a
>> few and they look like real issues:
>
> I fixed most of the obvious ones.
>
>>   - A lot of your structs have vtables. Initializing them to NULL, as in
>>     reftable_reader_refs_for_indexed(), leaves the risk that we'll try
>>     to call a NULL function pointer, even if it's for something simple
>
> I have the impression that coverity doesn't understand enough of the
> control flow.  Some of the things it complains of are code paths that
> only get executed if err==0, in which case, the struct members at hand
> should not be null.

I think coverity is right and the code has a logic error as it suggests.

In the reftable_reader_refs_for_indexed() example Jeff cites we'll "goto done" on error, and the reftable_record_release(&got_rec) will proceed to segfault since the next thing we do is to try to dereference a NULL pointer in reftable_record_release().

You can reproduce that as a segfault in your tests with e.g. this patch below, which just emulates what would happen if "err != 0":

diff --git a/reftable/reader.c b/reftable/reader.c
index 006709a645a..4c87b75a982 100644
--- a/reftable/reader.c
+++ b/reftable/reader.c
@@ -663,6 +663,7 @@ static int reftable_reader_refs_for_indexed(struct reftable_reader *r,
        /* Look through the reverse index. */
        reftable_record_from_obj(&want_rec, &want);
        err = reader_seek(r, &oit, &want_rec);
+       goto done;
        if (err != 0)
                goto done;

In that particular case this appears to be the quick fix that's needed:

diff --git a/reftable/record.c b/reftable/record.c
index 6a5dac32dc6..e6594d555e5 100644
--- a/reftable/record.c
+++ b/reftable/record.c
@@ -1090,6 +1090,8 @@ int reftable_record_decode(struct reftable_record *rec, struct strbuf key,
 
 void reftable_record_release(struct reftable_record *rec)
 {
+       if (!rec || !rec->ops)
+               return;
        rec->ops->release(rec->data);
 }

But in general this looks like an excellent candidate for some test
fuzzing, i.e. to intrstrument the various "err" returning functions to
chaos-monkey return non-zero some of the time and check for segfaults.
Previous: Han-Wen NienhuysNext: Jeff King
Message 3 of 7 in “coverity problems in reftable code”
  1. Jeff KingDec 4, 2021
  2. Fwd: coverity problems in reftable codeHan-Wen Nienhuys, Dec 7, 2021
  3. Ævar Arnfjörð BjarmasonDec 7, 2021
  4. Jeff KingDec 8, 2021
  5. Jeff KingDec 8, 2021
  6. Han-Wen NienhuysDec 8, 2021
  7. Jeff KingDec 8, 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.