Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61d7328

Browse files
committed
object: avoid call of memset with ouf of bounds pointer
When computing a short OID we do this by first copying the leading parts into the new OID structure and then setting the trailing part to zero. In the case of the desired length being `GIT_OID_HEXSZ - 1` we will call `memset` with an out of bounds pointer and a length of 0. While this seems to cause no problems for common platforms the C89 standard does not explicitly state that calling `memset` with an out of bounds pointer and length of 0 is valid. Fix the potential issue by using the newly introduced `git_oid__cpy_prefix` function.
1 parent e126bc9 commit 61d7328

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

src/object.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "commit.h"
1313
#include "tree.h"
1414
#include "blob.h"
15+
#include "oid.h"
1516
#include "tag.h"
1617

1718
bool git_object__strict_input_validation = true;
@@ -166,13 +167,9 @@ int git_object_lookup_prefix(
166167
error = git_odb_read(&odb_obj, odb, id);
167168
}
168169
} else {
169-
git_oid short_oid;
170+
git_oid short_oid = {{ 0 }};
170171

171-
/* We copy the first len*4 bits from id and fill the remaining with 0s */
172-
memcpy(short_oid.id, id->id, (len + 1) / 2);
173-
if (len % 2)
174-
short_oid.id[len / 2] &= 0xF0;
175-
memset(short_oid.id + (len + 1) / 2, 0, (GIT_OID_HEXSZ - len) / 2);
172+
git_oid__cpy_prefix(&short_oid, id, len);
176173

177174
/* If len < GIT_OID_HEXSZ (a strict short oid was given), we have
178175
* 2 options :

0 commit comments

Comments
 (0)