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

@@ -775,11 +775,16 @@
775775
776776 char *result = strdup(source);
777777 char *pos;
778- while ((pos = strstr(result, "require_relative")) != NULL) {
779- /* Check it's at start of line */
778+ char *scan_from = result;
779+ while ((pos = strstr(scan_from, "require_relative")) != NULL) {
780+ /* Check it's at start of line. If the match is mid-line (e.g.
781+ the word appears in a comment or string), advance past it and
782+ keep scanning the rest of the file — don't abort the whole
783+ loop, since later lines may have legitimate require_relative
784+ statements. */
780785 if (pos != result && *(pos - 1) != '\n') {
781- /* Not at line start, skip */
782- break;
786+ scan_from = pos + 1;
787+ continue;
783788 }
784789 char *line_end = strchr(pos, '\n');
785790 if (!line_end) line_end = pos + strlen(pos);
@@ -835,6 +840,8 @@
835840
836841 free(result);
837842 result = new_result;
843+ /* Buffer reallocated; restart scan from the top of the new buffer. */
844+ scan_from = result;
838845 free(content);
839846 }
840847 free(dir);