Skip to content

Commit 9360647

Browse files
committed
patch 9.2.0131: potential buffer overflow in regdump()
Problem: Potential buffer overflow in regdump() Solution: Add the size to the compiled regular expression and ensure we don't read over the limit. Note: this is not a security issue, because regdump() is typically not compiled in any version of Vim, so should not affect anybody. supported by AI claude. Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 49b8d99 commit 9360647

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/regexp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ typedef struct
7373
char_u reganch;
7474
char_u *regmust;
7575
int regmlen;
76+
#ifdef DEBUG
77+
int regsz;
78+
#endif
7679
#ifdef FEAT_SYN_HL
7780
char_u reghasz;
7881
#endif

src/regexp_bt.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,6 +2497,9 @@ bt_regcomp(char_u *expr, int re_flags)
24972497
if (r == NULL)
24982498
return NULL;
24992499
r->re_in_use = FALSE;
2500+
#ifdef DEBUG
2501+
r->regsz = regsize;
2502+
#endif
25002503

25012504
// Second pass: emit code.
25022505
regcomp_start(expr, re_flags);
@@ -5200,11 +5203,11 @@ regdump(char_u *pattern, bt_regprog_T *r)
52005203
s = r->program + 1;
52015204
// Loop until we find the END that isn't before a referred next (an END
52025205
// can also appear in a NOMATCH operand).
5203-
while (op != END || s <= end)
5206+
while ((op != END || s <= end) && s < r->program + r->regsz)
52045207
{
52055208
op = OP(s);
52065209
fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); // Where, what.
5207-
next = regnext(s);
5210+
next = (s + 3 <= r->program + r->regsz) ? regnext(s) : NULL;
52085211
if (next == NULL) // Next ptr.
52095212
fprintf(f, "(0)");
52105213
else
@@ -5230,14 +5233,22 @@ regdump(char_u *pattern, bt_regprog_T *r)
52305233
s += 5;
52315234
}
52325235
s += 3;
5236+
if (op == MULTIBYTECODE)
5237+
{
5238+
fprintf(f, " mbc=%d", utf_ptr2char(s));
5239+
s += utfc_ptr2len(s);
5240+
}
52335241
if (op == ANYOF || op == ANYOF + ADD_NL
52345242
|| op == ANYBUT || op == ANYBUT + ADD_NL
52355243
|| op == EXACTLY)
52365244
{
52375245
// Literal string, where present.
52385246
fprintf(f, "\nxxxxxxxxx\n");
5239-
while (*s != NUL)
5240-
fprintf(f, "%c", *s++);
5247+
while (*s != NUL && s < r->program + r->regsz)
5248+
{
5249+
fprintf(f, "%c", *s);
5250+
s += utfc_ptr2len(s); // advance by full char including combining
5251+
}
52415252
fprintf(f, "\nxxxxxxxxx\n");
52425253
s++;
52435254
}

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ static char *(features[]) =
734734

735735
static int included_patches[] =
736736
{ /* Add new patch number below this line */
737+
/**/
738+
131,
737739
/**/
738740
130,
739741
/**/

0 commit comments

Comments
 (0)