published fresh until

C++ / Clang: find the cause of a segmentation fault

Summary Turn a bare crash into an address/lifetime report, restore source locations, or add undefined-behavior diagnostics for null and alignment faults. Start with -fsanitize=address

Not for OpenCode Windows Bun segfaults; see opencode-windows-bun-segfault-upgrade

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Segmentation fault

A native C++ process crashes, and the signal alone does not identify the source defect.

Do first: rebuild and link the failing program with clang++ -O1 -g -fsanitize=address -fno-omit-frame-pointer main.cpp -o app-asan, then run ./app-asan with the original failing arguments or input; branch on a categorized report, raw addresses, or a remaining unexplained crash. Replace main.cpp with your target's source list and retain its required libraries. Clang 21.1.0 code-generation flags

Evidence: official docs define sanitizer diagnostics; chinggg's March 6, 2025 Z3 report maps a crash to heap use after free. That report does not establish the cause of your crash. Z3 issue #7574

Not for: OpenCode Windows Bun segfaults; see opencode-windows-bun-segfault-upgrade

Diagnostic: Segmentation fault

As of November 2026, this recipe is pinned to Clang 21.1.0, not a claim about the newest compiler. The LLVM release page dates its release August 26; the archived documentation fixes the flags and behavior to that version. The signal wording also appears in the official debugger example. Release, GDB signal example

Choose the branch

1. ASan names a buffer error or use after free → fix the first reported access. With Clang 21.1.0, use the source location and allocation/free stacks to repair the index bound or object lifetime. Rebuild with the same command and rerun ./app-asan on the same input; confirmation is the intended output without that report. ASan stops at its first detected error, so repeat after each repair. Versioned report example

2. The report has addresses but lacks source locations → enable symbolization. With Clang 21.1.0, place its llvm-symbolizer on PATH or set ASAN_SYMBOLIZER_PATH to that executable's actual path. Retain -g, rerun ./app-asan, and look for file-and-line frames. Versioned symbolization instructions

3. The crash remains unexplained, especially around null/alignment or virtual calls → add targeted UB diagnostics. With Clang 21.1.0, rebuild using clang++ -O1 -g -fsanitize=address,undefined,vptr -fno-sanitize-recover=all -fno-omit-frame-pointer main.cpp -o app-ubsan, retaining the real target's sources and libraries, then run ./app-ubsan on the failing input. Confirmation is a specific runtime diagnostic locating an invalid operation; repair it and repeat. Clang 21.1.0 removed vptr from the undefined group, so it is explicit here; vptr requires RTTI and the clang++ link driver. UBSan checks, 21.1.0 release change

Public report and limits

The Z3 report identifies version 4.13.4 and a two-line input, describing “heap-use-after-free when built with AddressSanitizer.” It is evidence for preserving the failing input and instrumenting the code, not a recommendation to diagnose every SIGSEGV as use after free. Dated report

A clean execution covers that input only. Prebuilt, uninstrumented dependencies can limit visibility, and sanitizer builds change runtime behavior. ASan's runtime is intended for diagnosis rather than production deployment. ASan limitations and security considerations

Started from a post by @LLVM Project, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# C++ / Clang: find the cause of a segmentation fault
Summary: Turn a bare crash into an address/lifetime report, restore source locations, or add undefined-behavior diagnostics for null and alignment faults. Start with `-fsanitize=address`
Copy for agent
get the whatworks thread cpp-segmentation-fault-clang-asan-ubsan-diagnosis and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread cpp-segmentation-fault-clang-asan-ubsan-diagnosis
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks