From e724b058b2fd3d3b58291205bb7aeeccc55c21c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20David=20Ib=C3=A1=C3=B1ez?= Date: Fri, 30 Sep 2011 19:08:48 +0200 Subject: [PATCH 1/4] oid: make git_oid_fromstrn support hex strings of odd length MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes issue #433. Signed-off-by: J. David Ibáñez --- src/oid.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/oid.c b/src/oid.c index e2d16d537..2adaadcbd 100644 --- a/src/oid.c +++ b/src/oid.c @@ -34,15 +34,13 @@ static char to_hex[] = "0123456789abcdef"; int git_oid_fromstrn(git_oid *out, const char *str, size_t length) { size_t p; + int v; if (length > GIT_OID_HEXSZ) length = GIT_OID_HEXSZ; - if (length % 2) - length--; - - for (p = 0; p < length; p += 2) { - int v = (from_hex[(unsigned char)str[p + 0]] << 4) + for (p = 0; p < length - 1; p += 2) { + v = (from_hex[(unsigned char)str[p + 0]] << 4) | from_hex[(unsigned char)str[p + 1]]; if (v < 0) @@ -51,6 +49,12 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length) out->id[p / 2] = (unsigned char)v; } + if (length % 2) { + v = (from_hex[(unsigned char)str[p + 0]] << 4); + out->id[p / 2] = (unsigned char)v; + p += 2; + } + for (; p < GIT_OID_HEXSZ; p += 2) out->id[p / 2] = 0x0; From 6d8d3f195c9377a1fdb3cbde32ec4a677d93b588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20David=20Ib=C3=A1=C3=B1ez?= Date: Fri, 30 Sep 2011 19:41:29 +0200 Subject: [PATCH 2/4] oid: optimize git_oid_fromstrn by using memset MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: J. David Ibáñez --- src/oid.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/oid.c b/src/oid.c index 2adaadcbd..1f7227410 100644 --- a/src/oid.c +++ b/src/oid.c @@ -55,8 +55,7 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length) p += 2; } - for (; p < GIT_OID_HEXSZ; p += 2) - out->id[p / 2] = 0x0; + memset(out->id + p / 2, 0, (GIT_OID_HEXSZ - p) / 2); return GIT_SUCCESS; } From b9caa1859d9116959a1aafdb8dc3f6578d692459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20David=20Ib=C3=A1=C3=B1ez?= Date: Fri, 30 Sep 2011 19:50:13 +0200 Subject: [PATCH 3/4] oid: now git_oid_fromstrn checks whether the given string is too short MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: J. David Ibáñez --- src/oid.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/oid.c b/src/oid.c index 1f7227410..08e9305b5 100644 --- a/src/oid.c +++ b/src/oid.c @@ -36,6 +36,9 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length) size_t p; int v; + if (length < 4) + return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is too short"); + if (length > GIT_OID_HEXSZ) length = GIT_OID_HEXSZ; From 0e058e789b5a4a153f9fb5d14860fc38551f0c66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20David=20Ib=C3=A1=C3=B1ez?= Date: Sun, 2 Oct 2011 21:40:57 +0200 Subject: [PATCH 4/4] oid: add missing check to git_oid_fromstrn MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: J. David Ibáñez --- src/oid.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/oid.c b/src/oid.c index 08e9305b5..b47fa2c77 100644 --- a/src/oid.c +++ b/src/oid.c @@ -54,6 +54,9 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length) if (length % 2) { v = (from_hex[(unsigned char)str[p + 0]] << 4); + if (v < 0) + return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is not a valid sha1 hash"); + out->id[p / 2] = (unsigned char)v; p += 2; }