summaryrefslogtreecommitdiff
path: root/sha1_name.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2015-09-24 21:05:45 (GMT)
committerJunio C Hamano <gitster@pobox.com>2015-09-25 17:18:18 (GMT)
commitaf49c6d0918bf04aad89bd885a4eef5767a33d0e (patch)
tree5037171be3cd4be197a9fafdbbcec9b305b96f99 /sha1_name.c
parent399ad553ce87fca77a9bc5a0e734a361a9e8a5a3 (diff)
downloadgit-af49c6d0918bf04aad89bd885a4eef5767a33d0e.zip
git-af49c6d0918bf04aad89bd885a4eef5767a33d0e.tar.gz
git-af49c6d0918bf04aad89bd885a4eef5767a33d0e.tar.bz2
add reentrant variants of sha1_to_hex and find_unique_abbrev
The sha1_to_hex and find_unique_abbrev functions always write into reusable static buffers. There are a few problems with this: - future calls overwrite our result. This is especially annoying with find_unique_abbrev, which does not have a ring of buffers, so you cannot even printf() a result that has two abbreviated sha1s. - if you want to put the result into another buffer, we often strcpy, which looks suspicious when auditing for overflows. This patch introduces sha1_to_hex_r and find_unique_abbrev_r, which write into a user-provided buffer. Of course this is just punting on the overflow-auditing, as the buffer obviously needs to be GIT_SHA1_HEXSZ + 1 bytes. But it is much easier to audit, since that is a well-known size. We retain the non-reentrant forms, which just become thin wrappers around the reentrant ones. This patch also adds a strbuf variant of find_unique_abbrev, which will be handy in later patches. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'sha1_name.c')
-rw-r--r--sha1_name.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/sha1_name.c b/sha1_name.c
index da6874c..c58b477 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -368,14 +368,13 @@ int for_each_abbrev(const char *prefix, each_abbrev_fn fn, void *cb_data)
return ds.ambiguous;
}
-const char *find_unique_abbrev(const unsigned char *sha1, int len)
+int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)
{
int status, exists;
- static char hex[41];
- memcpy(hex, sha1_to_hex(sha1), 40);
+ sha1_to_hex_r(hex, sha1);
if (len == 40 || !len)
- return hex;
+ return 40;
exists = has_sha1_file(sha1);
while (len < 40) {
unsigned char sha1_ret[20];
@@ -384,10 +383,17 @@ const char *find_unique_abbrev(const unsigned char *sha1, int len)
? !status
: status == SHORT_NAME_NOT_FOUND) {
hex[len] = 0;
- return hex;
+ return len;
}
len++;
}
+ return len;
+}
+
+const char *find_unique_abbrev(const unsigned char *sha1, int len)
+{
+ static char hex[GIT_SHA1_HEXSZ + 1];
+ find_unique_abbrev_r(hex, sha1, len);
return hex;
}