Commit 6d300505

6d300505ba93Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +1 added · ~2 modified

Description

Raise StringScanner::Error on invalid unscan

CRuby's unscan raises StringScanner::Error when there is no match record to rewind to: a double unscan, or unscan before any successful scan. Spinel's unscan silently succeeded, corrupting the scan position.

Guard unscan on the match flag and raise via sp_raise_cls before mutating state, so a failed unscan leaves the position intact. Register StringScanner_Error (the flattened StringScanner::Error) under StandardError in the codegen exception hierarchy so rescue StandardError and rescue Exception catch it, matching CRuby where StringScanner::Error < StandardError. rescue StringScanner::Error and a bare rescue already matched via name tagging.

Fixes #1265

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

Details

Commit
6d300505ba93
Parent
7a753885870f

lib/sp_strscan.cmodifiedView file

@@ -35,6 +35,7 @@
3535 extern size_t sp_ext_str_byte_len(const char *s);
3636 extern void *sp_ext_gc_alloc(size_t sz, void (*fin)(void *), void (*scan)(void *));
3737 extern void sp_ext_mark_string(const char *s);
38+extern void sp_raise_cls(const char *cls, const char *msg);
3839
3940 /* The scanner struct lives in spinel's GC heap. `source` /
4041 `matched` are GC-tracked strings; the scan function below
@@ -230,6 +231,13 @@
230231
231232 sp_StringScanner *sp_StringScanner_unscan(sp_StringScanner *sc) {
232233 if (!sc) return sc;
234+ /* CRuby raises StringScanner::Error when there is no match record to
235+ rewind to: a double unscan, or unscan before any successful scan.
236+ Guard before mutating state so a failed unscan leaves pos intact. */
237+ if (!sc->matched_p) {
238+ sp_raise_cls("StringScanner_Error",
239+ "unscan failed: previous match record not exist");
240+ }
233241 sc->pos = sc->last_pos;
234242 sc->matched = sp_ext_str_empty();
235243 sc->matched_p = 0;