mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-20 23:48:28 +00:00
git_oid_fromstrn: Simplify the implementation and fix memory access issues
This commit is contained in:
parent
c81e4adfd7
commit
00258cc0b6
24
src/oid.c
24
src/oid.c
@ -24,30 +24,24 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
|
|||||||
size_t p;
|
size_t p;
|
||||||
int v;
|
int v;
|
||||||
|
|
||||||
|
assert(out && str);
|
||||||
|
|
||||||
|
if (!length)
|
||||||
|
return oid_error_invalid("too short");
|
||||||
|
|
||||||
if (length > GIT_OID_HEXSZ)
|
if (length > GIT_OID_HEXSZ)
|
||||||
return oid_error_invalid("too long");
|
return oid_error_invalid("too long");
|
||||||
|
|
||||||
for (p = 0; p < length - 1; p += 2) {
|
memset(out->id, 0, GIT_OID_RAWSZ);
|
||||||
v = (git__fromhex(str[p + 0]) << 4)
|
|
||||||
| git__fromhex(str[p + 1]);
|
|
||||||
|
|
||||||
|
for (p = 0; p < length; p++) {
|
||||||
|
v = git__fromhex(str[p]);
|
||||||
if (v < 0)
|
if (v < 0)
|
||||||
return oid_error_invalid("contains invalid characters");
|
return oid_error_invalid("contains invalid characters");
|
||||||
|
|
||||||
out->id[p / 2] = (unsigned char)v;
|
out->id[p / 2] |= (unsigned char)(v << (p % 2 ? 0 : 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (length % 2) {
|
|
||||||
v = (git__fromhex(str[p + 0]) << 4);
|
|
||||||
if (v < 0)
|
|
||||||
return oid_error_invalid("contains invalid characters");
|
|
||||||
|
|
||||||
out->id[p / 2] = (unsigned char)v;
|
|
||||||
p += 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(out->id + p / 2, 0, (GIT_OID_HEXSZ - p) / 2);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user