In the simple program below command is pointing to 400 bytes on the heap. Then I copy "./search '" to command, *buffer points to the next byte after " ' " (single quote). Starting the memory pointed by buffer I use memset to set 300 bytes to value 0x41 (ASCII 'A'), then I append the closing single quote.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
int main(int argc, char *argv[]) {
char *command = (char *)malloc(400);
bzero(command, 400);
strcpy(command, "./search \'");
char *buffer = command + strlen(command);
memset(buffer, 0x41, 300);
strcat(command, "\'");
system(command);
free(command);
}
But when I look at *command and *buffer in gdb this is what I see.
char * command 0x601010 "./search '", 'A' <repeats 186 times>...
char * buffer 0x60101e 'A' <repeats 200 times>...
First I was expecting it to say repeats 299 times and second I was expecting both command and buffer repeats to be of similar value. Can someone please tell me what am I missing?