Commit 1bf0d77f
1bf0d77f44c3Merged
Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): escape \n \t \r in emitted regex literals
A regex source that contains a raw newline, tab, or CR (e.g. a
multi-line /x pattern) used to produce a broken C source — the
character flowed through to the emitted re_compile("...", N, F) call
unescaped, closing the C string literal mid-line:
/tmp/_t.c:8:28: warning: missing terminating " character
8 | sp_re_pat_0 = re_compile("
| ^
/tmp/_t.c:9:3: error: 'hello' undeclared (first use in this function)
/tmp/_t.c:11:1: warning: missing terminating " character
11 | ", 17, 0);
| ^
Only \ and " were being escaped. Extend the substitution loop to
also rewrite raw \n, \t, and \r in the pattern bytes to the
corresponding C escape sequences. The byte length passed to
re_compile stays the original pat.length, so the regex engine still
sees the same raw bytes at runtime — only the C-source representation
of the literal changes.
Adds test/regex_multiline_pattern.rb covering a multi-line /x
literal plus single-character \t / \n / \r escapes.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 1bf0d77f44c3
- Parent
- c98048e104bd
spinel_codegen.rbmodifiedView file
ch = pat[pi] if ch == 92.chr cpat = cpat + 92.chr + 92.chr elsif ch == 34.chr cpat = cpat + 92.chr + 34.chr elsif ch == 10.chr # Embedded newlines / tabs / CRs in /x patterns must be # encoded as escape sequences; emitting them raw produces an # unterminated C string literal. cpat = cpat + 92.chr + "n" elsif ch == 13.chr cpat = cpat + 92.chr + "r" elsif ch == 9.chr cpat = cpat + 92.chr + "t" else if ch == 34.chr cpat = cpat + 92.chr + 34.chr else cpat = cpat + ch end cpat = cpat + ch end pi = pi + 1 endtest/regex_multiline_pattern.rbaddedView file
Loading diff…