c - Overwriting of stack static array when the other static array overflows -
#include<stdio.h> int main(){ char a[10]; char b[10]; sprintf(a,"hello"); sprintf(b,"aaaaaaaaaabbbbbbbbbbcccccccccc"); printf("%s:%s\n",a,b); return(0); } (gdb) p &a $1 = (char (*)[10]) 0x7fffffffe450 (gdb) p &b $2 = (char (*)[10]) 0x7fffffffe440 (gdb) step 2: b = "aaaaaaaaaa" 1: = "bbbbcccccc" printf o/p- bbbbcccccccccc:aaaaaaaaaabbbbbbbbbbcccccccccc
questions-
a comes first in stack frame , b comes later. if b overwriting "bbbbbbbbbb"
should go why starting "bbbbcccccccccc"
?
also want know if overwrite bp, program terminate?
sure b
comes "after" a
in stack, stack (often, , assumedly on platform based on print-outs) growns downwards. notice address of b
less of a
.
so overwrite of b
goes a
.
also think gdb being clever , printing 10 characters b
, since prints 10 a
's , there's no termination.
you can't overwrite processor register using rogue string formatting, registers not in memory (in general, , not on x86 might using).
note code invokes undefined behavior, terminate reason.
Comments
Post a Comment