Skip to content

Commit 8bdaecb

Browse files
mhaggergitster
authored andcommitted
try_remove_empty_parents(): don't trash argument contents
It's bad manners and surprising and therefore error-prone. Signed-off-by: Michael Haggerty <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 730e034 commit 8bdaecb

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

refs/files-backend.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,33 +2282,35 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
22822282

22832283
/*
22842284
* Remove empty parents, but spare refs/ and immediate subdirs.
2285-
* Note: munges *refname.
22862285
*/
2287-
static void try_remove_empty_parents(char *refname)
2286+
static void try_remove_empty_parents(const char *refname)
22882287
{
2288+
struct strbuf buf = STRBUF_INIT;
22892289
char *p, *q;
22902290
int i;
2291-
p = refname;
2291+
2292+
strbuf_addstr(&buf, refname);
2293+
p = buf.buf;
22922294
for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
22932295
while (*p && *p != '/')
22942296
p++;
22952297
/* tolerate duplicate slashes; see check_refname_format() */
22962298
while (*p == '/')
22972299
p++;
22982300
}
2299-
for (q = p; *q; q++)
2300-
;
2301+
q = buf.buf + buf.len;
23012302
while (1) {
23022303
while (q > p && *q != '/')
23032304
q--;
23042305
while (q > p && *(q-1) == '/')
23052306
q--;
23062307
if (q == p)
23072308
break;
2308-
*q = '\0';
2309-
if (rmdir(git_path("%s", refname)))
2309+
strbuf_setlen(&buf, q - buf.buf);
2310+
if (rmdir(git_path("%s", buf.buf)))
23102311
break;
23112312
}
2313+
strbuf_release(&buf);
23122314
}
23132315

23142316
/* make sure nobody touched the ref, and unlink */

0 commit comments

Comments
 (0)