[PATCH v1] Remove 'restrict' from 'nptr' in strtol(3)-like functions
Martin Uecker
muecker@gwdg.de
Fri Jul 5 15:34:55 GMT 2024
Am Freitag, dem 05.07.2024 um 17:23 +0200 schrieb Alejandro Colomar:
> Hi Martin,
>
> On Fri, Jul 05, 2024 at 05:02:15PM GMT, Martin Uecker wrote:
> > > But when the thing gets non-trivial, as in strtol(3), GCC misses the
> > > -Wrestrict diagnostic, as reported in
> > > <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112833>.
> > >
> > > Let's write a reproducer by altering the dumb.c program from above, with
> > > just another reference:
> > >
> > > int
> > > dumb2(int *restrict a, int *restrict *restrict ap)
> > > {
> > > // We don't access the objects
> > > return a == *ap;
> > > }
> > >
> > > int
> > > main(void)
> > > {
> > > int x = 3;
> > > int *xp = &x;
> > >
> > > return dumb2(&x, &xp);
> > > }
> > >
> > > GCC doesn't report anything bad here, even though it's basically the
> > > same as the program from above:
> > >
> > > $ cc -Wall -Wextra dumb2.c
> > > $
> >
> > strtol does have a "char * restrict * restrict" though, so the
> > situation is different. A "char **" and a "const char *"
> > shouldn't alias anyway.
>
> Pedantically, it is actually declared as 'char **restrict' (the inner
> one is not declared as restrict, even though it will be restricted,
> since there are no other unrestricted pointers).
>
> I've written functions that more closely resemble strtol(3), to show
> that in the end they all share the same issue regarding const-ness:
>
> $ cat d.c
> int d(const char *restrict ca, char *restrict a)
> {
> return ca > a;
> }
>
> int main(void)
> {
> char x = 3;
> char *xp = &x;
> d(xp, xp);
> }
> $ cc -Wall -Wextra d.c
> d.c: In function ‘main’:
> d.c:10:9: warning: passing argument 2 to ‘restrict’-qualified parameter aliases with argument 1 [-Wrestrict]
> 10 | d(xp, xp);
> | ^
>
> This trivial program causes a diagnostic. (Although I think the '>'
> should also cause a diagnostic!!)
>
> Let's add a reference, to resemble strtol(3):
>
> $ cat d2.c
> int d2(const char *restrict ca, char *restrict *restrict ap)
> {
> return ca > *ap;
> }
>
> int main(void)
> {
> char x = 3;
> char *xp = &x;
> d2(xp, &xp);
> }
> $ cc -Wall -Wextra d2.c
> $
>
> Why does this not cause a -Wrestrict diagnostic, while d.c does? How
> are these programs any different regarding pointer restrict-ness?
It would require data flow anaylsis to produce the diagnostic while
the first can simply be diagnosed by comparing arguments.
Martin
>
> > > Well, I don't know how to report that defect to WG14. If you help me,
> > > I'll be pleased to do so. Do they have a public mailing list or
> > > anything like that?
> >
> > One can submit clarification or change requests:
> >
> > https://www.open-std.org/jtc1/sc22/wg14/www/contributing.html
>
> Thanks! Will do. Anyway, I think this should be discussed in glibc/gcc
> in parallel, since it's clearly a missed diagnostic, and possibly a
> dangerous use of restrict if the compiler does any assumptions that
> shouldn't be done.
>
> Have a lovely day!
> Alex
>
More information about the Gcc
mailing list