Skip to content

Commit 8cee8c2

Browse files
committed
Fix a couple tar --sort leaks.
1 parent 71b95f7 commit 8cee8c2

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

lib/dirtree.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ static struct dirtree *dirtree_handle_callback(struct dirtree *new,
125125
flags = callback(new);
126126

127127
if (S_ISDIR(new->st.st_mode) && (flags & df)) {
128+
// TODO: check openat returned fd for errors... and do what about it?
128129
if (*new->name) fd = openat(dirtree_parentfd(new), new->name, O_CLOEXEC);
129130
if (flags&DIRTREE_BREADTH) {
130131
new->again |= DIRTREE_BREADTH;
@@ -133,10 +134,11 @@ static struct dirtree *dirtree_handle_callback(struct dirtree *new,
133134
return DIRTREE_ABORTVAL;
134135
}
135136
flags = dirtree_recurse(new, callback, fd, flags);
137+
close(fd);
136138
}
137139

138140
// Free node that didn't request saving and has no saved children.
139-
if (!new->child && !(flags & DIRTREE_SAVE) && (!new->parent || !(new->parent->again&DIRTREE_BREADTH))) {
141+
if (!new->child && !(flags & DIRTREE_SAVE)) {
140142
free(new);
141143
new = 0;
142144
}
@@ -145,7 +147,7 @@ static struct dirtree *dirtree_handle_callback(struct dirtree *new,
145147
}
146148

147149
// Recursively read/process children of directory node, filtering through
148-
// callback(). Uses and closes supplied ->dirfd.
150+
// callback().
149151

150152
int dirtree_recurse(struct dirtree *node,
151153
int (*callback)(struct dirtree *node), int dirfd, int flags)

toys/posix/tar.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ static int add_to_tar(struct dirtree *node)
287287
free(name);
288288

289289
return DIRTREE_BREADTH;
290-
} else if (node->again&DIRTREE_BREADTH) {
290+
} else if ((node->again&DIRTREE_BREADTH) && node->child) {
291291
struct dirtree *dt, **sort = xmalloc(sizeof(void *)*node->extra);
292292

293293
for (node->extra = 0, dt = node->child; dt; dt = dt->next)

0 commit comments

Comments
 (0)