Commit 0aede4be

0aede4be4b0bMerged

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

Description

fix(strscan): NULL str + calloc guards in StringScanner_new (#811)

StringScanner.new(nil) used to call strlen(NULL) and segfault. CRuby raises TypeError; spinel can't easily raise from a typed- string constructor, so the safer fallback is to treat NULL str as empty -- the scanner behaves like a 0-length input.

Also check the calloc return so an OOM doesn't deref sc->source = str against a NULL sc.

Closes #811

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

Details

Commit
0aede4be4b0b
Parent
a5e1f2fb31d4

lib/strscan.cmodifiedView file

@@ -22,9 +22,15 @@
2222 int64_t matched_len;
2323 } sp_StringScanner;
2424
25-/* Constructor */
25+/* Constructor.
26+ Issue #811: NULL str used to segfault at strlen(NULL); treat as
27+ the empty string. CRuby raises TypeError -- spinel can't easily
28+ raise from a typed-string constructor here, so the safer fallback
29+ is empty-string behaviour. */
2630 sp_StringScanner *sp_StringScanner_new(const char *str) {
31+ if (!str) str = "";
2732 sp_StringScanner *sc = (sp_StringScanner *)calloc(1, sizeof(sp_StringScanner));
33+ if (!sc) return NULL;
2834 sc->source = str;
2935 sc->source_len = (int64_t)strlen(str);
3036 sc->pos = 0;