Re: [PATCH] Silence -Wpointer-to-int-cast warnings in newlib/libc/string/local.h.
Jeff Johnston <[email protected]> Tue, 21 Jul 2026 17:35:02 -0400
| Newsgroups | gmane.comp.lib.newlib |
|---|---|
| Message-ID | <CAOox84uE_Avue-6eDmLGk6KX-3y3tvRmuK3XTGz39CamFwq1jw@mail.gmail.com> |
--0000000000009b6d23065725cc0c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Jan, The fallback for intptr_t is unnecessary. According to libc/include/sys/_intsup.h, gcc 3.2 or higher should support the intptr info we need plus there is an #error statement if it isn't. So, the code should just use intptr_t. -- Jeff J. On Thu, Jul 16, 2026 at 7:40=E2=80=AFPM Jan Dubiec <[email protected]> wrote: > Compiling the library for a 16-bit target results in numerous warnings > (some of which are shown below) in the mem*, str*, and several other > functions. The existing code incorrectly assumes that pointers are 32 > bits wide. This patch removes that assumption and fixes the resulting > warnings. > > [...] > CC libc/string/libc_a-memchr.o > In file included from > ../../../../../../../newlib/newlib/libc/string/memchr.c:35: > ../../../../../../../newlib/newlib/libc/string/memchr.c: In function > 'memchr': > ../../../../../../../newlib/newlib/libc/string/local.h:24:25: warning: > cast from pointer to integer of different size [-Wpointer-to-int-cast] > 24 | #define UNALIGNED_X(X) ((long)(X) & (sizeof (long) - 1)) > | ^ > ../../../../../../../newlib/newlib/libc/string/memchr.c:50:10: note: in > expansion of macro 'UNALIGNED_X' > 50 | while (UNALIGNED_X(src)) > | ^~~~~~~~~~~ > CC libc/string/libc_a-memcmp.o > In file included from > ../../../../../../../newlib/newlib/libc/string/memcmp.c:33: > ../../../../../../../newlib/newlib/libc/string/memcmp.c: In function > 'memcmp': > ../../../../../../../newlib/newlib/libc/string/local.h:33:5: warning: cas= t > from pointer to integer of different size [-Wpointer-to-int-cast] > 33 | (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - > 1))) > | ^ > ../../../../../../../newlib/newlib/libc/string/memcmp.c:63:38: note: in > expansion of macro 'UNALIGNED_X_Y' > 63 | if (!TOO_SMALL_LITTLE_BLOCK(n) && !UNALIGNED_X_Y(s1,s2)) > | ^~~~~~~~~~~~~ > ../../../../../../../newlib/newlib/libc/string/local.h:33:39: warning: > cast from pointer to integer of different size [-Wpointer-to-int-cast] > 33 | (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - > 1))) > | ^ > ../../../../../../../newlib/newlib/libc/string/memcmp.c:63:38: note: in > expansion of macro 'UNALIGNED_X_Y' > 63 | if (!TOO_SMALL_LITTLE_BLOCK(n) && !UNALIGNED_X_Y(s1,s2)) > | ^~~~~~~~~~~~~ > [...] > > Signed-off-by: Jan Dubiec <[email protected]> > --- > newlib/libc/string/local.h | 19 +++++++++++++++++-- > 1 file changed, 17 insertions(+), 2 deletions(-) > > diff --git a/newlib/libc/string/local.h b/newlib/libc/string/local.h > index 012a30d16..79436ab69 100644 > --- a/newlib/libc/string/local.h > +++ b/newlib/libc/string/local.h > @@ -1,5 +1,6 @@ > #include <_ansi.h> > #include <../ctype/local.h> > +#include <stdint.h> > > /* internal function to compute width of wide char. */ > int __wcwidth (wint_t); > @@ -17,11 +18,24 @@ int __wcwidth (wint_t); > # define __inhibit_loop_to_libcall > #endif > > +#ifdef __INTPTR_TYPE__ > + #define INTPTRTYPE intptr_t > +#else > + /* Fallback, just in case there is no intptr_t on a target... */ > + #if __SIZEOF_POINTER__ > __SIZEOF_SHORT__ > + /* 32-bit targets; the default */ > + #define INTPTRTYPE long > + #else > + /* 16-bit targets; we silently assume sizeof(short)=3D=3D2 and > sizeof(long)=3D=3D4 */ > + #define INTPTRTYPE short > + #endif > +#endif > + > /* Nonzero if X is not aligned on a "long" boundary. > * This macro is used to skip a few bytes to find an aligned pointer. > * It's better to keep it as is even if _HAVE_HW_MISALIGNED_ACCESS is > enabled, > * to avoid small performance penalties (if they are not zero). */ > -#define UNALIGNED_X(X) ((long)(X) & (sizeof (long) - 1)) > +#define UNALIGNED_X(X) ((long)(INTPTRTYPE)(X) & (sizeof (long) - 1)) > > #ifdef _HAVE_HW_MISALIGNED_ACCESS > /* Hardware performs unaligned operations with little > @@ -30,7 +44,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)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1))) > + (((long)(INTPTRTYPE)X & (sizeof (long) - 1)) | \ > + ((long)(INTPTRTYPE)Y & (sizeof (long) - 1))) > #endif /* _HAVE_HW_MISALIGNED_ACCESS */ > > /* How many bytes are copied each iteration of the word copy loop. */ > -- > 2.54.0 > > --0000000000009b6d23065725cc0c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-family:verdana,= sans-serif">Hi Jan,</div><div class=3D"gmail_default" style=3D"font-family:= verdana,sans-serif"><br></div><div class=3D"gmail_default" style=3D"font-fa= mily:verdana,sans-serif">The fallback for intptr_t is unnecessary.=C2=A0 Ac= cording to libc/include/sys/_intsup.h, gcc 3.2 or higher should support</di= v><div class=3D"gmail_default" style=3D"font-family:verdana,sans-serif">the= intptr info we need plus there is an #error statement if it isn't.=C2= =A0 So, the code should just use intptr_t.=C2=A0=C2=A0</div><div class=3D"g= mail_default" style=3D"font-family:verdana,sans-serif"><br></div><div class= =3D"gmail_default" style=3D"font-family:verdana,sans-serif">-- Jeff J.</div= ></div><br><div class=3D"gmail_quote gmail_quote_container"><div dir=3D"ltr= " class=3D"gmail_attr">On Thu, Jul 16, 2026 at 7:40=E2=80=AFPM Jan Dubiec &= lt;<a href=3D"mailto:[email protected]">[email protected]</a>> wrote:<br></div><blockquo= te class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px = solid rgb(204,204,204);padding-left:1ex">Compiling the library for a 16-bit= target results in numerous warnings<br> (some of which are shown below) in the mem*, str*, and several other<br> functions. The existing code incorrectly assumes that pointers are 32<br> bits wide. This patch removes that assumption and fixes the resulting<br> warnings.<br> <br> [...]<br> =C2=A0 CC=C2=A0 =C2=A0 =C2=A0 =C2=A0libc/string/libc_a-memchr.o<br> In file included from ../../../../../../../newlib/newlib/libc/string/memchr= .c:35:<br> ../../../../../../../newlib/newlib/libc/string/memchr.c: In function 'm= emchr':<br> ../../../../../../../newlib/newlib/libc/string/local.h:24:25: warning: cast= from pointer to integer of different size [-Wpointer-to-int-cast]<br> =C2=A0 =C2=A024 | #define UNALIGNED_X(X) ((long)(X) & (sizeof (long) - = 1))<br> =C2=A0 =C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0^<br> ../../../../../../../newlib/newlib/libc/string/memchr.c:50:10: note: in exp= ansion of macro 'UNALIGNED_X'<br> =C2=A0 =C2=A050 |=C2=A0 =C2=A0while (UNALIGNED_X(src))<br> =C2=A0 =C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ^~~~~~~~~~~<br> =C2=A0 CC=C2=A0 =C2=A0 =C2=A0 =C2=A0libc/string/libc_a-memcmp.o<br> In file included from ../../../../../../../newlib/newlib/libc/string/memcmp= .c:33:<br> ../../../../../../../newlib/newlib/libc/string/memcmp.c: In function 'm= emcmp':<br> ../../../../../../../newlib/newlib/libc/string/local.h:33:5: warning: cast = from pointer to integer of different size [-Wpointer-to-int-cast]<br> =C2=A0 =C2=A033 |=C2=A0 =C2=A0(((long)X & (sizeof (long) - 1)) | ((long= )Y & (sizeof (long) - 1)))<br> =C2=A0 =C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A0^<br> ../../../../../../../newlib/newlib/libc/string/memcmp.c:63:38: note: in exp= ansion of macro 'UNALIGNED_X_Y'<br> =C2=A0 =C2=A063 |=C2=A0 =C2=A0if (!TOO_SMALL_LITTLE_BLOCK(n) && !UN= ALIGNED_X_Y(s1,s2))<br> =C2=A0 =C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 ^~~~~~~~~~~~~<br> ../../../../../../../newlib/newlib/libc/string/local.h:33:39: warning: cast= from pointer to integer of different size [-Wpointer-to-int-cast]<br> =C2=A0 =C2=A033 |=C2=A0 =C2=A0(((long)X & (sizeof (long) - 1)) | ((long= )Y & (sizeof (long) - 1)))<br> =C2=A0 =C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0^<br> ../../../../../../../newlib/newlib/libc/string/memcmp.c:63:38: note: in exp= ansion of macro 'UNALIGNED_X_Y'<br> =C2=A0 =C2=A063 |=C2=A0 =C2=A0if (!TOO_SMALL_LITTLE_BLOCK(n) && !UN= ALIGNED_X_Y(s1,s2))<br> =C2=A0 =C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 ^~~~~~~~~~~~~<br> [...]<br> <br> Signed-off-by: Jan Dubiec <<a href=3D"mailto:[email protected]" target=3D"_blank= ">[email protected]</a>><br> ---<br> =C2=A0newlib/libc/string/local.h | 19 +++++++++++++++++--<br> =C2=A01 file changed, 17 insertions(+), 2 deletions(-)<br> <br> diff --git a/newlib/libc/string/local.h b/newlib/libc/string/local.h<br> index 012a30d16..79436ab69 100644<br> --- a/newlib/libc/string/local.h<br> +++ b/newlib/libc/string/local.h<br> @@ -1,5 +1,6 @@<br> =C2=A0#include <_ansi.h><br> =C2=A0#include <../ctype/local.h><br> +#include <stdint.h><br> <br> =C2=A0/* internal function to compute width of wide char. */<br> =C2=A0int __wcwidth (wint_t);<br> @@ -17,11 +18,24 @@ int __wcwidth (wint_t);<br> =C2=A0# define __inhibit_loop_to_libcall<br> =C2=A0#endif<br> <br> +#ifdef __INTPTR_TYPE__<br> +=C2=A0 #define INTPTRTYPE intptr_t<br> +#else<br> +=C2=A0 /* Fallback, just in case there is no intptr_t on a target... */<br= > +=C2=A0 #if __SIZEOF_POINTER__ > __SIZEOF_SHORT__<br> +=C2=A0 =C2=A0 /* 32-bit targets; the default */<br> +=C2=A0 =C2=A0 =C2=A0 =C2=A0#define INTPTRTYPE long<br> +=C2=A0 #else<br> +=C2=A0 =C2=A0 /* 16-bit targets; we silently assume sizeof(short)=3D=3D2 a= nd sizeof(long)=3D=3D4 */<br> +=C2=A0 =C2=A0 =C2=A0 =C2=A0#define INTPTRTYPE short<br> +=C2=A0 #endif<br> +#endif<br> +<br> =C2=A0/* Nonzero if X is not aligned on a "long" boundary.<br> =C2=A0 * This macro is used to skip a few bytes to find an aligned pointer.= <br> =C2=A0 * It's better to keep it as is even if _HAVE_HW_MISALIGNED_ACCES= S is enabled,<br> =C2=A0 * to avoid small performance penalties (if they are not zero).=C2=A0= */<br> -#define UNALIGNED_X(X) ((long)(X) & (sizeof (long) - 1))<br> +#define UNALIGNED_X(X) ((long)(INTPTRTYPE)(X) & (sizeof (long) - 1))<b= r> <br> =C2=A0#ifdef _HAVE_HW_MISALIGNED_ACCESS<br> =C2=A0/* Hardware performs unaligned operations with little<br> @@ -30,7 +44,8 @@ int __wcwidth (wint_t);<br> =C2=A0#else /* _HAVE_HW_MISALIGNED_ACCESS */<br> =C2=A0/* Nonzero if either X or Y is not aligned on a "long" boun= dary.=C2=A0 */<br> =C2=A0#define UNALIGNED_X_Y(X, Y) \<br> -=C2=A0 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long= ) - 1)))<br> +=C2=A0 (((long)(INTPTRTYPE)X & (sizeof (long) - 1)) | \<br> +=C2=A0 =C2=A0((long)(INTPTRTYPE)Y & (sizeof (long) - 1)))<br> =C2=A0#endif /* _HAVE_HW_MISALIGNED_ACCESS */<br> <br> =C2=A0/* How many bytes are copied each iteration of the word copy loop.=C2= =A0 */<br> -- <br> 2.54.0<br> <br> </blockquote></div> --0000000000009b6d23065725cc0c--