bug-gnu-utils
[Top][All Lists]
Advanced

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

Re: gawk substr() problem


From: Paul Eggert
Subject: Re: gawk substr() problem
Date: Wed, 20 Nov 2002 14:05:15 -0800

> Date: Tue, 19 Nov 2002 14:00:44 +0200
> From: Aharon Robbins <address@hidden>

> > For example, the following is quite reasonable code:
> >
> >         match(s, "^[0-9]*")
> >         leading_digits_if_any = substr(s, RSTART, RLENGTH)
> >
> > but this code generates the warning if s happens to have no leading
> > digits.
> 
> And in turn, maybe that indicates a bug: if the string is supposed to
> match the pattern and it doesn't, then there's a problem.

But the string _does_ match this pattern.  The pattern is supposed to
grab the leading digits, if any.  If there are no leading digits, then
the pattern matches the empty string.

> My take is that asking for a zero-length substring is "most likely"
> to be unintentional,

Have you surveyed awk code to see whether your intuition is correct?
My intuition is completely the opposite of yours here.

To check my intuition, I just went through the source code of gawk
itself, and 5 out of 6 uses that I found of zero-length substrings
were correct.  Here are the correct uses:

doc/gawkinet.texi:1709:    split(substr($2, 1, i-1), MENU, "[/:]")
doc/gawkinet.texi:1710:    split(substr($2, i+1), PARAM, "&") 
doc/gawk.texi:5199:          tmp = substr($0, 1, t - 1)
doc/gawk.texi:5200:          u = index(substr($0, t + 2), "*/")
doc/gawk.texi:5213:          $0 = tmp substr($0, u + 2)

Here is the only incorrect use, but it is so incorrect that it can
attempt to generate a _negative_-length substring (which I agree is
questionable):

doc/gawk.texi:5401:          tmp = substr($0, 10)

So, in this (admittedly limited) survey, it seems that zero-length
substrings are usually not an error.


> It's only a lint warning, and I am fairly well convinced that nobody
> uses --lint anyway.

I didn't know about --lint until recently, but if --lint generates
warnings for perfectly-reasonable things like zero-length substrings,
I don't wonder that nobody uses it.

Anyway, please take a look at the patch that I submitted recently
<http://mail.gnu.org/pipermail/bug-gnu-utils/2002-November/003706.html>
and see what you think.  It fixes other bugs too, but it also fixes
this zero-length substring problem.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]