Skip to content
Snippets Groups Projects
Commit a830f7df authored by Yorhel's avatar Yorhel
Browse files

Use C99 flexible array member for struct dir

This should fix https://dev.yorhel.nl/ncdu/bug/99 - with the downside
that this requires a C99 compiler.

I also replaced all occurrences of static allocation of struct dir with
use dynamic allocation, because I wasn't really sure if static
allocation of flexible structs is allowed. In the case of dirlist.c the
dynamic allocation is likely required anyway, because it does store a
few bytes in the name field.
parent 3b55f8c1
Branches
Tags
No related merge requests found
...@@ -431,10 +431,13 @@ static int itemdir(uint64_t dev) { ...@@ -431,10 +431,13 @@ static int itemdir(uint64_t dev) {
static int iteminfo(struct dir **item, uint64_t dev, int isdir) { static int iteminfo(struct dir **item, uint64_t dev, int isdir) {
static struct dir dir; static struct dir *dirbuf;
struct dir *tmp, *d = &dir; struct dir *tmp, *d;
uint64_t iv; uint64_t iv;
if(!dirbuf)
dirbuf = malloc(sizeof(struct dir));
d = dirbuf;
memset(d, 0, sizeof(struct dir)); memset(d, 0, sizeof(struct dir));
d->flags |= isdir ? FF_DIR : FF_FILE; d->flags |= isdir ? FF_DIR : FF_FILE;
d->dev = dev; d->dev = dev;
......
...@@ -40,8 +40,7 @@ int dirlist_sort_desc = 1, ...@@ -40,8 +40,7 @@ int dirlist_sort_desc = 1,
dirlist_hidden = 0; dirlist_hidden = 0;
/* private state vars */ /* private state vars */
static struct dir dirlist_parent_alloc; static struct dir *parent_alloc, *head, *head_real, *selected, *top = NULL;
static struct dir *head, *head_real, *selected, *top = NULL;
...@@ -206,14 +205,15 @@ void dirlist_open(struct dir *d) { ...@@ -206,14 +205,15 @@ void dirlist_open(struct dir *d) {
head_real = head = dirlist_sort(head); head_real = head = dirlist_sort(head);
/* set the reference to the parent dir */ /* set the reference to the parent dir */
dirlist_parent_alloc.flags &= ~FF_BSEL;
dirlist_parent_alloc.flags |= FF_DIR;
if(d->parent) { if(d->parent) {
dirlist_parent = &dirlist_parent_alloc; if(!parent_alloc)
parent_alloc = calloc(1, SDIRSIZE + 3);
dirlist_parent = parent_alloc;
strcpy(dirlist_parent->name, ".."); strcpy(dirlist_parent->name, "..");
dirlist_parent->next = head; dirlist_parent->next = head;
dirlist_parent->parent = d; dirlist_parent->parent = d;
dirlist_parent->sub = d; dirlist_parent->sub = d;
dirlist_parent->flags = FF_DIR;
head = dirlist_parent; head = dirlist_parent;
} else } else
dirlist_parent = NULL; dirlist_parent = NULL;
......
...@@ -66,7 +66,7 @@ struct dir { ...@@ -66,7 +66,7 @@ struct dir {
struct dir *parent, *next, *prev, *sub, *hlnk; struct dir *parent, *next, *prev, *sub, *hlnk;
int items; int items;
unsigned char flags; unsigned char flags;
char name[3]; /* must be large enough to hold ".." */ char name[];
}; };
/* sizeof(total dir) = SDIRSIZE + strlen(name) = offsetof(struct dir, name) + strlen(name) + 1 */ /* sizeof(total dir) = SDIRSIZE + strlen(name) = offsetof(struct dir, name) + strlen(name) + 1 */
#define SDIRSIZE (offsetof(struct dir, name)+1) #define SDIRSIZE (offsetof(struct dir, name)+1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment