[binutils-gdb] obstack.h __PTR_ALIGN vs. ubsan

Alan Modra amodra@sourceware.org
Thu Sep 2 04:19:02 GMT 2021


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=76aa354424729aafdabdf4929aa9751c6fdee081

commit 76aa354424729aafdabdf4929aa9751c6fdee081
Author: Alan Modra <amodra@gmail.com>
Date:   Thu Sep 2 09:05:05 2021 +0930

    obstack.h __PTR_ALIGN vs. ubsan
    
    Current ubsan complains on every use of __PTR_ALIGN (when ptrdiff_t is
    as large as a pointer), due to making calculations relative to a NULL
    pointer.  This patch avoids the problem by extracting out and
    simplifying __BPTR_ALIGN for the usual case.  I've continued to use
    ptrdiff_t here, where it might be better to throw away __BPTR_ALIGN
    entirely and just assume uintptr_t exists.
    
            * obstack.h (__PTR_ALIGN): Expand and simplify __BPTR_ALIGN
            rather than calculating relative to a NULL pointer.

Diff:
---
 include/obstack.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/obstack.h b/include/obstack.h
index a6eb6c95587..0d8746f835b 100644
--- a/include/obstack.h
+++ b/include/obstack.h
@@ -137,9 +137,9 @@
    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)
+#define __PTR_ALIGN(B, P, A)						\
+  (sizeof (ptrdiff_t) < sizeof (void *) ? __BPTR_ALIGN (B, P, A)	\
+   : (char *) (((ptrdiff_t) (P) + (A)) & ~(A)))
 
 #ifndef __attribute_pure__
 # if defined __GNUC_MINOR__ && __GNUC__ * 1000 + __GNUC_MINOR__ >= 2096


More information about the Binutils-cvs mailing list