Code: Select all
struct t_tree {
struct t_tree *left, *right;
char name; /* Not just 1 char - space for whole name */
}; /* is allocated. Maybe a little cheesy? */
struct t_dirs {
struct t_dirs *left, *right;
time_t mtime; /* dir's modification time */
struct t_tree *files;
char name; /* ditto */
};
// in searc_directory():
if (!*i) { /* if dir isn't in dir tree, add him */
*i = malloc(sizeof(struct t_dirs) + strlen(dir));
(*i)->left = (*i)->right = NULL;
(*i)->files = NULL;
strcpy(&(*i)->name, dir);
}
So how come the code crashes, with the OS complaint of "buffer overflow"?
Code: Select all
*** buffer overflow detected ***: bin/chessd terminated
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(__fortify_fail+0x45)[0xb770c045]
/lib/i386-linux-gnu/libc.so.6(+0x102e1a)[0xb770ae1a]
/lib/i386-linux-gnu/libc.so.6(+0x10214d)[0xb770a14d]
./lib/chessd.so(search_directory+0xe5)[0xb75b8385]
./lib/chessd.so(+0x17b14)[0xb7585b14]
./lib/chessd.so(process_input+0x62b)[0xb7586eab]
./lib/chessd.so(select_loop+0x9fb)[0xb75a9abb]
bin/chessd(main+0x3ea)[0x8048eba]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb76214d3]
bin/chessd[0x8048fa1]