Don't match environment variables whose name is a prefix of the requested name. #2040
All Vouch review confidence checks passed
Residual risk 0.07 = Change Risk (0.22) × (1 − Review Confidence (0.70)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.22 · 19 lines diff across 2 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.54 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.179
grynspan
ardge-walkerlee
Fix
Environment.variable(named:)returning the value of a variable whose name is only a prefix of the requested name.Motivation:
Environment._get(_:fromEnviron:)compares eachenvironrow withstrncmp(rowp, name, keyLength), wherekeyLengthis the length of the row's key. That only checks that the row's key is a prefix of the requested name; it never checks that the requested name ends atkeyLength. A variable whose name is a proper prefix of a name Swift Testing looks up is therefore returned instead.For example, with
S=1in the environment, looking upSWT_EXIT_TEST_IDreturns"1". The process then believes it is an exit test child, finds no exit test with that ID and runs nothing, soS=1 swift testruns zero tests and still exits 0. In a larger suite we saw the same misreading make exit test children run the whole suite and spawn more children, until the user's process limit was reached.This code is used on Apple platforms, Linux, FreeBSD, OpenBSD, Android and WASI. The Windows implementation calls
GetEnvironmentVariableW()and is not affected.Reproduction
A package with one plain test and one exit test:
swift testruns both tests:S=1 swift testruns nothing and exits 0:Seen with the Swift Testing that ships with Swift 6.4.2 (macOS 27.0.1, Apple silicon). With this change applied (built from source in the
swiftlang/swift:nightly-main-jammycontainer),S=1 swift testruns both tests.Modifications:
Environment._get(_:fromEnviron:), also requirename[keyLength] == 0so that the requested name ends where the key ends.strncmp()runs first and the&&short circuits, soname[keyLength]is only read when the firstkeyLengthcharacters ofnamematched, i.e. it is in bounds.SWT_ENVIRONMENT_VARIABLE_FOR_TESTINGminus its last character and checks thatEnvironment.variable(named:)andEnvironment.flag(named:)ignore it when looking up the full name. The test fails without the fix. The full test suite passes with the fix (Linux,swiftlang/swift:nightly-main-jammy).Checklist: