On Fri, 2024-07-05 at 17:23 +0200, Alejandro Colomar wrote:
> > 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).
So how's the following implementation of strtol (10-based, no negative
number handling, no overflow handling, ASCII-only) wrong?
long int my_strtol(const char *restrict nptr, char **restrict endptr)
{
long ret = 0;
while (isdigit(*nptr))
ret = ret * 10 + (*nptr++ - '0');
*endptr = (char *)nptr;
return ret;
}
There's no dumb thing, there's no restrict violation (unless it's called
in a stupid way, see below), and there **shouldn't** be a -Wrestrict
warning.
If you do
char *x = NULL;
strtol((char *)&x, &x, 10);
it'll violate restrict. Nobody sane should write this, and it's warned
anyway:
t.c: In function 'main':
t.c:6:28: warning: passing argument 2 to 'restrict'-qualified parameter
aliases with argument 1 [-Wrestrict]
6 | strtol((char *)&x, &x, 10);
| ~~~~~~~~~~ ^~
--
Xi Ruoyao <[email protected]>
School of Aerospace Science and Technology, Xidian University