Make the UNALIGNED_X_Y() macro more robust against errors.

Message ID 20260809211434.709343-1-jdx@o2.pl
State New
Headers
Series Make the UNALIGNED_X_Y() macro more robust against errors. |

Commit Message

Jan Dubiec Aug. 9, 2026, 9:10 p.m. UTC
  On 23.07.2026 13:33, Torbjorn SVENSSON wrote:
> On 2026-07-22 01:48, Jan Dubiec wrote:[...]>>   /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
>>   #define UNALIGNED_X_Y(X, Y) \
>> -  (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
>> +  (((long)(intptr_t)X & (sizeof (long) - 1)) | \
>> +   ((long)(intptr_t)Y & (sizeof (long) - 1)))
> 
> Not really what you change, but ideally, shouldn't this be:
> 
> +  (((long)(intptr_t)(X) & (sizeof (long) - 1)) | \
> +   ((long)(intptr_t)(Y) & (sizeof (long) - 1)))
> 
> i.e. putting parenthesis around the macro arguments?

I totally agree. Thanks!

/J.D.


Signed-off-by: Jan Dubiec <jdx@o2.pl>
---
 newlib/libc/string/local.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Jeff Johnston Aug. 10, 2026, 6:49 p.m. UTC | #1
Patch merged.

-- Jeff J.

On Sun, Aug 9, 2026 at 5:15 PM Jan Dubiec <jdx@o2.pl> wrote:

> On 23.07.2026 13:33, Torbjorn SVENSSON wrote:
> > On 2026-07-22 01:48, Jan Dubiec wrote:[...]>>   /* Nonzero if either X
> or Y is not aligned on a "long" boundary.  */
> >>   #define UNALIGNED_X_Y(X, Y) \
> >> -  (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
> >> +  (((long)(intptr_t)X & (sizeof (long) - 1)) | \
> >> +   ((long)(intptr_t)Y & (sizeof (long) - 1)))
> >
> > Not really what you change, but ideally, shouldn't this be:
> >
> > +  (((long)(intptr_t)(X) & (sizeof (long) - 1)) | \
> > +   ((long)(intptr_t)(Y) & (sizeof (long) - 1)))
> >
> > i.e. putting parenthesis around the macro arguments?
>
> I totally agree. Thanks!
>
> /J.D.
>
>
> Signed-off-by: Jan Dubiec <jdx@o2.pl>
> ---
>  newlib/libc/string/local.h | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/newlib/libc/string/local.h b/newlib/libc/string/local.h
> index 94c7c4a8f..d9bff40ab 100644
> --- a/newlib/libc/string/local.h
> +++ b/newlib/libc/string/local.h
> @@ -31,8 +31,8 @@ int __wcwidth (wint_t);
>  #else /* _HAVE_HW_MISALIGNED_ACCESS */
>  /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
>  #define UNALIGNED_X_Y(X, Y) \
> -  (((long)(intptr_t)X & (sizeof (long) - 1)) | \
> -   ((long)(intptr_t)Y & (sizeof (long) - 1)))
> +  (((long)(intptr_t)(X) & (sizeof (long) - 1)) | \
> +   ((long)(intptr_t)(Y) & (sizeof (long) - 1)))
>  #endif /* _HAVE_HW_MISALIGNED_ACCESS */
>
>  /* How many bytes are copied each iteration of the word copy loop.  */
> --
> 2.55.0
>
>
  

Patch

diff --git a/newlib/libc/string/local.h b/newlib/libc/string/local.h
index 94c7c4a8f..d9bff40ab 100644
--- a/newlib/libc/string/local.h
+++ b/newlib/libc/string/local.h
@@ -31,8 +31,8 @@  int __wcwidth (wint_t);
 #else /* _HAVE_HW_MISALIGNED_ACCESS */
 /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
 #define UNALIGNED_X_Y(X, Y) \
-  (((long)(intptr_t)X & (sizeof (long) - 1)) | \
-   ((long)(intptr_t)Y & (sizeof (long) - 1)))
+  (((long)(intptr_t)(X) & (sizeof (long) - 1)) | \
+   ((long)(intptr_t)(Y) & (sizeof (long) - 1)))
 #endif /* _HAVE_HW_MISALIGNED_ACCESS */
 
 /* How many bytes are copied each iteration of the word copy loop.  */