Commit 94cc9f58
94cc9f58c5f2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix(parse): don't abort require_relative scan on a non-line-start match, close #12
resolve_requires() in spinel_parse.c uses strstr to locate the literal
text "require_relative". When the first hit was not at the start of a
line (e.g. the word appeared in a comment, string, or identifier
earlier in the file), the loop did break; instead of advancing past
the match — silently abandoning every legitimate require_relative
statement that followed. The downstream symptom was a confusing
unresolved-constant error far from the actual cause.
Replace the break with a continue after advancing a scan_from
cursor past the spurious hit. After a successful inline+replacement,
reset scan_from to the top of the new buffer (the previous buffer
was freed). The line-start guard itself is unchanged.
This is the exact patch suggested in the issue. It does not address the deeper limitation that strstr can match inside string literals or identifiers; an AST-walk-based resolver would be a separate change.
The existing make-test harness compiles each test/*.rb standalone, so there is no good place to add a multi-file regression here. Verified manually on three cases: the issue's reproducer, comment-free, and a file with multiple consecutive require_relatives after a comment.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 94cc9f58c5f2
- Parent
- 15932f0c2943
spinel_parse.cmodifiedView file
char *result = strdup(source); char *pos; while ((pos = strstr(result, "require_relative")) != NULL) { /* Check it's at start of line */ char *scan_from = result; while ((pos = strstr(scan_from, "require_relative")) != NULL) { /* Check it's at start of line. If the match is mid-line (e.g. the word appears in a comment or string), advance past it and keep scanning the rest of the file — don't abort the whole loop, since later lines may have legitimate require_relative statements. */ if (pos != result && *(pos - 1) != '\n') { /* Not at line start, skip */ break; scan_from = pos + 1; continue; } char *line_end = strchr(pos, '\n'); if (!line_end) line_end = pos + strlen(pos); free(result); result = new_result; /* Buffer reallocated; restart scan from the top of the new buffer. */ scan_from = result; free(content); } free(dir);