From: Bruno Haible Date: Fri, 1 Dec 2023 18:39:26 +0000 (+0100) Subject: obstack: Avoid undefined behaviour. X-Git-Tag: v1.0~570 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=e7cd5fb11f665d9e27f01d54672675199cd57f2d;p=gnulib.git obstack: Avoid undefined behaviour. Reported by Alexey Palienko in . * lib/obstack.in.h: Include . (__BPTR_ALIGN): Remove macro. (__PTR_ALIGN): For the optimized case, compute the alignment through uintptr_t, instead of computing NULL + something. --- diff --git a/ChangeLog b/ChangeLog index c486d5b4ac..b48c7912e9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,14 @@ -2023-12-02 Bruno Haible +2023-12-01 Bruno Haible + + obstack: Avoid undefined behaviour. + Reported by Alexey Palienko in + . + * lib/obstack.in.h: Include . + (__BPTR_ALIGN): Remove macro. + (__PTR_ALIGN): For the optimized case, compute the alignment through + uintptr_t, instead of computing NULL + something. + +2023-12-01 Bruno Haible doc: Update for FreeBSD 14.0. * doc/posix-headers/*.texi: Update. diff --git a/lib/obstack.in.h b/lib/obstack.in.h index 265203b6e2..468a797341 100644 --- a/lib/obstack.in.h +++ b/lib/obstack.in.h @@ -111,6 +111,7 @@ #endif #include /* For size_t and ptrdiff_t. */ +#include /* For uintptr_t. */ #include /* For memcpy. */ #if __STDC_VERSION__ < 199901L || defined __HP_cc @@ -134,20 +135,15 @@ /* If B is the base of an object addressed by P, return the result of aligning P to the next multiple of A + 1. B and P must be of type - char *. A + 1 must be a power of 2. */ - -#define __BPTR_ALIGN(B, P, A) ((B) + (((P) - (B) + (A)) & ~(A))) - -/* Similar to __BPTR_ALIGN (B, P, A), except optimize the common case - where pointers can be converted to integers, aligned as integers, - and converted back again. If ptrdiff_t is narrower than a - pointer (e.g., the AS/400), play it safe and compute the alignment - relative to B. Otherwise, use the faster strategy of computing the - alignment relative to 0. */ - -#define __PTR_ALIGN(B, P, A) \ - __BPTR_ALIGN (sizeof (ptrdiff_t) < sizeof (void *) ? (B) : (char *) 0, \ - P, A) + char *. A + 1 must be a power of 2. + If ptrdiff_t is narrower than a pointer (e.g., the AS/400), play it + safe and compute the alignment relative to B. Otherwise, use the + faster strategy of computing the alignment through uintptr_t. */ + +#define __PTR_ALIGN(B, P, A) \ + (sizeof (ptrdiff_t) < sizeof (void *) \ + ? (B) + (((P) - (B) + (A)) & ~(A)) \ + : (P) + ((- (uintptr_t) (P)) & (A))) #ifndef __attribute_pure__ # define __attribute_pure__ _GL_ATTRIBUTE_PURE