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>
This commit is contained in:
committed by
Junio C Hamano
parent
399ad553ce
commit
af49c6d091
13
hex.c
13
hex.c
@@ -61,12 +61,10 @@ int get_oid_hex(const char *hex, struct object_id *oid)
|
||||
return get_sha1_hex(hex, oid->hash);
|
||||
}
|
||||
|
||||
char *sha1_to_hex(const unsigned char *sha1)
|
||||
char *sha1_to_hex_r(char *buffer, const unsigned char *sha1)
|
||||
{
|
||||
static int bufno;
|
||||
static char hexbuffer[4][GIT_SHA1_HEXSZ + 1];
|
||||
static const char hex[] = "0123456789abcdef";
|
||||
char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
|
||||
char *buf = buffer;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < GIT_SHA1_RAWSZ; i++) {
|
||||
@@ -79,6 +77,13 @@ char *sha1_to_hex(const unsigned char *sha1)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
char *sha1_to_hex(const unsigned char *sha1)
|
||||
{
|
||||
static int bufno;
|
||||
static char hexbuffer[4][GIT_SHA1_HEXSZ + 1];
|
||||
return sha1_to_hex_r(hexbuffer[3 & ++bufno], sha1);
|
||||
}
|
||||
|
||||
char *oid_to_hex(const struct object_id *oid)
|
||||
{
|
||||
return sha1_to_hex(oid->hash);
|
||||
|
||||
Reference in New Issue
Block a user