I am working out of the 2nd edition of Jon Erickson's "Hacking: The Art of Exploitation" using a VM (virutalbox) to run the LiveCD it came with (Ubuntu 7.04). In section 0x272 "Using the Heap", the author explains the malloc() and free() functions using an example on pages 77-79.
The code for the heap_example.c is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[]){
char *char_ptr;
int *int_ptr;
int mem_size;
if(argc < 2)
mem_size = 50;
else
mem_size = atoi(argv[1]);
printf("\t[+]allocating %d bytes of memory on the heap for char_ptr\n",mem_size);
char_ptr = (char *)malloc(mem_size);
if(char_ptr == NULL){ //Error checking,in case malloc() fails
fprintf(stderr,"Error:could not allocate heap memory.\n");
exit(-1);
}
strcpy(char_ptr,"This is memory is located on the heap.");
printf("char_ptr(%p)-->'%s'\n",char_ptr,char_ptr);
printf("\t[+]allocating 12 bytes of memory on the heap for int_ptr\n");
int_ptr = (int *)malloc(12);
if(int_ptr == NULL){
fprintf(stderr,"Error:could not allocate heap memory.\n");
exit(-1);
}
*int_ptr = 31337;
printf("int_ptr (%p)--> %d\n",int_ptr,*int_ptr);
printf("\t[-]freeing char_ptr's heap memory...\n");
free(char_ptr);
printf("\t[+]allocating another 15 bytes for char_ptr\n");
char_ptr = (char*)malloc(15);
if(char_ptr == NULL){
fprintf(stderr,"Error:could not allocate heap memory.\n");
exit(-1);
}
strcpy(char_ptr,"new memory");
printf("char_ptr (%p)-->'%s'\n",char_ptr,char_ptr);
printf("\t[-]freeing int_ptr's heap memory...\n");
free(int_ptr);
printf("\t[-]freeing char_ptr's heap memory...\n");
free(char_ptr);
}
However, when I type the following instructions stated in the book into my terminal window:
reader@hacking:~/booksrc $ gcc -o heap_example heap_example.c
It spits out the following error message regarding syntax errors in my stdlib.h
In file included from heap_example.c:2:
/usr/include/stdlib.h:469: error: syntax error before "int32_t"
/usr/include/stdlib.h:471: error: syntax error before '*' token
/usr/include/stdlib.h:475: error: syntax error before '*' token
/usr/include/stdlib.h:476: error: syntax error before '}' token
/usr/include/stdlib.h:479: error: syntax error before "int32_t"
Here is where the error seems to take place in my stdlib.h (lines 467 - 479)
struct random_data
{
int32_t *fptr; /* Front pointer. */
int32_t *rptr; /* Rear pointer. */
int32_t *state; /* Array of state values. */
int rand_type; /* Type of random number generator. */
int rand_deg; /* Degree of random number generator. */
int rand_sep; /* Distance between front and rear. */
int32_t *end_ptr; /* Pointer behind state table. */
};
extern int random_r (struct random_data *__restrict __buf,
int32_t *__restrict __result) __THROW __nonnull ((1, 2));
Then when I try to run it:
reader@hacking:~/booksrc $ ./heap_example
I get this:
bash: ./heap_example: No such file or directory
I believe that heap_example.c is the first file in the book that includes the stdlib.h (all the prior examples used only stdio.h, so I had no issues).
Will changing the stdlib.h file fix this problem? If so, how might I go about correcting these errors? I figured I should fix this issue ASAP as the rest of the code examples in this book will probably start to include stdlib.h
Thanks!