threads / patch / 2908

patchsha1_to_hex: properly terminate the SHA1

Subject: [PATCH] sha1_to_hex: properly terminate the SHA1

## tl;dr

2 messages between Dec 22, 2005 and Dec 22, 2005. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Johannes Schindelin· Dec 22, 2005, 17:55 UTC · lore

sha1_to_hex() returns a pointer to a static buffer. Some of its users modify that buffer by appending a newline character. Other users rely on the fact that you can call

	printf("%s", sha1_to_hex(sha1));
Just to be on the safe side, terminate the SHA1 in sha1_to_hex().
Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
---
 sha1_file.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
3a743c154546e4937b4afdc7848d9943ec2f4148
Show changes to sha1_file.c +2 −0
diff --git a/sha1_file.c b/sha1_file.c
index 6011473..d451a94 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -81,6 +81,8 @@ char * sha1_to_hex(const unsigned char *
 		*buf++ = hex[val >> 4];
 		*buf++ = hex[val & 0xf];
 	}
+	*buf = '\0';
+
 	return buffer;
 }
 
-- 
1.0.0
Junio C Hamano· Dec 22, 2005, 20:51 UTC · re: Johannes Schindelin · lore

Re: [PATCH] sha1_to_hex: properly terminate the SHA1

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 7 quoted lines
> sha1_to_hex() returns a pointer to a static buffer. Some of its users
> modify that buffer by appending a newline character. Other users rely
> on the fact that you can call
>
> 	printf("%s", sha1_to_hex(sha1));
>
> Just to be on the safe side, terminate the SHA1 in sha1_to_hex().
Good catch, thanks.  The worst one is:
diff.c:727:		memcpy(one_sha1, sha1_to_hex(one->sha1), 41);

← back to recent threads