Weird usages of sentinel_ok()

Started by David Rowleyabout 1 month ago4 messageshackers
Jump to latest
#1David Rowley
dgrowleyml@gmail.com

In [1]/messages/by-id/CAApHDvp_-zh5cdR_aNaUCXGHeddWC0b1a5kiJpC1cKdG0vx6DA@mail.gmail.com, Matthias is proposing to introduce a new system memory context
type that basically ties palloc directly to malloc. While reviewing
that patch, I noticed it copied a usage of sentinel_ok() that deviates
from the norm. Technically it's fine, as sentinel_ok()'s first
parameter is a void pointer, so passing the MemoryChunk rather than
what palloc() would return is fine, provided you add on the byte
difference to the MemoryChunk. I felt the need to mention the
weirdness during the review, and since people clearly copy a load of
code from an existing context type when adding new ones, it means that
pattern is going to be copied again when the next person adds a new
allocator.

I thought it might be good to adjust this at the source to prevent that.

Anyone disagree?

Patch attached.

David

[1]: /messages/by-id/CAApHDvp_-zh5cdR_aNaUCXGHeddWC0b1a5kiJpC1cKdG0vx6DA@mail.gmail.com

Attachments:

t254200_1
v1-0001-Adjust-weird-usages-of-sentinel_ok.patchapplication/octet-stream; name=v1-0001-Adjust-weird-usages-of-sentinel_ok.patchDownload+3-4
#2shihao zhong
zhong950419@gmail.com
In reply to: David Rowley (#1)
Re: Weird usages of sentinel_ok()

Hi David,

The patch looks right to me. All three CHUNKHDRSZ macros are
sizeof(MemoryChunk), so the checked byte is the same.

While looking at the other callers I noticed bump.c sets the sentinel
and requested_size in BumpAllocLarge() and BumpAllocChunkFromBlock(),
but nothing ever reads them. Bump has no pfree, so BumpCheck() is the
only place that could check it, and it doesn't. Is that on purpose, or
should BumpCheck() call sentinel_ok() like the others do?

Thanks,
Shihao

#3David Rowley
dgrowleyml@gmail.com
In reply to: shihao zhong (#2)
Re: Weird usages of sentinel_ok()

On Fri, 16 Oct 2026 at 16:00, shihao zhong <zhong950419@gmail.com> wrote:

The patch looks right to me. All three CHUNKHDRSZ macros are
sizeof(MemoryChunk), so the checked byte is the same.

Thanks for checking.

While looking at the other callers I noticed bump.c sets the sentinel
and requested_size in BumpAllocLarge() and BumpAllocChunkFromBlock(),
but nothing ever reads them. Bump has no pfree, so BumpCheck() is the
only place that could check it, and it doesn't. Is that on purpose, or
should BumpCheck() call sentinel_ok() like the others do?

It should be checked. It's quite a bad oversight. I've pushed a fix
back to v17 to resolve that.

David

#4David Rowley
dgrowleyml@gmail.com
In reply to: shihao zhong (#2)
Re: Weird usages of sentinel_ok()

On Fri, 16 Oct 2026 at 16:00, shihao zhong <zhong950419@gmail.com> wrote:

The patch looks right to me. All three CHUNKHDRSZ macros are
sizeof(MemoryChunk), so the checked byte is the same.

I've now pushed the patch to adjust the 3 sentinel_ok() checks.

David