Turn 'if' condition around to avoid Svace complaint
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Wed, 25 Sep 2024 14:42:02 +0000 (16:42 +0200)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Wed, 25 Sep 2024 14:42:02 +0000 (16:42 +0200)
The unwritten assumption of this code is that both events->head and
events->tail are NULL together (an empty list) or they aren't.  So the
code was testing events->head for nullness and using that as a cue to
deference events->tail, which annoys the Svace static code analyzer.
We can silence it by testing events->tail member instead, and add an
assertion about events->head to ensure it's all consistent.

This code is very old and as far as we know, there's never been a bug
report related to this, so there's no need to backpatch.

This was found by the ALT Linux Team using Svace.

Author: Alexander Kuznetsov <kuznetsovam@altlinux.org>
Discussion: https://postgr.es/m/6d0323c3-3f5d-4137-af73-98a5ab90e77c@altlinux.org

src/backend/commands/trigger.c

index 29d30bfb6f7ead9151a6447f118e44b66ce97563..3671e82535e2f3aeb9553ffdc4c221ac2fccbdf9 100644 (file)
@@ -4097,8 +4097,11 @@ afterTriggerAddEvent(AfterTriggerEventList *events,
        chunk->endptr = chunk->endfree = (char *) chunk + chunksize;
        Assert(chunk->endfree - chunk->freeptr >= needed);
 
-       if (events->head == NULL)
+       if (events->tail == NULL)
+       {
+           Assert(events->head == NULL);
            events->head = chunk;
+       }
        else
            events->tail->next = chunk;
        events->tail = chunk;