[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#222189: libc6-dev: strtol() argument type mismatch (endptr should be const char **)



Package: libc6-dev
Version: 2.3.2.ds1-10
Severity: minor

I'd like to suggest a change to one of the arguments of the functions
in the strtol() family. The change can be done without breaking either
source or binary compatibility to either glibc itself, or any other C
libraries. I will only talk about strtol() in this bug report, but my
comments apply to all other functions in the same family.

Let us examine strtols function prototype (from the manual page):

long int strtol(const char *nptr, char **endptr, int base);

The first argument, nptr, points to the start of the string to be
converted, and endptr (optionally) points to a character pointer.

After calling strtol, *endptr is set to where the conversion ended,
which by definition is inside the string pointed to by nptr.

We here see that there is a type mismatch. Because nptr is defined as
const char *, it would be reasonable to type endptr as const char **.
It doesn't matter that *endptr is modified, the const in this case
applies to the actual characters inside the string, not to the
pointer.

Effectively, this means that the person using this function to parse
const char * strings is forced to cast the second argument to avoid a
warning, that is not valid in any case!

Changing the type of the endptr argument should not cause any problems
with binary compatibility -- and the only possible problem that could
arise with source compatibility would be the addition of a warning
(which could cause the program not to compile if -Werror was passed to
gcc.)

If nothing else, I think a note about this ideosyncracy should be made
in the manual page, under either NOTES or BUGS, reassuring the
programmer that casting a const char ** to char ** as argument to endptr
is safe.

-- System Information:
Debian Release: testing/unstable
Architecture: i386
Kernel: Linux tanya 2.4.20 #1 mån mar 31 19:30:02 CEST 2003 i686
Locale: LANG=sv_SE, LC_CTYPE=sv_SE

Versions of packages libc6-dev depends on:
ii  libc6                 2.3.2.ds1-10       GNU C Library: Shared libraries an
ii  linux-kernel-headers  2.5.999-test7-bk-9 Linux Kernel Headers for developme

-- no debconf information




Reply to: