From 1fb8a951b6ce2472753c502ff703e29be1307604 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 2 May 2016 16:24:14 +0200 Subject: [PATCH] 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. --- src/odb_loose.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/odb_loose.c b/src/odb_loose.c index 9d9bffd21..3c33160d0 100644 --- a/src/odb_loose.c +++ b/src/odb_loose.c @@ -91,7 +91,7 @@ static int object_mkdir(const git_buf *name, const loose_backend *be) static size_t get_binary_object_header(obj_hdr *hdr, git_buf *obj) { - unsigned char c; + unsigned long c; unsigned char *data = (unsigned char *)obj->ptr; size_t shift, size, used = 0;