diff options
author | Vicent Martà <tanoku@gmail.com> | 2011-10-03 14:40:06 -0700 |
---|---|---|
committer | Vicent Martà <tanoku@gmail.com> | 2011-10-03 14:40:06 -0700 |
commit | ef1e5da127e3d516933a54808943d17abfe6441f (patch) | |
tree | 5b1f616ab2add5a4676c3d52d10393f9af619026 | |
parent | cd19ca9584bd01925e05e94e7f3bddae6880acda (diff) | |
parent | 0e058e789b5a4a153f9fb5d14860fc38551f0c66 (diff) | |
download | libgit2-ef1e5da127e3d516933a54808943d17abfe6441f.tar.gz |
Merge pull request #438 from jdavid/development
Make git_oid_fromstrn support hex strings of odd length
-rw-r--r-- | src/oid.c | 23 |
1 files changed, 16 insertions, 7 deletions
@@ -34,15 +34,16 @@ 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 < 4) + return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is too short"); 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,8 +52,16 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length) out->id[p / 2] = (unsigned char)v; } - for (; p < GIT_OID_HEXSZ; p += 2) - out->id[p / 2] = 0x0; + 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; + } + + memset(out->id + p / 2, 0, (GIT_OID_HEXSZ - p) / 2); return GIT_SUCCESS; } |