]> Savannah Git Hosting - gnulib.git/commitdiff
obstack: Avoid undefined behaviour.
authorBruno Haible <bruno@clisp.org>
Fri, 1 Dec 2023 18:39:26 +0000 (19:39 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 18 Jan 2024 08:12:37 +0000 (09:12 +0100)
Reported by Alexey Palienko <Alexey.Palienko@cma.se> in
<https://lists.gnu.org/archive/html/bug-m4/2023-02/msg00000.html>.

* lib/obstack.in.h: Include <stdint.h>.
(__BPTR_ALIGN): Remove macro.
(__PTR_ALIGN): For the optimized case, compute the alignment through
uintptr_t, instead of computing NULL + something.

ChangeLog
lib/obstack.h

index e53cfffcb63ce737ec9eed85aa5a5aa17de06bb0..49fdf2c8458c428a2fca3d1701b9a873ecac3d16 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2023-12-01  Bruno Haible  <bruno@clisp.org>
+
+       obstack: Avoid undefined behaviour.
+       Reported by Alexey Palienko <Alexey.Palienko@cma.se> in
+       <https://lists.gnu.org/archive/html/bug-m4/2023-02/msg00000.html>.
+       * lib/obstack.h: Include <stdint.h>.
+       (__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  <bruno@clisp.org>
 
        sethostname tests: Fix a compilation error on FreeBSD 14.0.
index 1e66e4d4c8ff116db4ec7723b66e3d70fbdf324a..14a56e7a17e92824b08b9bf29740a313c8e6582c 100644 (file)
 #endif
 
 #include <stddef.h>             /* For size_t and ptrdiff_t.  */
+#include <stdint.h>             /* For uintptr_t.  */
 #include <string.h>             /* For __GNU_LIBRARY__, and memcpy.  */
 
 #if __STDC_VERSION__ < 199901L || defined __HP_cc
 
 /* 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