Skip to content

fix(#695): force byte-wise bracket ranges everywhere, and pin it with a scanner - #699

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/695-locale-ranges
Sep 15, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/695-locale-ranges

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Stacked on #697 (base is its branch, so the diff here is only this work;
GitHub retargets to main when #697 merges).

What #698 measured

#695 fixed one site. The obvious next question was how many others there are,
and the answer needed a glibc runner -- macOS's libc does not collate bracket
ranges at all, so nothing can be observed locally. #698 is the throwaway
diagnostic that got the matrix, on ubuntu-latest / bash 5.2 / a
localedef-built tr_TR.UTF-8:

shape C tr_TR
[[ =~ ]] [A-Z] vs I match NO MATCH
[[ =~ ]] [a-z] vs i match NO MATCH
[[ =~ ]] [a-zA-Z] vs I and i match NO MATCH
[[ =~ ]] [A-Za-z0-9_] vs ID match NO MATCH
[[ =~ ]] [A-Za-z] vs é NO match MATCH
case [A-Za-z]:/* vs I:/x match match
case *[!A-Za-z0-9._-]* vs SESS-I and é — identical
${v//[!a-zA-Z0-9]/-} — identical

Both failure directions are real -- a range that loses I, and the same range
gaining é -- but only through [[ =~ ]], which glibc's regcomp matches
by collation. bash's own glob matcher, behind case and ${v//[!...]/},
compares bytes and did not move.

That is narrower than I assumed before measuring, and it cut the change down:
an earlier draft of this branch restructured all eight of the hooks'
entry guards behind a new sourced library, which the matrix says fixes nothing.
Reverted.

What this changes

Thirteen one-line local LC_ALL=C insertions, each scoped to one function,
plus the scanner and the behavioural tests. Two of the thirteen are live bugs:

  • _remember_cfg_flatten_cache_valid_line (scripts/log.sh) -- a config key
    carrying an I makes its cache line read as malformed, so the config-data
    cache is refused and every hook re-reads and re-flattens the config, forever,
    with nothing said. A permanent performance floor that no log line mentions.
  • _remember_normalize_win_path (scripts/resolve-paths.sh, mirrored in
    scripts/lib-env-cache.sh) -- drive I: stops being recognised. Its
    tr '[:lower:]' '[:upper:]' is the second half: under Turkish rules a
    lower-case i: drive upper-cases to a dotted İ, two bytes in a slot that
    must hold one ASCII letter.

The other eleven are case or substitution sites inside functions, where the
measurement says nothing is broken today and one line makes them immune anyway.

The eight that were left alone

The hooks' top-level entry guards (agy-stop-hook.sh, post-tool-hook.sh,
session-end-hook.sh, session-start-hook.sh) stay exactly as they are. They
are case patterns, which the matrix cleared, and reaching a shared helper
would mean moving the guard later in the file -- the guard's position at the
point of entry is the guarantee. They sit in the scanner's ALLOWLIST with that
measurement written out as the reason, rather than being excluded from the
scan: one libc and one bash is not a licence to stop looking, and POSIX does
permit a case range to collate.

Test-first

The first commit is tests only and is red: the scanner on 14 sites, plus the
two behavioural modules on the Linux legs. The fix follows.

Part of #695

[AI-generated]

@fdaviddpt
fdaviddpt changed the base branch from fix/695-safe-eval-locale to main September 15, 2026 13:16
…lation

Red on purpose. The fix is not in this commit.

Two halves.

The scanner (tests/test_locale_ranges_695.py) reads source on every
platform and reports every ASCII letter range in a shipped shell script
that is not matched under LC_ALL=C. It is red on 14 sites today. Three
places in this tree already carried the fix before #695 -- config(),
_remember_cfg_flatten_cache_valid_value, lib-slug.sh's LC_ALL=C sed --
each found separately, one bug report at a time; this is the general
form, so the next one is a finding at authoring time instead.

The behavioural half (tests/test_id_guards_locale_695.py) covers the two
sites #698's measured matrix showed actually break, both reached through
`[[ =~ ]]`:

  _remember_cfg_flatten_cache_valid_line -- a config key carrying an `I`
  makes its cache line read as malformed, so the config-data cache is
  refused and every hook re-reads and re-flattens the config, forever,
  silently. A performance floor nothing reports.

  _remember_normalize_win_path -- drive `I:` stops being recognised, and
  its `tr '[:lower:]' '[:upper:]'` maps a lower-case `i:` drive to a
  dotted `İ` under Turkish rules: two bytes where one ASCII letter has
  to go.

What #698 measured, on ubuntu-latest / bash 5.2 / localedef-built
tr_TR.UTF-8, and why the scope is what it is:

  [[ =~ ]] over [A-Z], [a-z], [a-zA-Z], [A-Za-z0-9_]   loses I and i
  [[ =~ ]] over [A-Za-z]                               GAINS é
  case patterns, and ${v//[!...]/}                     do not move

Both failure directions are real, and only the regex path collates;
bash's own glob matcher compares bytes. The `case` guards are therefore
listed in the scanner's allowlist with that measurement as their stated
reason rather than excluded from the scan -- one libc and one bash is
not a licence to stop looking, and POSIX does permit a case range to
collate.

The locale probe both modules share accepts a locale only after watching
this machine's bash fail under it, and builds one with localedef when
the runner ships none.

Part of #695

Co-Authored-By: Max <noreply>
…at hold them

Green half of the previous commit's red.

Thirteen `local LC_ALL=C` insertions, each scoped to one function and
restoring the caller's locale on return. Two of them are live bugs that
#698's measured matrix identified, both reached through `[[ =~ ]]`:

  _remember_cfg_flatten_cache_valid_line (scripts/log.sh) validated each
  config-data cache line against ^_RCFG_[A-Za-z0-9_]+=. A config key
  carrying an I made the line read as malformed under Turkish collation,
  so the cache was refused and every hook fell back to re-reading and
  re-flattening the config on every invocation, permanently, with nothing
  said. Confirmed red on the ubuntu legs of the previous commit: VALID
  became REJECTED.

  _remember_normalize_win_path (scripts/resolve-paths.sh, mirrored in
  scripts/lib-env-cache.sh) stopped recognising the drive I:, and its
  `tr '[:lower:]' '[:upper:]'` mapped a lower-case i: drive to a dotted
  İ under Turkish case rules -- two bytes where one ASCII letter has to
  go. Forcing C fixes the range and the case mapping together.

The other eleven are `case` and substitution sites, which the matrix
cleared: only the regex path collates. One line makes them immune anyway
and leaves the scanner no exception to carry.

Also here, because the previous commit's CI said so rather than because
I reasoned it:

  The drive-letter legs failed with exit 127, not with a wrong answer --
  `_remember_normalize_win_path` is `unset -f`'d at resolve-paths.sh:340
  once resolution is done, so sourcing the script does not leave it
  callable. Extracted by regex instead, the same route
  tests/test_save_session_marker_unreadable_625.py already takes for
  ts_marker_read and for the same recorded reason. Paired with two new
  C-locale legs that run on every platform, so a harness that stops
  working fails loudly everywhere instead of skipping in silence -- which
  is exactly how it hid the first time.

  scripts/lib-memory-dir.sh is byte-pinned against origin/main
  (tests/test_case_divergence_298.py), so both insertions into it are
  declared in _SANCTIONED_DIVERGENCE with their reason, the way #429 and
  #662 already are.

Part of #695

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt force-pushed the fix/695-locale-ranges branch from fe27560 to 2652353 Compare September 15, 2026 13:48
@fdaviddpt
fdaviddpt merged commit 6d1caf2 into main Sep 15, 2026
16 checks passed
@fdaviddpt
fdaviddpt deleted the fix/695-locale-ranges branch September 15, 2026 14:02
fdaviddpt added a commit that referenced this pull request Sep 15, 2026
…e the tag (#701)

The v0.33.0 release audit (round 1, token rel-0330-429b3dd1) returned six
findings, none in a blocking row. Five are fixed here; the sixth is filed
as #700.

**The scanner credited a nested function's declaration to its parent.**
_function_spans closed a function at the first `}` at column 0, on the
stated assumption that no function is defined inside another. #696 broke
that assumption: capture_was_seen() is defined at column 0 inside
_remember_deferred_phase(), so the outer span was computed as ending at
the inner function's brace and the inner's `local LC_ALL=C` was credited
to 148 outer lines that have none. Neither PR contains this -- #696 has
no scanner, #699 has no nesting -- it existed only once both had landed,
which is the kind of thing only a whole-delta audit sees. Nothing falls
through it today (a direct sweep of those lines finds no ranges), but
the scanner is the entire standing defence for a class that already
shipped a total-failure bug. Now a stack, with a nested fixture pinning
both halves.

**`local LC_ALL=C` never reached the `tr` child.** `local` on a name the
environment never exported leaves it unexported, so on a host whose
language comes from LANG alone -- what setting a system language
actually produces -- the drive-letter uppercase still ran under Turkish
rules and mapped `i` to the dotted `İ`. My own test in #699 passed
because its harness exports LC_ALL, so `local` shadowed an
already-exported name: it passed for a reason that does not hold on a
real host. The new control puts a recording `tr` on PATH and asserts
what locale it was actually called under, which runs on every platform
and does not need a glibc runner to fail. The changelog fragment that
claimed this was fixed is corrected in the same commit.

**An unmeasured file size laundered into 0**, in two places, which made
_remember_emit_file's own "no usable size" arm unreachable from its only
caller: `0` is a digit string and `0 -gt 16384` is false, so a file of
any size took the read path that #660's own threshold exists to keep it
off -- 4MB measured at 24.1s on windows-latest, on the session-start
foreground path. The batched `wc -c` can fail wholesale, leaving every
file in that state at once. Unknown is now kept distinct from zero end
to end, and the compact-mode notice says `size unknown` rather than
printing `0 bytes` for a file nobody measured.

**The deferred default-path control asserted one of the three records
its own docstring named.** It now polls both that this fixture's storage
mode can produce, and says why the third needs external storage and
where it is covered instead.

Local suite: 2483 passed, 58 skipped. The one failure on the way was
this repo's own blanket-win32-skip guard catching a triage row I had not
written yet -- added.

Part of #695
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant