Bug 143375 - [patch] awk(1) trashes memory with regexp and ^ anchor
Summary: [patch] awk(1) trashes memory with regexp and ^ anchor
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 8.0-STABLE
Hardware: Any Any
: Normal Affects Only Me
Assignee: Warner Losh
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-30 17:30 UTC by Mikolaj Golub
Modified: 2021-07-10 17:09 UTC (History)
0 users

See Also:


Attachments
file.diff (382 bytes, patch)
2010-01-30 17:30 UTC, Mikolaj Golub
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Mikolaj Golub 2010-01-30 17:30:01 UTC
This problem with awk(1) regexp and ^ anchor trashing memory was reported to NetBSD by Nicolas Joly and it was fixed there.

http://www.netbsd.org/cgi-bin/query-pr-single.pl?number=40689

This script trashes the memory:

awk '/^root:/' /etc/passwd

Fix: See the attached patch adopted from NetBSD (PR/40689: Nicolas Joly: awk(1) trashes memory with RE and ^ anchor. Another place to special-case HAT.).

Patch attached with submission follows:
How-To-Repeat: zhuzha:/usr/src/contrib/one-true-awk% CFLAGS=-g make

zhuzha:/usr/src/contrib/one-true-awk% gdb a.out
GNU gdb 6.1.1 [FreeBSD]
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-marcel-freebsd"...
(gdb) b b.c:927
Breakpoint 1 at 0x804e15b: file b.c, line 927.
(gdb) run '/^root:/' /etc/passwd
Starting program: /usr/src/contrib/one-true-awk/a.out '/^root:/' /etc/passwd

Breakpoint 1, cgoto (f=0x28239000, s=2, c=261) at b.c:927
927             f->gototab[s][c] = f->curstat;
(gdb) pt f->gototab
type = unsigned char [32][259]
(gdb) p c
$1 = 261
(gdb) 
(gdb) list b.c:927
922             xfree(f->posns[f->curstat]);
923             if ((p = (int *) calloc(1, (setcnt+1)*sizeof(int))) == NULL)
924                     overflo("out of space in cgoto");
925
926             f->posns[f->curstat] = p;
927             f->gototab[s][c] = f->curstat;
928             for (i = 0; i <= setcnt; i++)
929                     p[i] = tmpset[i];
930             if (setvec[f->accept])
931                     f->out[f->curstat] = 1;
Comment 1 Pedro F. Giffuni freebsd_committer freebsd_triage 2017-10-08 17:43:02 UTC
Warner is taking some awk changes from NetBSD.
Comment 2 Eitan Adler freebsd_committer freebsd_triage 2018-05-28 19:42:49 UTC
batch change:

For bugs that match the following
-  Status Is In progress 
AND
- Untouched since 2018-01-01.
AND
- Affects Base System OR Documentation

DO:

Reset to open status.


Note:
I did a quick pass but if you are getting this email it might be worthwhile to double check to see if this bug ought to be closed.
Comment 3 Warner Losh freebsd_committer freebsd_triage 2021-07-08 01:50:41 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=f39dd6a9784467f0db5886012b3f4b13899be6b8

