I'm having some trouble with a problem I found. Given the following:
int match(char *s1, char *s2) {
while( *s1 != '\0' && *s2 != '\0' && *s1 == *s2 ){
s1++; s2++;
}
return( *s1 - *s2 );
}
int main() {
char str1[8], str2[8];
scanf("%s", str1);
scanf("%s", str2);
if (match(str1, str2) == 0)
printf("They are the same.\n");
else
printf("They are not the same.\n");
}
What two input strings of different values can be used to cause the program to print the message "They are the same"? (The code above can not be altered)
I understand that when the arrays are added to the stack, they are "pushed" into it and information is written in the same direction. So if I were to enter "AAAAAAAAA" (A x 9) to str2, it would overflow and str1 would print "A".
My first attempts were to enter A x 16 for str2, hoping that the program would overwrite the value in str1 with 8 A's and the program would only read 8 values in str2. str1 did have a value of A x 8, but str2 retained its value of A x 16.
Is there a way to use this to solve this problem? Or am I thinking about this the wrong way?
EDIT: This problem was meant to be run on a specific machine with an outdated, therefore vulnerable, version of Linux. I've run the program through gdb and it shows that the two strings are next to each other in memory and that str2 is overflowing into str1. My question then, is can I use this to make str2 and str1 look identical to the program when it compares them?