Commit d909df8b

d909df8b5931Merged

Ryan Seys <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(spinel): avoid BSD mktemp tail-template collision on macOS

BSD mktemp (macOS) only substitutes the trailing run of X's in the template, so mktemp /tmp/spinel_out.XXXXXX.c collapses to the literal name /tmp/spinel_out.XXXXXX.c instead of producing a unique path. Concurrent spinel foo.rb invocations then race on the same file, truncating each other's emitted C and crashing the codegen pipeline.

Mint a unique 0700 temp directory via mktemp -d and place the .c file inside it under a known name. Compared to the obvious mktemp ...).c trick:

  • No leaked sibling: the bare-name file mktemp creates without -d is never written to once the shell appends .c to the variable; here the directory holds the only .c and the trap removes it.
  • Explicit error path: || exit 1 fires if mktemp -d fails (e.g. /tmp not writable); previously $C_TMP would silently become .c and downstream rm would target the wrong path.
  • Permissions: the 0700 directory hides the .c filename from other local users, where > file under the default umask would have created a 0644 file.

Cleanup trap rewritten to do rm -rf "$C_TMP_DIR" guarded by a non-empty check so an early-exit before the dir is minted is a no-op. The C_FILE/C_TMP plumbing on the rest of the pipeline is unchanged — only the path source moves from a flat temp name to a file inside a temp dir.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
d909df8b5931
Parent
1d5291554707

spinelmodifiedView file

@@ -42,6 +42,7 @@
4242 CC_CMD="cc"
4343 EXTRA_FLAGS=""
4444 C_TMP=""
45+C_TMP_DIR=""
4546 EVAL_SCRIPT=""
4647 EVAL_USED=0
4748 EVAL_TMP=""
@@ -110,7 +111,7 @@
110111 fi
111112 AST_TMP="$(mktemp /tmp/spinel_ast.XXXXXX)"
112113 IR_TMP="$(mktemp /tmp/spinel_ir.XXXXXX)"
113-trap 'rm -f "$AST_TMP" "$IR_TMP" "$C_TMP" "$EVAL_TMP"' EXIT
114+trap 'rm -f "$AST_TMP" "$IR_TMP" "$EVAL_TMP"; [ -n "$C_TMP_DIR" ] && rm -rf "$C_TMP_DIR"' EXIT
114115
115116 # Step 1: Parse. spinel_parse is C-only; build it via `make parse`
116117 # (or `make all`) before invoking spinel.
@@ -148,7 +149,8 @@
148149 if [ $C_ONLY -eq 1 ]; then
149150 C_FILE="${OUTPUT:-$BASENAME.c}"
150151 else
151- C_TMP="$(mktemp /tmp/spinel_out.XXXXXX.c)"
152+ C_TMP_DIR="$(mktemp -d /tmp/spinel_out.XXXXXX)" || { echo "spinel: mktemp -d failed" >&2; exit 1; }
153+ C_TMP="$C_TMP_DIR/out.c"
152154 C_FILE="$C_TMP"
153155 fi
154156