Runtime (SIGSEGV / Signal)Runtime Signals6 min read

Fix “Bus error (SIGBUS)” & Unaligned Access in C

Unlike SIGSEGV (accessing an unmapped virtual page), `SIGBUS` (`signal 7` on Linux x86-64 / signal `10` on BSD/macOS) occurs when the hardware bus rejects a physically invalid or misaligned memory transaction—such as dereferencing a multi-byte pointer at an odd byte offset or accessing beyond a truncated `mmap()` region.

Quick Fix Summary

Replace unsafe pointer casts on raw byte buffers (*(uint32_t *)(buf + 1)) with uint32_t val; memcpy(&val, buf + 1, sizeof val); which is portable, avoids strict-aliasing UB, and optimizes to safe loads.

Signal & Exit Code Reference

SIGBUS is not portable across architectures; check your target CPU alignment rules and UBSan alignment diagnostics.

Signal NameSignal #Shell Exit Code (128 + N)Trigger Condition & Meaning
SIGBUS7135Bus fault on misaligned memory access or out-of-range mmap page (signal 7 on Linux x86-64).
SIGSEGV11139Segmentation fault from invalid virtual page permission or NULL dereference.

Why This Error Happens

Why casting byte buffers to int* breaks strict aliasing and alignment

Even on CPUs that sometimes tolerate misaligned scalar loads, SIMD instructions or -fsanitize=alignment trap immediately when an int * lacks 4-byte address alignment.

Portable memcpy eliminates alignment faults cleanly

Copy multi-byte integers out of unaligned buffers using memcpy(&out, src, sizeof out); optimizing compilers compile small fixed-size memcpy calls cleanly into safe instructions.

Top Root Causes & Specific Code Fixes

Casting a char buffer at an odd byte offset to int* or uint32_t*

Reading multi-byte integers across misaligned byte offsets triggers hardware/UBSan traps.

Broken Pattern
unsigned char raw[8] = {0};
uint32_t *p = (uint32_t *)(raw + 1);
uint32_t v = *p;
Corrected Pattern
unsigned char raw[8] = {0};
uint32_t v = 0;
memcpy(&v, raw + 1, sizeof v);
Illustrative diagnostic (not executed):runtime error: load of misaligned address 0x... for type 'uint32_t', which requires 4 byte alignment

Taking address of unaligned members in __attribute__((packed)) structs

Passing &packed_hdr.len to int* creates a misaligned pointer.

Broken Pattern
struct __attribute__((packed)) H { char a; int b; } h;
int *p = &h.b;
Corrected Pattern
struct __attribute__((packed)) H { char a; int b; } h = {0, 7};
int val = h.b;

Complete Broken and Corrected Programs

Compare the source before running it. The broken program is intentionally incorrect; its behavior can depend on the compiler and platform.

Read the broken program
#include <stdint.h>
#include <stdio.h>
int main(void) {
    unsigned char raw[8] = {0, 42, 0, 0, 0};
    uint32_t v = *(uint32_t *)(raw + 1);
    printf("%u\n", v);
    return 0;
}
Open broken program in compiler
Read the corrected program
#include <stdint.h>
#include <stdio.h>
#include <string.h>
int main(void) {
    unsigned char raw[8] = {0, 42, 0, 0, 0};
    uint32_t v = 0;
    memcpy(&v, raw + 1, sizeof v);
    printf("%u\n", v);
    return 0;
}
Open corrected program in compiler

Expected output from the corrected program

Reference output, not a live execution result.

42

Compiler & Sanitizer Diagnostic Matrix

Compiler / Runtime SymptomRoot CauseDiagnostic Flag / CheckCorrected Pattern
SIGBUS / misaligned uint32_t loadDereferencing unaligned buffer cast `(int *)(buf + 1)`-fsanitize=alignment -Wcast-alignCopy unaligned bytes with `memcpy(&val, buf + 1, sizeof val)`
SIGBUS on mmap regionReading/writing past underlying file lengthCheck `ftruncate` / file sizeEnsure file is resized to cover full mapped span before access

▶ Load failing reproducer in Online C Compiler

Debugging Techniques and Commands

UBSan Alignment Sanitizer
gcc -std=c17 -g -fsanitize=alignment -fno-sanitize-recover=all main.c -o app

Reports misaligned reads/writes even on x86-64 hosts.

Strict Cast Warning
gcc -std=c17 -Wcast-align=strict -Wall main.c -o app

Warns whenever casting a narrow pointer to a wider alignment requirement.

Frequently Asked Questions

Why does UBSan fail my cast on x86-64 when my laptop runs it without crashing?

Unaligned pointer dereference is undefined behavior in ISO C; ARM/RISC-V hardware and compiler vectorized SSE/AVX instructions can fault immediately.

Is memcpy slow for reading a 4-byte integer?

No. At `-O1` and above, compilers replace fixed 2/4/8-byte `memcpy` calls with unaligned-safe register moves without calling libc.

Can mmap() also cause SIGBUS?

Yes. Accessing memory past the underlying file size on an `mmap()` mapping raises `SIGBUS` rather than extending the file.