Commit a02bb3d9

a02bb3d902ddMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

regexp: dynamic alternation array (lift hardcoded 64-alt limit)

compile_alt() used a fixed-size 64-element alt_starts[] array, so any pattern with more than 64 top-level alternatives ("a|b|c|...") failed to compile with "too many alternatives". The repro from #777 builds a 1000-alt pattern via parts.join("|") and hits this immediately.

Promote alt_starts to a heap buffer that grows as alternatives are parsed, doubling capacity on each overflow. The remaining hard ceiling is the uint16_t SPLIT/JMP offset field (~65K instructions); surface that as an explicit compile_error so it fails gracefully rather than silently truncating to 16 bits.

Refs #777

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
a02bb3d902dd
Parent
1be306489136

lib/regexp/re_compile.cmodifiedView file

@@ -811,30 +811,40 @@
811811 insert a SPLIT before it by shifting code. */
812812
813813 /* Collect all alternatives, then emit SPLIT chain at the end.
814- This avoids insert_inst offset corruption for multi-way alternation. */
815- uint32_t alt_starts[64]; /* start positions of each alternative */
816- int num_alts = 0;
814+ This avoids insert_inst offset corruption for multi-way alternation.
815+ alt_starts grows dynamically; the only ceiling is the offset field's
816+ uint16_t width (~65535), enforced later when the SPLIT chain is
817+ wired up. Issue #777. */
818+ uint32_t alt_cap = 64;
819+ uint32_t *alt_starts = (uint32_t *)malloc(sizeof(uint32_t) * alt_cap);
820+ if (!alt_starts) compile_error(c, "out of memory");
821+ uint32_t num_alts = 0;
817822 alt_starts[num_alts++] = alt_start;
818823
819824 while (peek(c) == '|') {
820825 next_char(c);
821826 emit(c, RE_JMP, 0, 0); /* placeholder: jump to end */
827+ if (num_alts >= alt_cap) {
828+ alt_cap *= 2;
829+ uint32_t *grown = (uint32_t *)realloc(alt_starts, sizeof(uint32_t) * alt_cap);
830+ if (!grown) { free(alt_starts); compile_error(c, "out of memory"); }
831+ alt_starts = grown;
832+ }
822833 alt_starts[num_alts++] = c->code_len;
823- if (num_alts >= 64) compile_error(c, "too many alternatives");
824834 compile_seq(c);
825835 }
826836
827- if (num_alts <= 1) return; /* shouldn't happen, but safety */
837+ if (num_alts <= 1) { free(alt_starts); return; }
828838
829839 /* Now insert SPLIT chain before the alternatives.
830840 For n alternatives: n-1 SPLIT instructions, each pointing to
831841 their respective alternative. */
832- uint32_t split_count = (uint32_t)(num_alts - 1);
842+ uint32_t split_count = num_alts - 1;
833843 /* Insert split_count instructions at alt_starts[0] */
834844 for (uint32_t i = 0; i < split_count; i++) {
835845 insert_inst(c, alt_starts[0], RE_JMP, 0, 0); /* placeholder */
836846 /* adjust all alt_starts by +1 due to insertion */
837- for (int j = 0; j < num_alts; j++) {
847+ for (uint32_t j = 0; j < num_alts; j++) {
838848 alt_starts[j]++;
839849 }
840850 }
@@ -842,18 +852,28 @@
842852 /* Now set up SPLIT chain: each SPLIT tries next instruction or jumps to alt */
843853 for (uint32_t i = 0; i < split_count; i++) {
844854 uint32_t pos = alt_starts[0] - split_count + i;
855+ uint32_t target = alt_starts[i + 1];
856+ if (target > 0xFFFF) {
857+ free(alt_starts);
858+ compile_error(c, "regex too large (alternation offset overflow)");
859+ }
845860 c->code[pos].op = RE_SPLIT;
846861 c->code[pos].a = 0;
847- c->code[pos].offset = (uint16_t)alt_starts[i + 1];
862+ c->code[pos].offset = (uint16_t)target;
848863 }
849864
850865 /* Patch JMPs (they are right before each alt_starts[1..n-1]) to point to end */
851866 uint32_t end = c->code_len;
852- for (int i = 1; i < num_alts; i++) {
867+ if (end > 0xFFFF) {
868+ free(alt_starts);
869+ compile_error(c, "regex too large (end offset overflow)");
870+ }
871+ for (uint32_t i = 1; i < num_alts; i++) {
853872 uint32_t jmp_pos = alt_starts[i] - 1;
854873 c->code[jmp_pos].op = RE_JMP;
855874 c->code[jmp_pos].offset = (uint16_t)end;
856875 }
876+ free(alt_starts);
857877 }
858878
859879 /*