Commit 55acc57b
55acc57bcbecMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(regexp): reversed char class range raises (#778)
[z-a] (lo > hi) used to silently produce a class that matched
nothing. CRuby raises RegexpError "empty range in char class";
emit the same.
Closes #778
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 55acc57bcbec
- Parent
- 9565e9b1a8da
lib/regexp/re_compile.cmodifiedView file
@@ -292,6 +292,13 @@
292292
if (peek(c) == '-' && c->p + 1 < c->src_end && c->p[1] != ']') {293293
next_char(c); /* skip '-' */294294
uint32_t hi = read_class_atom(c);295+
/* Issue #778: reversed range like [z-a] is a hard error in296+
CRuby (RegexpError "empty range in char class"). Spinel297+
used to silently accept it and emit a class that matched298+
nothing. Raise instead. */299+
if (cp > hi) {300+
compile_error(c, "empty range in char class");301+
}295302
if (cp < 128 && hi < 128) {296303
class_set_range(cc, (uint8_t)cp, (uint8_t)hi);297304
}@@ -300,7 +307,7 @@
300307
Mixed ASCII/non-ASCII ranges are rare; stash the whole301308
span in the codepoint list (the bitmap covers ASCII only,302309
so a non-ASCII upper bound forces the codepoint path). */303-
if (cp <= hi) class_add_range(cc, cp, hi);310+
class_add_range(cc, cp, hi);304311
}305312
}306313
else {