Skip to content

Commit ea65773

Browse files
chriscoolgitster
authored andcommitted
sha1_file: remove static strbuf from sha1_file_name()
Using a static buffer in sha1_file_name() is error prone and the performance improvements it gives are not needed in many of the callers. So let's get rid of this static buffer and, if necessary or helpful, let's use one in the caller. Suggested-by: Jeff Hostetler <git@jeffhostetler.com> Helped-by: Kevin Daudt <me@ikke.info> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3013dff commit ea65773

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

cache.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -902,12 +902,10 @@ extern void check_repository_format(void);
902902
#define TYPE_CHANGED 0x0040
903903

904904
/*
905-
* Return the name of the file in the local object database that would
906-
* be used to store a loose object with the specified sha1. The
907-
* return value is a pointer to a statically allocated buffer that is
908-
* overwritten each time the function is called.
905+
* Put in `buf` the name of the file in the local object database that
906+
* would be used to store a loose object with the specified sha1.
909907
*/
910-
extern const char *sha1_file_name(const unsigned char *sha1);
908+
extern void sha1_file_name(struct strbuf *buf, const unsigned char *sha1);
911909

912910
/*
913911
* Return an abbreviated sha1 unique within this repository's object database.

http-walker.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,10 @@ static int fetch_object(struct walker *walker, unsigned char *sha1)
544544
} else if (hashcmp(obj_req->sha1, req->real_sha1)) {
545545
ret = error("File %s has bad hash", hex);
546546
} else if (req->rename < 0) {
547-
ret = error("unable to write sha1 filename %s",
548-
sha1_file_name(req->sha1));
547+
struct strbuf buf = STRBUF_INIT;
548+
sha1_file_name(&buf, req->sha1);
549+
ret = error("unable to write sha1 filename %s", buf.buf);
550+
strbuf_release(&buf);
549551
}
550552

551553
release_http_object_request(req);

http.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,7 +2150,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
21502150
unsigned char *sha1)
21512151
{
21522152
char *hex = sha1_to_hex(sha1);
2153-
const char *filename;
2153+
struct strbuf filename = STRBUF_INIT;
21542154
char prevfile[PATH_MAX];
21552155
int prevlocal;
21562156
char prev_buf[PREV_BUF_SIZE];
@@ -2162,14 +2162,15 @@ struct http_object_request *new_http_object_request(const char *base_url,
21622162
hashcpy(freq->sha1, sha1);
21632163
freq->localfile = -1;
21642164

2165-
filename = sha1_file_name(sha1);
2165+
sha1_file_name(&filename, sha1);
21662166
snprintf(freq->tmpfile, sizeof(freq->tmpfile),
2167-
"%s.temp", filename);
2167+
"%s.temp", filename.buf);
21682168

2169-
snprintf(prevfile, sizeof(prevfile), "%s.prev", filename);
2169+
snprintf(prevfile, sizeof(prevfile), "%s.prev", filename.buf);
21702170
unlink_or_warn(prevfile);
21712171
rename(freq->tmpfile, prevfile);
21722172
unlink_or_warn(freq->tmpfile);
2173+
strbuf_release(&filename);
21732174

21742175
if (freq->localfile != -1)
21752176
error("fd leakage in start: %d", freq->localfile);
@@ -2284,6 +2285,7 @@ void process_http_object_request(struct http_object_request *freq)
22842285
int finish_http_object_request(struct http_object_request *freq)
22852286
{
22862287
struct stat st;
2288+
struct strbuf filename = STRBUF_INIT;
22872289

22882290
close(freq->localfile);
22892291
freq->localfile = -1;
@@ -2309,8 +2311,10 @@ int finish_http_object_request(struct http_object_request *freq)
23092311
unlink_or_warn(freq->tmpfile);
23102312
return -1;
23112313
}
2312-
freq->rename =
2313-
finalize_object_file(freq->tmpfile, sha1_file_name(freq->sha1));
2314+
2315+
sha1_file_name(&filename, freq->sha1);
2316+
freq->rename = finalize_object_file(freq->tmpfile, filename.buf);
2317+
strbuf_release(&filename);
23142318

23152319
return freq->rename;
23162320
}

sha1_file.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,11 @@ static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)
251251
}
252252
}
253253

254-
const char *sha1_file_name(const unsigned char *sha1)
254+
void sha1_file_name(struct strbuf *buf, const unsigned char *sha1)
255255
{
256-
static struct strbuf buf = STRBUF_INIT;
257-
258-
strbuf_reset(&buf);
259-
strbuf_addf(&buf, "%s/", get_object_directory());
256+
strbuf_addf(buf, "%s/", get_object_directory());
260257

261-
fill_sha1_path(&buf, sha1);
262-
return buf.buf;
258+
fill_sha1_path(buf, sha1);
263259
}
264260

265261
struct strbuf *alt_scratch_buf(struct alternate_object_database *alt)
@@ -643,7 +639,12 @@ int check_and_freshen_file(const char *fn, int freshen)
643639

644640
static int check_and_freshen_local(const unsigned char *sha1, int freshen)
645641
{
646-
return check_and_freshen_file(sha1_file_name(sha1), freshen);
642+
static struct strbuf buf = STRBUF_INIT;
643+
644+
strbuf_reset(&buf);
645+
sha1_file_name(&buf, sha1);
646+
647+
return check_and_freshen_file(buf.buf, freshen);
647648
}
648649

649650
static int check_and_freshen_nonlocal(const unsigned char *sha1, int freshen)
@@ -799,8 +800,12 @@ static int stat_sha1_file(const unsigned char *sha1, struct stat *st,
799800
const char **path)
800801
{
801802
struct alternate_object_database *alt;
803+
static struct strbuf buf = STRBUF_INIT;
804+
805+
strbuf_reset(&buf);
806+
sha1_file_name(&buf, sha1);
807+
*path = buf.buf;
802808

803-
*path = sha1_file_name(sha1);
804809
if (!lstat(*path, st))
805810
return 0;
806811

@@ -824,8 +829,12 @@ static int open_sha1_file(const unsigned char *sha1, const char **path)
824829
int fd;
825830
struct alternate_object_database *alt;
826831
int most_interesting_errno;
832+
static struct strbuf buf = STRBUF_INIT;
833+
834+
strbuf_reset(&buf);
835+
sha1_file_name(&buf, sha1);
836+
*path = buf.buf;
827837

828-
*path = sha1_file_name(sha1);
829838
fd = git_open(*path);
830839
if (fd >= 0)
831840
return fd;
@@ -1487,9 +1496,12 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
14871496
git_SHA_CTX c;
14881497
unsigned char parano_sha1[20];
14891498
static struct strbuf tmp_file = STRBUF_INIT;
1490-
const char *filename = sha1_file_name(sha1);
1499+
static struct strbuf filename = STRBUF_INIT;
1500+
1501+
strbuf_reset(&filename);
1502+
sha1_file_name(&filename, sha1);
14911503

1492-
fd = create_tmpfile(&tmp_file, filename);
1504+
fd = create_tmpfile(&tmp_file, filename.buf);
14931505
if (fd < 0) {
14941506
if (errno == EACCES)
14951507
return error("insufficient permission for adding an object to repository database %s", get_object_directory());
@@ -1542,7 +1554,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
15421554
warning_errno("failed utime() on %s", tmp_file.buf);
15431555
}
15441556

1545-
return finalize_object_file(tmp_file.buf, filename);
1557+
return finalize_object_file(tmp_file.buf, filename.buf);
15461558
}
15471559

15481560
static int freshen_loose_object(const unsigned char *sha1)

0 commit comments

Comments
 (0)