← Back to Pull requests

Don't match environment variables whose name is a prefix of the requested name. #2040

Merged
ardge-walkerlee merged 2 files (19 lines) on Nov 14, 2026 18:33 UTC
View on GitHub
ardge-walkerlee ardge-walkerlee commented on Nov 14, 2026 17:34 UTC
Author

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 each environ row with strncmp(rowp, name, keyLength), where keyLength is 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 at keyLength. A variable whose name is a proper prefix of a name Swift Testing looks up is therefore returned instead.

For example, with S=1 in the environment, looking up SWT_EXIT_TEST_ID returns "1". The process then believes it is an exit test child, finds no exit test with that ID and runs nothing, so S=1 swift test runs 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:

// Package.swift
// swift-tools-version: 6.0
import PackageDescription
let package = Package(name: "Repro", targets: [
  .target(name: "Repro"),
  .testTarget(name: "ReproTests", dependencies: ["Repro"]),
])

// Tests/ReproTests/ReproTests.swift
import Testing
import Foundation

@Test func announce() {
  FileHandle.standardError.write("RUN pid=\(getpid())\n".data(using: .utf8)!)
}

@Test func exitsCleanly() async {
  await #expect(processExitsWith: .success) { exit(0) }
}

swift test runs both tests:

✔ Test announce() passed after 0.001 seconds.
✔ Test exitsCleanly() passed after 0.005 seconds.
✔ Test run with 2 tests in 0 suites passed after 0.005 seconds.

S=1 swift test runs nothing and exits 0:

warning: No matching test cases were run
0 tests completed in 0.00s ()
$ echo $?
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-jammy container), S=1 swift test runs both tests.

Modifications:

  • In Environment._get(_:fromEnviron:), also require name[keyLength] == 0 so that the requested name ends where the key ends. strncmp() runs first and the && short circuits, so name[keyLength] is only read when the first keyLength characters of name matched, i.e. it is in bounds.

  • Add a test that sets a variable whose name is SWT_ENVIRONMENT_VARIABLE_FOR_TESTING minus its last character and checks that Environment.variable(named:) and Environment.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:

  • Code and documentation should follow the style of the Style Guide.

  • If public symbols are renamed or modified, DocC references should be updated.
  • Diff: 19 lines (+18 / -1) across 2 files Reviewer: grynspan Review duration: 59m
    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
    0.22
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.54 · 1 substantive comment(s)
    0.54
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    70%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Real PR Conversation Timeline
    3 items
    grynspan grynspan approved these changes on Nov 14, 2026 18:32 UTC
    Approved Reviewer

    Approved these changes.

    grynspan grynspan commented on Nov 14, 2026 19:28 UTC
    Contributor

    We should probably cherry-pick this out of shame.

    Reviewers
    Assignees
    ardge-walkerlee ardge-walkerlee
    Labels
    risk: low confidence: 70.1% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 22.4% (LOW) · Higher = larger blast radius
    22.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 70.1% · Lower = rushed or shallow review
    70.1%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 6.7% · Safe to merge
    6.7%