On Tue, 31 Mar 2026 14:40:49 +0200, Renaud Allard wrote:

> The regex repetition count parser in awk(1) does not limit the
> value of the bound in expressions like {999999999}.
>
> The repeat count is parsed at b.c:1463 as:
>
>   num = 10 * num + c - '0';
>
> with no upper bound check.  This value is then used in
> replace_repeat() to compute a buffer size:
>
>   size += atomlen*(firstnum-1);
>
> With large repeat counts, the multiplication overflows signed int.
> If the result wraps to a small positive value, malloc allocates a
> small buffer and the subsequent memcpy loop writes past it, causing
> a heap buffer overflow.
>
> Additionally, the num accumulation itself (10 * num + c) overflows
> signed int for values exceeding ~214 million.
>
> The 6-byte awk program /{00}/ triggers a SIGSEGV via this path.

While I agree that your patch for awk makes sense, the crash from
/{00}/ is actually a different (distinct) bug.  The repeat code
expects the last atom to be part of the same string as the repeition.
That assumption is broken if all we have is the repetition.  The
simplest fix for it is:

 - todd

Index: b.c
===================================================================
RCS file: /cvs/src/usr.bin/awk/b.c,v
diff -u -p -u -r1.55 b.c
--- b.c 5 Feb 2025 20:32:56 -0000       1.55
+++ b.c 2 Apr 2026 02:41:43 -0000
@@ -1214,6 +1214,9 @@ replace_repeat(const uschar *reptok, int
 static int repeat(const uschar *reptok, int reptoklen, const uschar *atom,
                  int atomlen, int firstnum, int secondnum)
 {
+       if (atom == NULL)
+               return 0;
+
        /*
           In general, the repetition specifier or "bound" is replaced here
           by an equivalent ERE string, repeating the immediately previous atom

Reply via email to