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
extern size_t sp_ext_str_byte_len(const char *s);extern void *sp_ext_gc_alloc(size_t sz, void (*fin)(void *), void (*scan)(void *));extern void sp_ext_mark_string(const char *s);extern void sp_raise_cls(const char *cls, const char *msg);/* The scanner struct lives in spinel's GC heap. `source` / `matched` are GC-tracked strings; the scan function belowsp_StringScanner *sp_StringScanner_unscan(sp_StringScanner *sc) { if (!sc) return sc; /* CRuby raises StringScanner::Error when there is no match record to rewind to: a double unscan, or unscan before any successful scan. Guard before mutating state so a failed unscan leaves pos intact. */ if (!sc->matched_p) { sp_raise_cls("StringScanner_Error", "unscan failed: previous match record not exist"); } sc->pos = sc->last_pos; sc->matched = sp_ext_str_empty(); sc->matched_p = 0;spinel_codegen.rbmodifiedView file
Loading diff…
test/string_scanner_unscan_error.rbaddedView file
Loading diff…