[PATCH v1] Remove 'restrict' from 'nptr' in strtol(3)-like functions

Xi Ruoyao xry111@xry111.site
Fri Jul 5 13:38:21 GMT 2024


On Fri, 2024-07-05 at 15:03 +0200, Alejandro Colomar wrote:
> ISO C specifies these APIs as accepting a restricted pointer in their
> first parameter:
> 
> $ stdc c99 strtol
> long int strtol(const char *restrict nptr, char **restrict endptr, int base);
> $ stdc c11 strtol
> long int strtol(const char *restrict nptr, char **restrict endptr, int base);
> 
> However, it should be considered a defect in ISO C.  It's common to see
> code that aliases it:
> 
> 	char str[] = "10 20";
> 
> 	p = str;
> 	a = strtol(p, &p, 0);  // Let's ignore error handling for
> 	b = strtol(p, &p, 0);  // simplicity.

Why this is wrong?

During the execution of strtol() the only expression accessing the
object "p" is *endptr.  When the body of strtol() refers "nptr" it
accesses a different object, not "p".

And if this is really wrong you should report it to WG14 before changing
glibc.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University


More information about the Gcc mailing list