Skip to content

Commit 851c879

Browse files
committed
Fix scan position truncation with fixed_anchor on 64-bit
adjust_registers_to_matched() narrowed 64-bit scan positions to int via onig_region_set(), so pos > INT_MAX produced negative match registers and getch/get_byte read out of bounds in fixed_anchor mode. Store the positions directly in the region instead.
1 parent a552ea6 commit 851c879

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

‎ext/strscan/strscan.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,11 +1183,16 @@ static void
11831183
adjust_registers_to_matched(struct strscanner *p)
11841184
{
11851185
onig_region_clear(&(p->regs));
1186+
if (onig_region_resize(&(p->regs), 1) != 0) return;
11861187
if (p->fixed_anchor_p) {
1187-
onig_region_set(&(p->regs), 0, (int)p->prev, (int)p->curr);
1188+
/* Store absolute positions without narrowing them to int: scan
1189+
* positions can exceed INT_MAX (64-bit builds). */
1190+
p->regs.beg[0] = p->prev;
1191+
p->regs.end[0] = p->curr;
11881192
}
11891193
else {
1190-
onig_region_set(&(p->regs), 0, 0, (int)(p->curr - p->prev));
1194+
p->regs.beg[0] = 0;
1195+
p->regs.end[0] = p->curr - p->prev;
11911196
}
11921197
}
11931198

0 commit comments

Comments
 (0)