commit f39dd6a9784467f0db5886012b3f4b13899be6b8
Merge: 7cd22ac43418 746b7396bb3e
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2021-07-07 23:30:35 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2021-07-08 01:25:43 +0000

    one-true-awk: import 20210221 (1e4bc42c53a1) which fixes a number of bugs

    Import the latest bsd-features branch of the one-true-awk upstream:

    o Move to bison for $YACC
    o Set close-on-exec flag for file and pipe redirects that aren't std*
    o lots of little fixes to modernize ocde base
    o free sval member before setting it
    o fix a bug where a{0,3} could match aaaa
    o pull in systime and strftime from NetBSD awk
    o pull in fixes from {Net,Free,Open}BSD (normalized our code with them)
    o add BSD extensions and, or, xor, compl, lsheift, rshift (mostly a nop)

    Also revert a few of the trivial FreeBSD changes that were done slightly
    differently in the upstreaming process. Also, our PR database may have
    been mined by upstream for these fixes, and Mikolaj Golub may deserve
    credit for some of the fixes in this update.

    Suggested by:           Mikolaj Golub <to.my.trociny@gmail.com>
    PR:                     143363,143365,143368,143369,143373,143375,214782
    Sponsored by:           Netflix

 contrib/one-true-awk/ChangeLog                     | 108 +++
 contrib/one-true-awk/FIXES                         | 261 +++++-
 contrib/one-true-awk/REGRESS                       |   4 +
 contrib/one-true-awk/awk.1                         | 159 +++-
 contrib/one-true-awk/awk.h                         |  67 +-
 contrib/one-true-awk/awkgram.y                     |  48 +-
 contrib/one-true-awk/b.c                           | 438 +++++++---
 .../one-true-awk/bugs-fixed/missing-precision.ok   |   2 +-
 contrib/one-true-awk/bugs-fixed/negative-nf.ok     |   2 +-
 contrib/one-true-awk/lex.c                         |  77 +-
 contrib/one-true-awk/lib.c                         | 327 +++++---
 contrib/one-true-awk/main.c                        | 163 ++--
 contrib/one-true-awk/makefile                      |  74 +-
 contrib/one-true-awk/maketab.c                     |  66 +-
 contrib/one-true-awk/parse.c                       |  29 +-
 contrib/one-true-awk/proctab.c                     | 202 ++---
 contrib/one-true-awk/proto.h                       |  41 +-
 contrib/one-true-awk/run.c                         | 918 ++++++++++++++-------
 contrib/one-true-awk/tran.c                        | 164 ++--
 usr.bin/awk/Makefile                               |  10 +-
 20 files changed, 2192 insertions(+), 968 deletions(-)
Comment 4 commit-hook freebsd_committer freebsd_triage 2021-07-10 17:09:31 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=666abb0888d277e82c6468851e015798e9a7629f

commit 666abb0888d277e82c6468851e015798e9a7629f
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2021-07-07 23:30:35 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2021-07-10 17:07:26 +0000

    one-true-awk: import 20210221 (1e4bc42c53a1) which fixes a number of bugs

    Import the latest bsd-features branch of the one-true-awk upstream:

    o Move to bison for $YACC
    o Set close-on-exec flag for file and pipe redirects that aren't std*
    o lots of little fixes to modernize ocde base
    o free sval member before setting it
    o fix a bug where a{0,3} could match aaaa
    o pull in systime and strftime from NetBSD awk
    o pull in fixes from {Net,Free,Open}BSD (normalized our code with them)
    o add BSD extensions and, or, xor, compl, lsheift, rshift (mostly a nop)

    Also revert a few of the trivial FreeBSD changes that were done slightly
    differently in the upstreaming process. Also, our PR database may have
    been mined by upstream for these fixes, and Mikolaj Golub may deserve
    credit for some of the fixes in this update.

    Suggested by:           Mikolaj Golub <to.my.trociny@gmail.com>
    PR:                     143363, 143365, 143368, 143369, 143373, 143375, 214783
    Sponsored by:           Netflix

    (cherry picked from commit f39dd6a9784467f0db5886012b3f4b13899be6b8)

 contrib/one-true-awk/ChangeLog                     | 108 +++
 contrib/one-true-awk/FIXES                         | 261 +++++-
 contrib/one-true-awk/REGRESS                       |   4 +
 contrib/one-true-awk/awk.1                         | 159 +++-
 contrib/one-true-awk/awk.h                         |  67 +-
 contrib/one-true-awk/awkgram.y                     |  48 +-
 contrib/one-true-awk/b.c                           | 438 +++++++---
 .../one-true-awk/bugs-fixed/missing-precision.ok   |   2 +-
 contrib/one-true-awk/bugs-fixed/negative-nf.ok     |   2 +-
 contrib/one-true-awk/lex.c                         |  77 +-
 contrib/one-true-awk/lib.c                         | 327 +++++---
 contrib/one-true-awk/main.c                        | 163 ++--
 contrib/one-true-awk/makefile                      |  74 +-
 contrib/one-true-awk/maketab.c                     |  66 +-
 contrib/one-true-awk/parse.c                       |  29 +-
 contrib/one-true-awk/proctab.c                     | 202 ++---
 contrib/one-true-awk/proto.h                       |  41 +-
 contrib/one-true-awk/run.c                         | 918 ++++++++++++++-------
 contrib/one-true-awk/tran.c                        | 164 ++--
 usr.bin/awk/Makefile                               |  10 +-
 20 files changed, 2192 insertions(+), 968 deletions(-)