From: millert Date: Wed, 15 Nov 2023 18:56:53 +0000 (+0000) Subject: fnematch: fix a bug that could result in extra chars being pushed back. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=af0a16b59a852763abf390189f9cd9b8e16265bf;p=openbsd fnematch: fix a bug that could result in extra chars being pushed back. From Arnold Robbins. https://github.com/onetrueawk/awk/pull/213 --- diff --git a/usr.bin/awk/b.c b/usr.bin/awk/b.c index ab0228b9c63..543fbf798c7 100644 --- a/usr.bin/awk/b.c +++ b/usr.bin/awk/b.c @@ -1,4 +1,4 @@ -/* $OpenBSD: b.c,v 1.46 2023/11/15 18:48:13 millert Exp $ */ +/* $OpenBSD: b.c,v 1.47 2023/11/15 18:56:53 millert Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved @@ -905,13 +905,10 @@ bool fnematch(fa *pfa, FILE *f, char **pbuf, int *pbufsize, int quantum) * (except for EOF's nullbyte, if present) and null * terminate the buffer. */ - do { - int ii; - for (ii = r.len; ii > 0; ii--) - if (buf[--k] && ungetc(buf[k], f) == EOF) - FATAL("unable to ungetc '%c'", buf[k]); - } while (k > i + patlen); - buf[k] = '\0'; + for (; r.len > 0; r.len--) + if (buf[--k] && ungetc(buf[k], f) == EOF) + FATAL("unable to ungetc '%c'", buf[k]); + buf[k-patlen] = '\0'; return true; } else