Abort instead of segfault with clear memory violation

后端 未结 6 594
渐次进展
渐次进展 2021-01-13 18:48

I came upon this weird behaviour when dealing with C strings. This is an exercise from the K&R book where I was supposed to write a function that appends one string onto

6条回答
  •  孤独总比滥情好
    2021-01-13 19:36

    Here is the reason why your program didn't crash:

    Your strings are declared as array (s1[] and s2[]). So they're on the stack. And just so happens that memory for s2[] is right after s1[]. So when strcat() is called, all it does is moving each character in s2[] one byte forward. Stack as stack is readable and writable. So there is no restriction what you'e doing.

    But I believe the compiler is free to locate s1[] and s2[] where it see fits so this is just a happy accident.

    Now to get your program to crash is relatively easy

    1. Swap s1 and s2 in your call: instead of strcat(s1, s2), do strcat(s2, s1). This should cause stack smashing exception.
    2. Change s1[] and s2[] to *s1 and *s2. This should cause segfault when you're writing to readonly segment.

提交回复
热议问题