awkgetline: do not access unitialized data on EOF
authormillert <millert@openbsd.org>
Mon, 1 Nov 2021 18:28:24 +0000 (18:28 +0000)
committermillert <millert@openbsd.org>
Mon, 1 Nov 2021 18:28:24 +0000 (18:28 +0000)
getrec() returns 0 on EOF and leaves the contents of buf unchanged.
From https://github.com/onetrueawk/awk/pull/134

usr.bin/awk/run.c

index d7570bc..128a7ea 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: run.c,v 1.69 2020/12/09 20:00:11 millert Exp $        */
+/*     $OpenBSD: run.c,v 1.70 2021/11/01 18:28:24 millert Exp $        */
 /****************************************************************
 Copyright (C) Lucent Technologies 1997
 All Rights Reserved
@@ -448,13 +448,15 @@ Cell *awkgetline(Node **a, int n) /* get next line from specific input */
                        n = getrec(&record, &recsize, true);
                else {                  /* getline var */
                        n = getrec(&buf, &bufsize, false);
-                       x = execute(a[0]);
-                       setsval(x, buf);
-                       if (is_number(x->sval, & result)) {
-                               x->fval = result;
-                               x->tval |= NUM;
+                       if (n > 0) {
+                               x = execute(a[0]);
+                               setsval(x, buf);
+                               if (is_number(x->sval, & result)) {
+                                       x->fval = result;
+                                       x->tval |= NUM;
+                               }
+                               tempfree(x);
                        }
-                       tempfree(x);
                }
        }
        setfval(r, (Awkfloat) n);