Commit c5a9c6e1

c5a9c6e1d664Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +2 added · ~3 modified

Description

Implement File.expand_path

File.expand_path(path[, base]) was unwired -- it fell through to the generic dispatch and emitted 0 (cannot resolve call to 'expand_path' on int).

Add a pure-string expansion (CRuby-compatible; does not require the path to exist):

  • sp_runtime.h: sp_file_expand_path resolves a leading ~/~/ to $HOME, makes a relative path absolute against base (itself expanded; the 1-arg form uses the cwd), and collapses ./../ duplicate-slash segments. ~user is unsupported (left as-is).
  • codegen: File.expand_path(path[, base]) routes to it (base NULL for the 1-arg form).
  • analyze: types it string alongside basename/dirname/extname.

File.expand_path("test", "/tmp") # => "/tmp/test" (was 0) File.expand_path("../x", "/a/b") # => "/a/x" File.expand_path("a") # => "/a"

The format strings cap each path component (%.4000s) so the compiler can prove no snprintf truncation (-Werror=format-truncation under the test harness); 4000 chars/component is well past PATH_MAX.

(The pre-existing FFI-only compile-time fold in ffi_arg_str, which uses the source dir as the default base, is unaffected -- it only fires for File.expand_path appearing as an FFI directive argument.)

Full suite green (710).

Fixes #1124

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

Details

Commit
c5a9c6e1d664
Parent
4a4b506b2ad6

lib/sp_runtime.hmodifiedView file

@@ -3494,6 +3494,81 @@
34943494 return buf;
34953495 }
34963496
3497+/* File.expand_path(path[, base]) -- CRuby-compatible pure-string
3498+ expansion (does NOT require the path to exist). A leading `~` / `~/`
3499+ becomes $HOME; a relative path is resolved against `base` (itself
3500+ expanded; NULL means the current working directory); and `.` / `..` /
3501+ duplicate-slash segments are collapsed. `~user` is unsupported and is
3502+ left as-is. */
3503+static const char *sp_file_expand_path(const char *path, const char *base) {
3504+ char raw[8192];
3505+ char cwd[4096];
3506+ const char *home = getenv("HOME");
3507+ if (!home) home = "";
3508+ if (!path) path = "";
3509+
3510+ /* The `%.4000s` precision caps each component so the compiler can
3511+ prove the combined output (<= 8001) fits in the 8192 buffer -- this
3512+ silences -Wformat-truncation (an error under the test harness's
3513+ -Werror). 4000 chars/component is well past PATH_MAX, so real paths
3514+ never truncate. */
3515+ if (path[0] == '~' && (path[1] == '\0' || path[1] == '/')) {
3516+ snprintf(raw, sizeof(raw), "%.4000s%.4000s", home, path + 1);
3517+ } else if (path[0] == '/') {
3518+ snprintf(raw, sizeof(raw), "%.4000s", path);
3519+ } else {
3520+ char basebuf[8192];
3521+ const char *b;
3522+ if (base && base[0]) {
3523+ if (base[0] == '~' && (base[1] == '\0' || base[1] == '/')) {
3524+ snprintf(basebuf, sizeof(basebuf), "%.4000s%.4000s", home, base + 1);
3525+ b = basebuf;
3526+ } else if (base[0] == '/') {
3527+ b = base;
3528+ } else {
3529+ if (!getcwd(cwd, sizeof(cwd))) cwd[0] = 0;
3530+ snprintf(basebuf, sizeof(basebuf), "%.4000s/%.4000s", cwd, base);
3531+ b = basebuf;
3532+ }
3533+ } else {
3534+ if (!getcwd(cwd, sizeof(cwd))) cwd[0] = 0;
3535+ b = cwd;
3536+ }
3537+ snprintf(raw, sizeof(raw), "%.4000s/%.4000s", b, path);
3538+ }
3539+
3540+ /* Normalize: walk segments, collapsing `.`/`..`/`//`. seg_start[k]
3541+ records the output length to roll back to when a `..` pops the
3542+ k-th kept segment. */
3543+ size_t rawlen = strlen(raw);
3544+ char *out = sp_str_alloc(rawlen + 1);
3545+ size_t seg_start[1024];
3546+ int nseg = 0;
3547+ size_t olen = 0;
3548+ out[olen++] = '/';
3549+ const char *p = raw;
3550+ while (*p) {
3551+ if (*p == '/') { p++; continue; }
3552+ const char *q = p;
3553+ while (*q && *q != '/') q++;
3554+ size_t slen = (size_t)(q - p);
3555+ if (slen == 1 && p[0] == '.') {
3556+ /* current dir -- skip */
3557+ } else if (slen == 2 && p[0] == '.' && p[1] == '.') {
3558+ if (nseg > 0) { nseg--; olen = seg_start[nseg]; }
3559+ } else {
3560+ size_t mark = olen;
3561+ if (olen > 1) out[olen++] = '/';
3562+ memcpy(out + olen, p, slen);
3563+ olen += slen;
3564+ if (nseg < 1024) seg_start[nseg++] = mark;
3565+ }
3566+ p = q;
3567+ }
3568+ out[olen] = 0;
3569+ return out;
3570+}
3571+
34973572 /* Read a file's bytes into a fresh IntArray. Distinct from
34983573 `sp_str_bytes(sp_file_read(path))` because plain sp_str_bytes uses
34993574 null-termination and stops at the first 0x00 byte — wrong for