Skip to content

Replace manual offset calculation with offsetof to avoid UB - #1

Closed
kenballus wants to merge 1 commit into
wtarreau:masterfrom
kenballus:master
Closed

kenballus wants to merge 1 commit into
wtarreau:masterfrom
kenballus:master

Conversation

@kenballus

Copy link
Copy Markdown
Contributor

This header unconditionally includes stddef.h, so offsetof is guaranteed to be defined.

(Only submitting this PR to shut up UBSan in HAProxy.)

@wtarreau

Copy link
Copy Markdown
Owner

This header unconditionally includes stddef.h, so offsetof is guaranteed to be defined.

No it's not guaranteed at all, it depends on compilers types and versions. We've had to add #ifndef/#define offsetof everywhere for a long time now due to this. However, here in cebtree we already define it, and use it almost everywhere, so the one you've found clearly is a leftover that was uncaught. I'll merge it, thanks Ben!

@kenballus

Copy link
Copy Markdown
Contributor Author

No it's not guaranteed at all, it depends on compilers types and versions. We've had to add #ifndef/#define offsetof everywhere for a long time now due to this.

I should have been clearer; I meant guaranteed by the standards. offsetof was defined to be in stddef.h even in C89.

If you care about supporting non-standard C compilers, then nothing is ever guaranteed :)

I'll merge it, thanks Ben!

Sure thing!

@wtarreau

wtarreau commented Nov 7, 2025

Copy link
Copy Markdown
Owner

Now merged, I've extended the commit message a little bit from the one you wrote for ebtree. I'm going to merge it into haproxy as well. Thanks Ben!

@wtarreau wtarreau closed this Nov 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants