develooper Front page | perl.perl5.porters | Postings from February 2015

[perl #123814] grok_atou() for regexp quantifiers

Thread Previous
From:
Hugo van der Sanden via RT
Date:
February 16, 2015 11:44
Subject:
[perl #123814] grok_atou() for regexp quantifiers
Message ID:
rt-4.0.18-9412-1424087046-374.123814-15-0@perl.org
On Sun Feb 15 21:37:46 2015, demerphq wrote:
> Am i understanding this to mean that grok_atou will turn any "number"
> starting with 0 that is not 0 itself into MAX_UV?

Yes, that's what it is documented to do. But it's almost as if not everybody that uses it fully reads its documentation before using it.

> That sounds less than awesome. It also makes me wonder if people
> really are using this in the wild?
> 
> Why dont we just ignore the leading zeros?

Presumably because that gives equally surprising results, whether you end up treating 010 as 10 or 8.

There are inputs that it considers errors, and it has a mechanism to report those errors; however a high proportion of callers are ignoring that.

I think the whole interface of it is encouraging bugs. I think we need something more like the grok_bslash_x style - to return a boolean success and use pointer arguments for the resulting values - since it becomes a bit more obvious if the caller isn't checking that the parse was successful, particularly in combination with -Wunused-result.

I think we'd also benefit from specific functions (or macros) for each of the different integer types/sizes we might target, so that the range checks happen in just one place and we reduce the problem of blind casting.

Ignoring XS/APItest and code duplicataion in ext/re, I found some 29 callers; a quick glance at those suggest around 25 of them are dubious.

In the meantime, we might want to consider the doc patch below.

Hugo

--- a/numeric.c
+++ b/numeric.c
@@ -1035,7 +1035,7 @@ Perl_grok_number_flags(pTHX_ const char *pv, STRLEN len, U
 /*
 =for apidoc grok_atou
 
-grok_atou is a safer replacement for atoi and strtol.
+grok_atou is a replacement for atoi and strtol.
 
 grok_atou parses a C-style zero-byte terminated string, looking for
 a decimal unsigned integer.
(END)

---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=123814

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About