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

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

Re: gawk substr() problem


From: Aharon Robbins
Subject: Re: gawk substr() problem
Date: Tue, 19 Nov 2002 14:00:44 +0200

> Date: Tue, 19 Nov 2002 01:39:50 -0800 (PST)
> From: Paul Eggert <address@hidden>
>
> > Date: Tue, 19 Nov 2002 09:37:12 +0100
> > From: Stepan Kasal <address@hidden>
>
> > > Alas, --lint catches the zero length case as well; this is a bug,
> > > since it's quite reasonable to compute a zero-length substring ...
> > 
> > >   $ gawk --lint 'BEGIN {print substr("x", 1, 0);}' </dev/null
> > >   gawk: cmd. line:1: warning: substr: length 0 is <= 0
> > 
> > IMHO it's very similar to 
> > 
> > >   $ gawk --lint 'BEGIN {print substr("x", 1, 4);}' </dev/null
> > >   gawk: cmd. line:1: warning: substr: length 4 at start index 1 exceeds 
> > > length of first argument (1)
>
> No, it's not similar.  It's quite reasonable for a program to do the
> former, but the latter is less resonable.
>
> 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.

My take is that asking for a zero-length substring is "most likely"
to be unintentional, and therefore I generate a warning.  If it's ok,
then the programmer can ignore the warning.  If it's not, then I've
caught something for someone and saved them some debugging hassle later.

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

Arnold




reply via email to

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