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: Mon, 18 Nov 2002 15:31:13 +0200

Greetings. Re this:

> Date: Tue, 12 Nov 2002 15:55:17 +0100
> From: Stepan Kasal <address@hidden>
> To: address@hidden
> Subject: gawk substr() problem
>
> Hello,
>       I've discovered a bug in GNU awk (tried with 3.1.1 and 3.0.4):
>
> substr(str, start, len)
>
>   - POSIX doesn't say what to do if `start' is less or equal to 0.
>       I'd say it should be an error.  GNU awk manual says that the null
>       string is returned.
>
> when you call substr() in gawk with start <= 0, it behaves as if start
> was 0, which differs from the above specification.
>
> $ gawk-3.1.1/gawk 'BEGIN{print substr("abc",-5,2)}'
> ab
> $

Gawk follows traditional Unix awk, which treats it as if it was zero.
The manual needs to be fixed. (Just done in my tree for the next patch.)

Gawk does warn you about such things if you use --lint:

        $ gawk --lint 'BEGIN{print substr("abc",-5,2)}'
        gawk: cmd. line:1: warning: substr: start index -5 is invalid, using 1
        ab

> Wouldn't it be best to fix both docs and code and issue an error message
> if `start' is <= 0.
> It would be particularily helpful for  start == 0  as this indicates that
> the programmer has forgotten that awk strings are indexed from 1.
>
> (Willing to make a patch, when the king's ruling is known.  :-)
>
>   - POSIX doesn't say what to do if `len' is negative.
>       GNU awk returns the null string.
>
> This is not a bug but a feature request: wouldn't it be better to issue
> an error message instead?

For both of these, gawk does what Unix does, which is fail silently.
--lint does catch the negative length case too:

        $ gawk --lint 'BEGIN{print substr("abc",2,-2)}'
        gawk: cmd. line:1: warning: substr: length -2 is <= 0

So, no code changes needed, and I'll fix the doc.

Thanks,

Arnold




reply via email to

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