[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: fatal: substr: length 0 is not >= 1
From: |
arnold |
Subject: |
Re: fatal: substr: length 0 is not >= 1 |
Date: |
Thu, 07 May 2020 00:39:08 -0600 |
User-agent: |
Heirloom mailx 12.5 7/5/10 |
Hi.
Thanks for your note.
The lint option checking that the length parameter is zero assumes
(as in this case) that the result comes from a runtime computation and
that the user wants a real substring and not an empty string. In such a
case, a warning that zero came in is appropriate.
-Lfatal makes things a little worse than intended, in that the
program exits immediately. Personally, I don't recommend running
with -Lfatal in production, but rather during development and testing.
If -Lfatal is the default, then you have to protect the substr()
call, as you've done.
Thanks,
Arnold
Roland Illig <address@hidden> wrote:
> Dear GNU Awk developers,
>
> In a real-world AWK program
> (https://github.com/NetBSD/pkgsrc/blob/8075fc03e305003a/mk/scripts/subst-identity.awk)
> I wrote the following code:
>
> # Tests whether a single "s,from,to," is an identity substitution.
> function is_identity_subst(
> s,
> len, i, sep, pat_from, pat_to, ch, subst
> ) {
> len = length(s);
> if (len < 6 || substr(s, 1, 1) != "s")
> return 0;
>
> sep = substr(s, 2, 1);
> i = 3;
> pat_to = "";
> while (i < len && substr(s, i, 1) != sep) {
> ch = identity_char(substr(s, i));
> if (ch == "")
> break;
> pat_to = pat_to substr(ch, 1, 1);
> i += length(ch);
> }
>
> # The next 2 lines are only needed for GNU Awk 5.0.1
> # in -Lfatal mode.
> if (pat_to == "")
> return 0;
>
> pat_from = substr(s, 3, i - 3);
>
> subst = "s" sep pat_from sep pat_to sep;
> return s == subst || s == subst "g";
> }
>
>
> I ran this program with -Lfatal, and it presented me this "error":
>
> fatal: substr: length 0 is not >= 1
>
> I think it is ok to have length 0, which would always return the null
> string. In my mind, only negative lengths should trigger this error
> message. Was there a specific reason for making length 0 an error?
>
> I know that in the above code, I could have written the equivalent "i ==
> 3" instead. But that would have repeated the number 3, and I didn't want
> that. I thought was that the expression 'pat_to == ""' expressed my
> thoughts more clearly.
>
> Best,
> Roland
>