Commit c80632fb
c80632fb44fbMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
docs(hash-nullable): record Phase 2 revert + re-plan notes
#708 surfaced a real-world breakage from 0ec6b1d's StrStrHash NULL return -- tep apps segfault at boot through code shapes the Phase 2 audit didn't enumerate. Reverted via 532ba3e.
Document the revert, three re-plan options (build flag, lazy
null-coalesce in codegen, or surgical compile_cond_expr fix that
only touches truthy semantics), and the current state (*StrHash
[missing] == sp_str_empty -- documented Ruby-semantic violation,
stable runtime).
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- c80632fb44fb
- Parent
- 8d3f67cf8b30
docs/HASH-NULLABLE.mdmodifiedView file
@@ -215,25 +215,52 @@
215215
`INT64_MIN + 1` — surprising but not a crash). Documented in216216
existing int? work (commit f6c3b6d era).217217
218-
## Phase 2 Scope218+
## Phase 2 Scope — REVERTED (2026-05-25, #708)219219
220-
Implement only the `*StrHash` variants first:220+
Phase 2 attempted to change `sp_StrStrHash_get` / `sp_IntStrHash_get` /221+
`sp_SymStrHash_get` to return `NULL` on missing keys. Committed as222+
0ec6b1d, then **reverted via 532ba3e** after #708 surfaced a223+
real-world breakage:221224
222-
1. `lib/sp_runtime.h`:223-
- `sp_StrStrHash_get`: `return h->default_v ? h->default_v : sp_str_empty;`224-
→ `return h->default_v ? h->default_v : NULL;`225-
- `sp_IntStrHash_get`: same226-
2. `spinel_codegen.rb` `emit_*_runtime` for `SymStrHash`:227-
- `sp_SymStrHash_get`: same NULL change228-
3. Verify no internal caller depends on `_get` returning non-NULL229-
(the `_values`, `_dup`, etc. paths — they read `h->order[i]`230-
which are existing keys, so the return is always non-NULL from231-
`h->vals[idx]`, not the miss fallback)232-
4. `make test` + bootstrap + optcarrot regression checks225+
- Tep apps segfault at boot.226+
- `lib/tep.rb`'s seeding chain (175-730) reads from hash slots227+
in patterns that the Phase 2 commit's "safe call paths" audit228+
missed -- the audit covered string-method dispatch229+
(`sp_str_length` / `sp_str_eq` / `sp_str_concat` are NULL-safe)230+
but didn't enumerate every downstream `const char *` use site231+
in user code or framework runtime.232+
- Restoring `sp_str_empty` semantics is the conservative call233+
until a wider audit + codemod recipe is ready.233234
234-
Phase 3 widens analyze's `[]` return type for string-valued hashes235-
(no real change since `string` is already nullable at the C level —236-
mostly tightens up call-site emit where it assumed non-NULL).235+
### Re-planned Phase 2 (deferred)236+
237+
The Ruby semantic violation is real (`if h[:missing]` evaluates as238+
truthy under the `""` fallback because `sp_str_empty` is a non-NULL239+
pointer). Bringing it inline with Ruby without breaking tep / real-240+
blog requires one of:241+
242+
1. **Opt-in via build flag** (`-DSP_STRHASH_MISS_NIL`). Apps that243+
audit their hash use can flip the flag; others stay on the244+
`""` fallback. Lowest-risk for downstream users.245+
2. **Lazy null-coalescing pass** in codegen: detect `h[k]` followed246+
by a method-call that requires non-NULL (`.length`, `.bytes`,247+
`.split`, ...) and insert a NULL → `sp_str_empty` coalesce.248+
The `if h[:k]` truthy site dispatches to a separate truthy249+
helper that treats `sp_str_empty` as falsy when the value came250+
from a hash miss (requires tagging the miss in the runtime).251+
3. **Surgical attack** on `compile_cond_expr` only: when the252+
condition is `h[k]` on a typed-string hash, emit253+
`sp_StrStrHash_has_key(h, k)` instead of the raw value-truthy254+
check. Doesn't change the runtime miss return at all; only255+
fixes the truthy check. Tep / real-blog can keep their256+
`s = h[k]; s.length` etc. patterns working.257+
258+
Option 3 is the smallest and most targeted; consider for a future259+
Phase 2 attempt.260+
261+
Until that lands, `*StrHash[missing] == sp_str_empty` (current262+
spinel behavior) -- documented Ruby-semantic violation, stable263+
runtime.237264
238265
## Phase 4 Scope — DEFERRED (2026-05-25)239266