summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2020-07-09 23:12:58 +0100
committerGitHub <noreply@github.com>2020-07-09 23:12:58 +0100
commit325375e3b6c2c31152b7ee48794aa6a1f73709d5 (patch)
tree42a7d325d0fc1d3eb3af24188eda953ddd126cd2
parent2ffa426e1dffc78c64112ea8a4a9796be66462a9 (diff)
parentd0656ac80f33d54967425e782cbe71a0f1b963db (diff)
downloadlibgit2-325375e3b6c2c31152b7ee48794aa6a1f73709d5.tar.gz
Merge pull request #5568 from lhchavez/ubsan
Make the tests run cleanly under UndefinedBehaviorSanitizer
-rw-r--r--src/apply.c3
-rw-r--r--src/buffer.c7
-rw-r--r--src/index.c14
3 files changed, 15 insertions, 9 deletions
diff --git a/src/apply.c b/src/apply.c
index f90166343..b0be2d8e6 100644
--- a/src/apply.c
+++ b/src/apply.c
@@ -66,6 +66,9 @@ static int patch_image_init_fromstr(
if (git_pool_init(&out->pool, sizeof(git_diff_line)) < 0)
return -1;
+ if (!in_len)
+ return 0;
+
for (start = in; start < in + in_len; start = end) {
end = memchr(start, '\n', in_len - (start - in));
diff --git a/src/buffer.c b/src/buffer.c
index 3ee2775b1..c203650c7 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -365,7 +365,7 @@ int git_buf_encode_base85(git_buf *buf, const char *data, size_t len)
for (i = 24; i >= 0; i -= 8) {
uint8_t ch = *data++;
- acc |= ch << i;
+ acc |= (uint32_t)ch << i;
if (--len == 0)
break;
@@ -759,7 +759,8 @@ int git_buf_join(
ssize_t offset_a = -1;
/* not safe to have str_b point internally to the buffer */
- assert(str_b < buf->ptr || str_b >= buf->ptr + buf->size);
+ if (buf->size)
+ assert(str_b < buf->ptr || str_b >= buf->ptr + buf->size);
/* figure out if we need to insert a separator */
if (separator && strlen_a) {
@@ -769,7 +770,7 @@ int git_buf_join(
}
/* str_a could be part of the buffer */
- if (str_a >= buf->ptr && str_a < buf->ptr + buf->size)
+ if (buf->size && str_a >= buf->ptr && str_a < buf->ptr + buf->size)
offset_a = str_a - buf->ptr;
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, strlen_a, strlen_b);
diff --git a/src/index.c b/src/index.c
index 361288b8d..d5afc9bb6 100644
--- a/src/index.c
+++ b/src/index.c
@@ -2781,17 +2781,19 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const cha
ondisk.flags = htons(entry->flags);
if (entry->flags & GIT_INDEX_ENTRY_EXTENDED) {
+ const size_t path_offset = offsetof(struct entry_long, path);
struct entry_long ondisk_ext;
memcpy(&ondisk_ext, &ondisk, sizeof(struct entry_short));
ondisk_ext.flags_extended = htons(entry->flags_extended &
GIT_INDEX_ENTRY_EXTENDED_FLAGS);
- memcpy(mem, &ondisk_ext, offsetof(struct entry_long, path));
- path = ((struct entry_long*)mem)->path;
- disk_size -= offsetof(struct entry_long, path);
+ memcpy(mem, &ondisk_ext, path_offset);
+ path = (char *)mem + path_offset;
+ disk_size -= path_offset;
} else {
- memcpy(mem, &ondisk, offsetof(struct entry_short, path));
- path = ((struct entry_short*)mem)->path;
- disk_size -= offsetof(struct entry_short, path);
+ const size_t path_offset = offsetof(struct entry_short, path);
+ memcpy(mem, &ondisk, path_offset);
+ path = (char *)mem + path_offset;
+ disk_size -= path_offset;
}
if (last) {