Skip to content

Commit 7f40771

Browse files
committed
odb_loose: fix undefined behavior when computing size
An object's size is computed by reading the object header's size field until the most significant bit is not set anymore. To get the total size, we increase the shift on each iteration and add the shifted value to the total size. We read the current value into a variable of type `unsigned char`, from which we then take all bits except the most significant bit and shift the result. We will end up with a maximum shift of 60, but this exceeds the width of the value's type, resulting in undefined behavior. Fix the issue by instead reading the values into a variable of type `unsigned long`, which matches the required width. This is equivalent to git.git, which uses an `unsigned long` as well.
1 parent 7b24c4f commit 7f40771

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/odb_loose.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static int object_mkdir(const git_buf *name, const loose_backend *be)
9191

9292
static size_t get_binary_object_header(obj_hdr *hdr, git_buf *obj)
9393
{
94-
unsigned char c;
94+
unsigned long c;
9595
unsigned char *data = (unsigned char *)obj->ptr;
9696
size_t shift, size, used = 0;
9797

0 commit comments

Comments
 (0)