void hello(void) { fprintf(stderr, "hello!\n"); }
void fun(void) {
void *buf[10];
static int i;
for (i = 0; i < 100; i++) buf[i] = hello;
}
int main(void) {
int buf[1000];
fun();
return 0;
}
main 的 buf[1000] 在棧上分配,fun 的 buf[10] 在更高位址。寫入 buf[10]~buf[99] 覆蓋了 main 的局部變數區域,但未觸碰返回地址或關鍵控制數據。static int i 放在數據段,非棧上。hello 函數指針寫入未使用的棧空間,恰好未破壞關鍵幀。-fstack-protector-strong -D_FORTIFY_SOURCE=2 (GCC/Clang) 或 /GS (MSVC)。strcpy_s, memcpy_s, scanf_s。cppcheck, Clang Static Analyzer, Coverity。AddressSanitizer (-fsanitize=address) 精準捕獲越界。**不崩潰 ≠ 正確**。越界寫入屬嚴重 Bug,必須修復。