strtoul pitfalls
Hi Alejandro,
reviewing the strtoul() calls in gnulib and gettext, I found several places where the original coder had fallen into one of these two pitfalls. I would find it good to mention both pitfalls more prominently in the man-pages (the first one is mentioned as a NOTE, should better be called a BUG).
* Leading whitespace and a sign are accepted by default. Often this is not desired.
To avoid this, the caller should pre-parse the initial portion of the string, up to the first digit.
* If values > MAX should lead to a diagnostic, they arise in two circumstances:
- errno = ERANGE,
- errno = 0, end == str, but value > MAX.
It is not obvious how to arrange the code, so as to get the same diagnostic, without code duplication.
Bruno
[1] https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=commitdiff;h=008ff109def9bdfdd04a12355d8f4a835045ae41
[2] https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=commitdiff;h=3df6da258c338e4fd165a2850bb8d4b0f39b6e9b
[3] https://git.savannah.gnu.org/gitweb/?p=gettext.git;a=commitdiff;h=b8ab4f2bef0ff18cf1ae749c1c81a359a40d0450
Alejandro Colomar
maintainerHi Bruno,
Quoted 13 lines · Bruno Haible
Hi Alejandro,
reviewing the
strtoul()calls in gnulib and gettext, I found several places where the original coder had fallen into one of these two pitfalls. I would find it good to mention both pitfalls more prominently in the man-pages (the first one is mentioned as a NOTE, should better be called a BUG).* Leading whitespace and a sign are accepted by default. Often this is not desired.
To avoid this, the caller should pre-parse the initial portion of the string, up to the first digit.
Quoted 6 lines
* If values > MAX should lead to a diagnostic, they arise in two circumstances:
- errno = ERANGE,
- errno = 0, end == str, but value > MAX.
It is not obvious how to arrange the code, so as to get the same diagnostic, without code duplication.
Agree. I think this page needs a long BUGS and CAVEATS pair of sections. I'll write something about it. Feel free to report more bugs about these APIs; this will help kill them soon. I'll present the resulting manual pages as part of the defense of my paper for C2y.
Have a lovely night!
Alex
Quoted 9 lines
Bruno
[1] https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=commitdiff;h=008ff109def9bdfdd04a12355d8f4a835045ae41
[2] https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=commitdiff;h=3df6da258c338e4fd165a2850bb8d4b0f39b6e9b
[3] https://git.savannah.gnu.org/gitweb/?p=gettext.git;a=commitdiff;h=b8ab4f2bef0ff18cf1ae749c1c81a359a40d0450
Signature · 1 line
<https://www.alejandro-colomar.es/>