bnemias wrote:syzygy wrote:What bug? The code is not buggy.
name is defined as a char, but it's not a char. in fact, why even have that member when you could just index the memory directly with casts and pointer addition? name is merely a placeholder for something else.
If you consider using a variable in ways it wasn't defined not to be a bug, be my guest.
I thought you were talking about a bug that needs to be tracked and fixed.
Anyway, I don't consider code that always does exactly what it was intended to do buggy. And the char ends up storing a char. Add [1] behind it and you have a code construct that has been used since forever (but is not completely supported by the standard, a problem that hgm's code does not have).
But how about this?
Code: Select all
struct t_tree {
struct t_tree *left, *right;
char name[];
};
struct t_dirs {
struct t_dirs *left, *right;
time_t mtime; /* dir's modification time */
struct t_tree *files;
char name[];
};
// in searc_directory():
if (!*p) { /* if dir isn't in dir tree, add him */
*p = malloc(sizeof(struct t_dirs) + strlen(dir) + 1);
(*p)->left = (*p)->right = NULL;
(*p)->files = NULL;
strcpy((*p)->name, dir);
}
Buggy? Poor coding practice? Or fine?
syzygy wrote:And again, this is C. In C everything is dangerous.
C isn't inherently dangerous. Using risky code is. Just because it doesn't explicitly prohibit bad behavior doesn't mean it can't be used safely. Writing solid code is a mindset irrespective of language.
How is the above code more risky than:
Code: Select all
struct t_tree {
struct t_tree *left, *right;
char *name;
};
struct t_dirs {
struct t_dirs *left, *right;
time_t mtime; /* dir's modification time */
struct t_tree *files;
char *name;
};
// in searc_directory():
if (!*p) { /* if dir isn't in dir tree, add him */
*p = malloc(sizeof(struct t_dirs));
(*p)->left = (*p)->right = NULL;
(*p)->files = NULL;
(*p)->name = malloc(strlen(dir) + 1);
strcpy((*p)->name, dir);
}
Now you have to remember free()ing (*p)->name, so I only see more "risk".
I have nothing against solid code. My point is the code was already solid.