bug-gawk
[Top][All Lists]
Advanced

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

[bug-gawk] FIELDWIDTHS can miscount the number of fields


From: Arnold Robbins
Subject: [bug-gawk] FIELDWIDTHS can miscount the number of fields
Date: Sat, 20 May 2017 23:11:18 +0300
User-agent: Heirloom mailx 12.5 6/20/10

This report is based on a discussion in comp.lang.awk.

gawk can miscalculate NF when using FIELDWIDTHS to parse data:

Given the program:

        $ cat x.awk
        BEGIN { FIELDWIDTHS = "2 3 4" }
        { print NF }

And the data:

        $ cat x.in
        12
        12345
        123456789

When run, we get:

        $ gawk -f x.awk x.in
        3
        3
        3

Ooops!

The following patch to gawk's master seems to fix the problem.

Andy - look ok to you?

Arnold
---------------------------
diff --git a/field.c b/field.c
index 608be7d..66153cb 100644
--- a/field.c
+++ b/field.c
@@ -777,7 +777,7 @@ fw_parse_field(long up_to,  /* parse only up to this field 
number */
                 * in practice.
                 */
                memset(&mbs, 0, sizeof(mbstate_t));
-               while (nf < up_to) {
+               while (nf < up_to && scan < end) {
                        if (nf >= fw->nf) {
                                *buf = end;
                                return nf;
@@ -788,7 +788,7 @@ fw_parse_field(long up_to,  /* parse only up to this field 
number */
                        scan += flen;
                }
        } else {
-               while (nf < up_to) {
+               while (nf < up_to && scan < end) {
                        if (nf >= fw->nf) {
                                *buf = end;
                                return nf;



reply via email to

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