I have some C code that contains an array of text that I'm trying to manipulate in the following manner :-
- Allocate an array of pointers
dictionary
of sizedictionary_size
initialized to 50 - Replace all spaces and \n's with '\0'
- Store the address of every 3rd string(separated by an unknown number of \n's or spaces) in
dictionary
- If
dictionary
is full, realloc to size,dictionary_size * 2
The code however, causes the following error :-
*** glibc detected *** ./crack: realloc(): invalid next size: 0x0000000001386010 ***
^Cmake: *** [run] Interrupt
The code is as follows :-
// Replace all spaces with '\0'
for ( i = 0; i < file_size; i++ ) {
if ( temp_buffer[i] == ' ' || temp_buffer[i] == '\n' ) {
while ( temp_buffer[i] == ' ' || temp_buffer[i] == '\n' ) {
temp_buffer[i] = '\0';
}
j++;
}
if ( (j-1) % 3 == 0 ) {
dictionary[k] = temp_buffer+i;
k += 1;
if ( k == dictionary_size ) {
dictionary_size *= 2;
printf("Going to realloc to %d\n", dictionary_size);
dictionary = (char **)realloc(dictionary, dictionary_size);
}
}
}
[EDIT] Based on the debugging statements I have, the very first realloc(to a size of 100) fails.