Skip to content

Commit bda3a31

Browse files
committed
reflog-expire: Avoid creating new files in a directory inside readdir(3) loop
"git reflog expire --all" opened a directory in $GIT_DIR/logs/, read reflog files in there readdir(3), and rewrote the file by creating a new file and renaming it back inside the loop. This code structure can cause the newly created file to be returned by subsequent call to readdir(3), and fall into an infinite loop in the worst case. This separates the processing to two phase. Running for_each_reflog() to find out and collect all refs, and then iterate over them, calling expire_reflog(). This way, the program would behave exactly the same way as if all the refs were given by the user from the command line. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7720224 commit bda3a31

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

builtin-reflog.c

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ struct expire_reflog_cb {
3434
struct cmd_reflog_expire_cb *cmd;
3535
};
3636

37+
struct collected_reflog {
38+
unsigned char sha1[20];
39+
char reflog[FLEX_ARRAY];
40+
};
41+
struct collect_reflog_cb {
42+
struct collected_reflog **e;
43+
int alloc;
44+
int nr;
45+
};
46+
3747
#define INCOMPLETE (1u<<10)
3848
#define STUDYING (1u<<11)
3949

@@ -281,6 +291,20 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
281291
return status;
282292
}
283293

294+
static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
295+
{
296+
struct collected_reflog *e;
297+
struct collect_reflog_cb *cb = cb_data;
298+
size_t namelen = strlen(ref);
299+
300+
e = xmalloc(sizeof(*e) + namelen + 1);
301+
hashcpy(e->sha1, sha1);
302+
memcpy(e->reflog, ref, namelen + 1);
303+
ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
304+
cb->e[cb->nr++] = e;
305+
return 0;
306+
}
307+
284308
static int reflog_expire_config(const char *var, const char *value)
285309
{
286310
if (!strcmp(var, "gc.reflogexpire"))
@@ -349,8 +373,20 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
349373
putchar('\n');
350374
}
351375

352-
if (do_all)
353-
status |= for_each_reflog(expire_reflog, &cb);
376+
if (do_all) {
377+
struct collect_reflog_cb collected;
378+
int i;
379+
380+
memset(&collected, 0, sizeof(collected));
381+
for_each_reflog(collect_reflog, &collected);
382+
for (i = 0; i < collected.nr; i++) {
383+
struct collected_reflog *e = collected.e[i];
384+
status |= expire_reflog(e->reflog, e->sha1, 0, &cb);
385+
free(e);
386+
}
387+
free(collected.e);
388+
}
389+
354390
while (i < argc) {
355391
const char *ref = argv[i++];
356392
unsigned char sha1[20];

0 commit comments

Comments
 (0)