malloc() in C not working as expected

前端 未结 7 717
别那么骄傲
别那么骄傲 2021-01-25 04:52

I\'m new to C. Sorry if this has already been answered, I could\'n find a straight answer, so here we go..

I\'m trying to understand how malloc() works in C. I have this

7条回答
  •  感情败类
    2021-01-25 05:56

    Yes writing more data into an allocated buffer is a buffer overflow. However there is no buffer overflow check in C and if there happens to be valid memory after your buffer than your code will appear to work correctly.

    However what you have done is write into memory that you don't own and likely have corrupted the heap. Your next call to free or malloc will likely crash, or if not the next call, some later call could crash, or you could get lucky and malloc handed you a larger buffer than you requested, in which case you'll never see an issue.

提交回复
热议问题