From 05014f5b9e468730925c0452a3d3399ea4353fd3 Mon Sep 17 00:00:00 2001 From: Leonardo Taccari Date: Wed, 29 Aug 2018 18:06:33 +0200 Subject: [PATCH 1/7] avoid undefined behaviour when using ctype(3) functions in relex() Because NCHARS is (256+3) cc->cc_func(i) was called with 256, 257 and 258 as argument leading to possible undefined behaviour (at least on NetBSD with non-C locale (e.g. `en_US.UTF-8') this led to only honoring one `[:...:]' character class in bracket expressions). Fix #11 --- b.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/b.c b/b.c index 89a7841..c248902 100644 --- a/b.c +++ b/b.c @@ -27,6 +27,7 @@ THIS SOFTWARE. #define DEBUG #include +#include #include #include #include @@ -823,7 +824,7 @@ int relex(void) /* lexical analyzer for reparse */ if (cc->cc_name != NULL && prestr[1 + cc->cc_namelen] == ':' && prestr[2 + cc->cc_namelen] == ']') { prestr += cc->cc_namelen + 3; - for (i = 0; i < NCHARS; i++) { + for (i = 0; i <= UCHAR_MAX; i++) { if (!adjbuf((char **) &buf, &bufsz, bp-buf+1, 100, (char **) &bp, "relex2")) FATAL("out of space for reg expr %.10s...", lastre); if (cc->cc_func(i)) { From 9dbd1f1de31ea9a805d1095f5586fb146a1b6a9e Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Fri, 25 Jan 2019 12:56:06 +0200 Subject: [PATCH 2/7] Make getline POSIX compliant w.r.t. numeric strings. --- ChangeLog | 5 +++++ bugs-fixed/README | 5 ++++- bugs-fixed/getline-numeric.awk | 6 ++++++ bugs-fixed/getline-numeric.bad | 6 ++++++ bugs-fixed/getline-numeric.in | 2 ++ bugs-fixed/getline-numeric.ok | 6 ++++++ run.c | 8 ++++++++ 7 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 bugs-fixed/getline-numeric.awk create mode 100644 bugs-fixed/getline-numeric.bad create mode 100644 bugs-fixed/getline-numeric.in create mode 100644 bugs-fixed/getline-numeric.ok diff --git a/ChangeLog b/ChangeLog index 59d4b07..3cf5002 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2019-01-25 Arnold D. Robbins + + * run.c (awkgetline): Check for numeric value in all getline + variants. See the numeric-getline.* files in bugs-fixed directory. + 2018-08-29 Arnold D. Robbins * REGRESS: Check for existence of a.out. If not there, run diff --git a/bugs-fixed/README b/bugs-fixed/README index 9c644f9..2f27c10 100644 --- a/bugs-fixed/README +++ b/bugs-fixed/README @@ -51,4 +51,7 @@ array passed as the second argument, then split() would previously read from the freed memory and possibly produce incorrect results (depending on the system's malloc()/free() behaviour.) - +15. getline-numeric: The `getline xx < file' syntax did not check if +values were numeric, in discordance from POSIX. Test case adapted from +one posted by Ben Bacarisse in comp.lang.awk, +January 2019. diff --git a/bugs-fixed/getline-numeric.awk b/bugs-fixed/getline-numeric.awk new file mode 100644 index 0000000..5571a95 --- /dev/null +++ b/bugs-fixed/getline-numeric.awk @@ -0,0 +1,6 @@ +{ + print $0, ($0 <= 50 ? "<=" : ">"), 50 + getline dd < ARGV[1] + print dd, (dd <= 50 ? "<=" : ">"), 50 + if (dd == $0) print "same" +} diff --git a/bugs-fixed/getline-numeric.bad b/bugs-fixed/getline-numeric.bad new file mode 100644 index 0000000..5247bfc --- /dev/null +++ b/bugs-fixed/getline-numeric.bad @@ -0,0 +1,6 @@ +120 > 50 +120 <= 50 +same +120 > 50 +120 <= 50 +same diff --git a/bugs-fixed/getline-numeric.in b/bugs-fixed/getline-numeric.in new file mode 100644 index 0000000..b635013 --- /dev/null +++ b/bugs-fixed/getline-numeric.in @@ -0,0 +1,2 @@ +120 +120 diff --git a/bugs-fixed/getline-numeric.ok b/bugs-fixed/getline-numeric.ok new file mode 100644 index 0000000..901a4d9 --- /dev/null +++ b/bugs-fixed/getline-numeric.ok @@ -0,0 +1,6 @@ +120 > 50 +120 > 50 +same +120 > 50 +120 > 50 +same diff --git a/run.c b/run.c index ce30e93..2dfb3e6 100644 --- a/run.c +++ b/run.c @@ -425,6 +425,10 @@ Cell *awkgetline(Node **a, int n) /* get next line from specific input */ } else if (a[0] != NULL) { /* getline var sval)) { + x->fval = atof(x->sval); + x->tval |= NUM; + } tempfree(x); } else { /* getline sval)) { + x->fval = atof(x->sval); + x->tval |= NUM; + } tempfree(x); } } From 9206c643e89e4d9d090293f83fcf36b782c7382c Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Fri, 25 Jan 2019 13:02:19 +0200 Subject: [PATCH 3/7] Update FIXES. --- FIXES | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/FIXES b/FIXES index 909afb7..66b524b 100644 --- a/FIXES +++ b/FIXES @@ -25,6 +25,16 @@ THIS SOFTWARE. This file lists all bug fixes, changes, etc., made since the AWK book was sent to the printers in August, 1987. +Jan 25, 2019: + Make getline handle numeric strings properly in all cases. + (Thanks, Arnold.) + +Jan 21, 2019: + Merged a number of small fixes from GitHub pull requests. + Thanks to GitHub users Arnold Robbins (arnoldrobbins), + Cody Mello (melloc) and Christoph Junghans (junghans). + PR numbers: 13-21, 23, 24, 27. + Oct 25, 2018: Added test in maketab.c to prevent generating a proctab entry for YYSTYPE_IS_DEFINED. It was harmless but some gcc settings From 778d21f73b526874a24a56120657e4db6a34d82e Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sat, 26 Jan 2019 20:50:31 +0200 Subject: [PATCH 4/7] Simplify getline-numeric input and results. --- bugs-fixed/getline-numeric.bad | 3 --- bugs-fixed/getline-numeric.in | 1 - bugs-fixed/getline-numeric.ok | 3 --- 3 files changed, 7 deletions(-) diff --git a/bugs-fixed/getline-numeric.bad b/bugs-fixed/getline-numeric.bad index 5247bfc..d911c77 100644 --- a/bugs-fixed/getline-numeric.bad +++ b/bugs-fixed/getline-numeric.bad @@ -1,6 +1,3 @@ 120 > 50 120 <= 50 same -120 > 50 -120 <= 50 -same diff --git a/bugs-fixed/getline-numeric.in b/bugs-fixed/getline-numeric.in index b635013..52bd8e4 100644 --- a/bugs-fixed/getline-numeric.in +++ b/bugs-fixed/getline-numeric.in @@ -1,2 +1 @@ 120 -120 diff --git a/bugs-fixed/getline-numeric.ok b/bugs-fixed/getline-numeric.ok index 901a4d9..f7efd3d 100644 --- a/bugs-fixed/getline-numeric.ok +++ b/bugs-fixed/getline-numeric.ok @@ -1,6 +1,3 @@ 120 > 50 120 > 50 same -120 > 50 -120 > 50 -same From f25e845cf7a09d7b43c7a07e535a2e8d7f63aee1 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sat, 26 Jan 2019 21:55:03 +0200 Subject: [PATCH 5/7] Update version string in main.c. --- ChangeLog | 4 ++++ main.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 3cf5002..7516cd6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2019-01-26 Arnold D. Robbins + + * main.c (version): Updated. + 2019-01-25 Arnold D. Robbins * run.c (awkgetline): Check for numeric value in all getline diff --git a/main.c b/main.c index 1c38a1e..b5c7c9f 100644 --- a/main.c +++ b/main.c @@ -22,7 +22,7 @@ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ****************************************************************/ -const char *version = "version 20180827"; +const char *version = "version 20190125"; #define DEBUG #include From cc165f4be9a6ea8f77ff4533786bcddde1d13fdf Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 29 Jan 2019 17:20:00 -0800 Subject: [PATCH 6/7] maketab: support build systems with read-only source. If your generated files are considered outputs that live elsewhere, you need a way to tell maketab where ytab.h actually is. Specifically, I'm trying to avoid checking in generated files in Android's AOSP tree's copy of one-true-awk. --- makefile | 2 +- maketab.c | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/makefile b/makefile index 3f3c3c2..a4f1b8a 100644 --- a/makefile +++ b/makefile @@ -67,7 +67,7 @@ y%.c y%.h: awk.h proto.h awkgram.y ytab.h: ytab.c proctab.c: maketab - ./maketab >proctab.c + ./maketab ytab.h >proctab.c maketab: ytab.h maketab.c $(CC) $(CFLAGS) maketab.c -o maketab diff --git a/maketab.c b/maketab.c index bb8e317..dbe3d24 100644 --- a/maketab.c +++ b/maketab.c @@ -125,8 +125,12 @@ int main(int argc, char *argv[]) for (i = SIZE; --i >= 0; ) names[i] = ""; - if ((fp = fopen("ytab.h", "r")) == NULL) { - fprintf(stderr, "maketab can't open ytab.h!\n"); + if (argc != 2) { + fprintf(stderr, "usage: maketab YTAB_H\n"); + exit(1); + } + if ((fp = fopen(argv[1], "r")) == NULL) { + fprintf(stderr, "maketab can't open %s!\n", argv[1]); exit(1); } printf("static char *printname[%d] = {\n", SIZE); From 785d10884a09fdb77583ca9499a52586e91e74d2 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 29 Jan 2019 17:27:07 -0800 Subject: [PATCH 7/7] Add .gitignore. --- .gitignore | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f469d21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +a.out +maketab +proctab.c +ytab.c +ytab.h +*.o