c++ - stack smashing detected by valgrind -
a stack smashing detected in main function in c++ code... here body of main:
int main() { long int acn; char dot[15]; float open_balance=1; char k; int total_account=0; int c; static int ac=10000; transaction trn; back upwards sprt; do{ cout<<"\n1.new account\n2. transaction\n3. exit\n\nenter choice:"; cin>>k; switch(k) { case '1': ac+=1; time_t rawtime; time(&rawtime); strcpy(dot,ctime(&rawtime)); do{ if(open_balance<=0) cout<<"opening balance can not less zero"; cout<<"\nenter opening balance :"; cin>>open_balance; }while(open_balance<=0); bln[total_account].get_data(ac,open_balance,dot); ++total_account; break; case '2': trn.trans(total_account); break; case '3': break; default : cout<<"\nwrong choice!!"; } }while(k!='3'); cout<<"thank you"; return(0); } when run code through valgrind finds stack smashing can't find memory leak. valgrind report:
1.new business relationship 2. transaction 3. exit
enter choice:3 * stack smashing detected *: ./a.out terminated give thanks you==9813==
==9813== heap summary:
==9813== in utilize @ exit: 0 bytes in 0 blocks
==9813== total heap usage: 10 allocs, 10 frees, 954 bytes allocated
==9813==
==9813== heap blocks freed -- no leaks possible
==9813==
==9813== counts of detected , suppressed errors, rerun with: -v
==9813== error summary: 0 errors 0 contexts (suppressed: 0 0) aborted (core dumped)
where going wrong?
it's line strcpy(dot,ctime(&rawtime)); causes stack smeshing.function ctime returns string alike "wed jun 30 21:49:08 1993\n", length more 15 bytes, , need more bytes store result of ctime.strcpy not check margin of target memory, considered dangerous, alternative strncpy suggested instead. and, if programme runs more 1 thread, ctime_r preferred.
c++ memory-leaks valgrind main stack-dump
No comments:
Post a Comment