Change 1:
I will try to explain how you can find the answer for yourself. I'm not sure what actually happens, as this is not a defined behavior (as others claim), but you can do a simple debugging to figure out what your compiler does.
Original answer
I guess both of them are on the stack. You can verify this by changing the code as follows:
int main() { char c1 = 'X'; char s1[] = "hello"; char s2[] = "eheheheheheh"; char c2 = '3'; printf("%s\n", strcat(s1, s2)); }
c1 and c2 will be on the stack. Knowing that you can check if s1 and s2 .
If the address c1 less than s1 , and the address s1 less than c2 , then it is on the stack. Otherwise, it is probably in your .bss section (which would be reasonable, but would break the recursion).
The reason I tackle the lines on the stack is because if you change them in a function, and this function calls itself, then the second call will not have its own copy of the lines and therefore will not be valid .. However, the compiler still knows that this function is not recursive and can put strings in .bss so that I can be wrong.
Assuming I'm assuming he's on the stack right in your code
int main() { char s1[] = "hello"; char s2[] = "eheheheheheh"; printf("%s\n", strcat(s1, s2)); }
"hello" (with a null terminator) is "eheheheheheh" stack, followed by "eheheheheheh" (with a null terminator).
They are both located one after the other (due to the simplicity of the order in which you wrote them), forming a single block of memory that you can write (but should not!) ... That's why there is no seg, you can see it, crashed before printf and looking at addresses.
s2 == (uintptr_t)s1 + (strlen(s1) + 1) should be right if I am right.
Change code with
int main() { char s1[] = "hello"; char c = '3'; char s2[] = "eheheheheheh"; printf("%s\n", strcat(s1, s2)); }
Must see c overwritten if I'm right ...
However, if I am mistaken and located in the .bss section, they can still be contiguous, and you will overwrite them without seg failing.
If you really want to know this, disassemble it:
Unfortunately, I only know how to do this on Linux. Try using nm <binary> > <text file>.txt or objdump -t <your_binary> > <text file>.sym to unload all the characters from your program. Teams must also provide a section where each character is located.
Find the file for the characters s1 and s2 , if you do not find them, this should mean that they are on the stack, but we will check this in the next step.
Use the objdump -S your_binary > text_file.S (make sure you have created your binary with debugging symbols), and then open the .S file in a text editor.
Find the characters s1 and s2 again (hope there are no others, I suspect not, but I'm not sure).
If you find their definitions followed by push or sub %esp , then they are on the stack. If you are not sure what their definitions mean, post it here and see.