bufmgr: Fix undefined behaviour with, unrealistically, large temp_buffers
authorAndres Freund <[email protected]>
Thu, 30 Mar 2023 16:50:18 +0000 (09:50 -0700)
committerAndres Freund <[email protected]>
Thu, 30 Mar 2023 17:26:10 +0000 (10:26 -0700)
Quoting Melanie:
> Since if buffer is INT_MAX, then the -(buffer + 1) version invokes
> undefined behavior while the -buffer - 1 version doesn't.

All other places were already using the correct version. I (Andres), copied
the code into more places in a patch. Melanie caught it in review, but to
prevent more people from copying the bad code, fix it. Even if it is a
theoretical issue.

We really ought to wrap these accesses in a helper function...

As this is a theoretical issue, don't backpatch.

Reported-by: Melanie Plageman <[email protected]>
Discussion: https://postgr.es/m/CAAKRu_aW2SX_LWtwHgfnqYpBrunMLfE9PD6-ioPpkh92XH0qpg@mail.gmail.com

src/backend/storage/buffer/localbuf.c

index 5325ddb663ded7fce0451b19d9914abe2764b10d..68b4817c67bcb44b298c174215e05ad4bf1a8569 100644 (file)
@@ -305,7 +305,7 @@ MarkLocalBufferDirty(Buffer buffer)
        fprintf(stderr, "LB DIRTY %d\n", buffer);
 #endif
 
-       bufid = -(buffer + 1);
+       bufid = -buffer - 1;
 
        Assert(LocalRefCount[bufid] > 0);