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 Name | Signal # | Shell Exit Code (128 + N) | Trigger Condition & Meaning |
|---|---|---|---|
| SIGBUS | 7 | 135 | Bus fault on misaligned memory access or out-of-range mmap page (signal 7 on Linux x86-64). |
| SIGSEGV | 11 | 139 | Segmentation 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.
unsigned char raw[8] = {0};
uint32_t *p = (uint32_t *)(raw + 1);
uint32_t v = *p;unsigned char raw[8] = {0};
uint32_t v = 0;
memcpy(&v, raw + 1, sizeof v);Taking address of unaligned members in __attribute__((packed)) structs
Passing &packed_hdr.len to int* creates a misaligned pointer.
struct __attribute__((packed)) H { char a; int b; } h;
int *p = &h.b;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 compilerRead 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 compilerExpected output from the corrected program
Reference output, not a live execution result.
42Compiler & Sanitizer Diagnostic Matrix
| Compiler / Runtime Symptom | Root Cause | Diagnostic Flag / Check | Corrected Pattern |
|---|---|---|---|
| SIGBUS / misaligned uint32_t load | Dereferencing unaligned buffer cast `(int *)(buf + 1)` | -fsanitize=alignment -Wcast-align | Copy unaligned bytes with `memcpy(&val, buf + 1, sizeof val)` |
| SIGBUS on mmap region | Reading/writing past underlying file length | Check `ftruncate` / file size | Ensure file is resized to cover full mapped span before access |
Debugging Techniques and Commands
gcc -std=c17 -g -fsanitize=alignment -fno-sanitize-recover=all main.c -o app
Reports misaligned reads/writes even on x86-64 hosts.
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.