C in 2026: The Mistakes I Still See in Firmware Code Reviews
The mistakes I flag most often in firmware reviews are not exotic: they are the same ones as twenty years ago, and nearly all of them compile without a single warning.
In 2026 the talk is about Rust and AI-generated code, yet most of the firmware I review is still C. The language is not the problem. The problem is that C trusts the author, and on a microcontroller that trust is repaid with bugs that show up once a month, in the field, with no debugger attached.
Here are the twelve mistakes I flag most often, grouped into four families. Each one comes with the faulty code, the reason it slips through, and the fix. A checklist at the end is ready to paste into your pull request template.
Concurrency: data shared with an interrupt
This is the most expensive family, because the bugs depend on timing and cannot be reproduced on demand.
1. The missing volatile
static bool rx_ready; /* written in the ISR */
void USART1_IRQHandler(void) { rx_ready = true; }
void main_loop(void) {
while (!rx_ready) { } /* at -O2 this may become while(1) */
process_frame();
}The compiler sees nothing inside the loop that modifies rx_ready, so it may read it only once. It works in debug builds and hangs in release. Every variable shared between an ISR and the main code must be declared volatile.
2. volatile mistaken for atomicity
static volatile uint32_t tick_ms; /* incremented in the timer ISR */
uint32_t now = tick_ms; /* on an 8- or 16-bit MCU: several instructions */
rx_count++; /* read-modify-write: never atomic */volatile forces a fresh read from memory; it does not stop an interrupt from arriving halfway through that read. On an 8-bit core, a 32-bit read can return half the old value and half the new one. On Cortex-M a single 32-bit read is safe, but the increment and multi-field structures are not. The fix is a short critical section, or <stdatomic.h> where the toolchain supports it.
3. The critical section that re-enables interrupts behind your back
void queue_push(uint8_t b) {
__disable_irq();
buf[head++] = b;
__enable_irq(); /* what if they were already disabled? */
}If queue_push is called from inside another critical section, it switches interrupts back on in the middle of the caller's protected code. Save the previous state and restore that instead:
uint32_t primask = __get_PRIMASK();
__disable_irq();
buf[head++] = b;
__set_PRIMASK(primask);Types: arithmetic that does not do what it reads
C's integer rules were written for portability across machines, not for readability. These three mistakes all look correct on the page.
4. int where the width matters
unsigned int baud = 115200; /* truncated where int is 16 bits */
char c = rx_byte;
if (c > 0x7F) { ... } /* never true where char is signed */int is 16 bits on many 8- and 16-bit targets, and whether plain char is signed is up to the compiler. Code that moves from one MCU family to another breaks silently. Use <stdint.h> types whenever the width matters, and uint8_t for raw bytes.
5. Integer promotion
uint8_t crc = compute_crc(buf, len);
if (~crc == rx_crc) { ... } /* always false */Before ~ is applied, crc is promoted to int. With crc = 0x5A the result is 0xFFFFFFA5, which can never equal a uint8_t. The same rule breaks shifts: (uint16_t)x << 16 shifts a signed int, and overflowing it is undefined behaviour. Cast the result back to the type you mean:
if ((uint8_t)~crc == rx_crc) { ... }
uint32_t hi = (uint32_t)x << 16;6. The timeout that fails when the tick counter wraps
uint32_t deadline = tick_ms + 500;
while (tick_ms < deadline) { ... } /* wrong when the sum wraps */A 32-bit millisecond counter wraps after 49.7 days. If tick_ms + 500 overflows, deadline becomes a small number and the loop exits at once. A device that passes every bench test then fails after seven weeks of uptime. Compare elapsed time instead, because unsigned subtraction stays correct across the wrap:
uint32_t start = tick_ms;
while ((uint32_t)(tick_ms - start) < 500u) { ... }Memory: no MMU will catch you
On a desktop, a bad pointer gives you a segmentation fault. On most microcontrollers it quietly overwrites a neighbouring variable, and the crash happens somewhere else, later.
7. The receive buffer with no bounds check
static uint8_t rx_buf[64];
static uint8_t rx_idx;
void USART1_IRQHandler(void) {
rx_buf[rx_idx++] = USART1->DR; /* byte 65 lands on someone else's data */
}The author assumed frames never exceed 64 bytes. A noisy line or a misbehaving peer proves otherwise. Every write through an index needs a check against the buffer size, and every sprintf or strcpy should be an snprintf with sizeof the destination.
8. The stack nobody measured
void log_task(void *arg) {
char line[512]; /* task stack: 128 words = 512 bytes */
...
}Stack sizes are often copied from an example project and never revisited. A large local array, a deep call chain or a printf with floating point is enough to overflow into the next task's memory. Measure the worst case: GCC's -fstack-usage, your RTOS high-water mark, or a fill pattern checked at runtime. For the same reason, keep malloc out of the main loop: allocate at start-up or use fixed-size pools, so fragmentation cannot take the device down after weeks.
9. Casting a byte buffer to a struct
typedef struct {
uint8_t type;
uint32_t value;
} msg_t;
msg_t *m = (msg_t *)&rx_buf[3];
uint32_t v = m->value; /* padding, alignment, byte order */This one line carries three assumptions: that the compiler adds no padding after type (it adds three bytes), that the CPU tolerates unaligned access (Cortex-M0 faults), and that the wire format matches the host's byte order. A packed attribute fixes the first two and still leaves the third. Decode fields explicitly instead. Modern compilers turn this into the same instructions, without the assumptions:
uint32_t v;
memcpy(&v, &rx_buf[4], sizeof v); /* then convert byte order if needed */Robustness: code written for the bench, not the field
On the bench the sensor is always connected and the bus is always clean. These mistakes only show once the product leaves the lab.
10. Ignored return values
HAL_I2C_Master_Transmit(&hi2c1, addr, cmd, 2, 100);
HAL_I2C_Master_Receive(&hi2c1, addr, data, 6, 100);
temperature = decode(data); /* decodes whatever was in the buffer */If the sensor does not answer, data holds stale or uninitialised bytes, and the system acts on a temperature that was never measured. Check every status, and decide in advance what the caller does on failure: retry, report, or fall back to a safe state.
11. The wait with no timeout
while (!(SPI1->SR & SPI_SR_RXNE)) { } /* waits forever if the clock never comes */A disconnected cable or a peripheral stuck in an error state turns this loop into a hang. If a watchdog is running, the device resets with no trace of the cause. Every wait on hardware needs a bounded exit and an error path, using the wrap-safe pattern from point 6.
12. The ISR that does the work
void EXTI0_IRQHandler(void) {
read_sensor_over_i2c();
printf("sample: %d\n", sample);
HAL_Delay(10); /* blocks every lower-priority interrupt */
}While this handler runs, lower-priority interrupts wait, bytes are lost on other peripherals and timing jitter grows. HAL_Delay inside an ISR can also deadlock, since it depends on the tick interrupt. An ISR should capture the event, clear the flag and hand over to the main loop or a task:
void EXTI0_IRQHandler(void) {
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_0);
sample_requested = true; /* volatile, see point 1 */
}A review checklist, and the tools that should run first
A reviewer's attention is limited, so spend it where tools cannot help. Paste this list into your pull request template:
Every variable shared with an ISR is volatile, and every multi-step access to it is protected.
Critical sections restore the previous interrupt state.
Fixed-width types are used wherever the size matters.
Results of ~, << and mixed-width arithmetic are cast to the intended type.
Timeouts compare elapsed time, never absolute deadlines.
Every buffer write is bounds-checked; no sprintf, strcpy or gets.
Worst-case stack usage is measured for each task, and nothing allocates from the heap after start-up.
Wire formats are decoded field by field, with explicit byte order.
Every return value is checked or explicitly discarded with a comment.
Every wait on hardware has a timeout and an error path.
ISRs only capture, clear and signal.
Most of these can be caught before a human opens the diff:
Compiler warnings (-Wall -Wextra -Wconversion -Wshadow, with -Werror in CI): width and sign conversions, unused results. Points 4, 5, 10.
Stack reports (-fstack-usage, -Wstack-usage=N): oversized frames, per function. Point 8.
Static analysis (cppcheck, clang-tidy, a MISRA C checker): buffer overruns, ignored returns, unsafe casts. Points 5, 7, 9, 10.
Host unit tests with AddressSanitizer and UBSan: out-of-bounds writes, signed overflow, misaligned access. Points 5, 7, 9.
RTOS stack high-water mark and a fault handler that logs: stack overflow and hangs seen in the field. Points 8, 11.
Concurrency (points 1 to 3) and ISR design (point 12) are the exceptions. Tools catch very little there, so that is where review time belongs.
What actually changes the outcome
None of these twelve mistakes needs a new language to fix. They need compiler warnings treated as errors, a static analyser in CI, and a reviewer who knows where tools go blind.
If you adopt only one habit from this list, make it the first checklist line: for every variable in a diff, ask who else can touch it and when. That single question finds the bugs that cost weeks.
Which mistake would you add as number thirteen?



Commenti