Skip to content

Commit 325375e

Browse files
authored
Merge pull request libgit2#5568 from lhchavez/ubsan
Make the tests run cleanly under UndefinedBehaviorSanitizer
2 parents 2ffa426 + d0656ac commit 325375e

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

src/apply.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ static int patch_image_init_fromstr(
6666
if (git_pool_init(&out->pool, sizeof(git_diff_line)) < 0)
6767
return -1;
6868

69+
if (!in_len)
70+
return 0;
71+
6972
for (start = in; start < in + in_len; start = end) {
7073
end = memchr(start, '\n', in_len - (start - in));
7174

src/buffer.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ int git_buf_encode_base85(git_buf *buf, const char *data, size_t len)
365365

366366
for (i = 24; i >= 0; i -= 8) {
367367
uint8_t ch = *data++;
368-
acc |= ch << i;
368+
acc |= (uint32_t)ch << i;
369369

370370
if (--len == 0)
371371
break;
@@ -759,7 +759,8 @@ int git_buf_join(
759759
ssize_t offset_a = -1;
760760

761761
/* not safe to have str_b point internally to the buffer */
762-
assert(str_b < buf->ptr || str_b >= buf->ptr + buf->size);
762+
if (buf->size)
763+
assert(str_b < buf->ptr || str_b >= buf->ptr + buf->size);
763764

764765
/* figure out if we need to insert a separator */
765766
if (separator && strlen_a) {
@@ -769,7 +770,7 @@ int git_buf_join(
769770
}
770771

771772
/* str_a could be part of the buffer */
772-
if (str_a >= buf->ptr && str_a < buf->ptr + buf->size)
773+
if (buf->size && str_a >= buf->ptr && str_a < buf->ptr + buf->size)
773774
offset_a = str_a - buf->ptr;
774775

775776
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, strlen_a, strlen_b);

src/index.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2781,17 +2781,19 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const cha
27812781
ondisk.flags = htons(entry->flags);
27822782

27832783
if (entry->flags & GIT_INDEX_ENTRY_EXTENDED) {
2784+
const size_t path_offset = offsetof(struct entry_long, path);
27842785
struct entry_long ondisk_ext;
27852786
memcpy(&ondisk_ext, &ondisk, sizeof(struct entry_short));
27862787
ondisk_ext.flags_extended = htons(entry->flags_extended &
27872788
GIT_INDEX_ENTRY_EXTENDED_FLAGS);
2788-
memcpy(mem, &ondisk_ext, offsetof(struct entry_long, path));
2789-
path = ((struct entry_long*)mem)->path;
2790-
disk_size -= offsetof(struct entry_long, path);
2789+
memcpy(mem, &ondisk_ext, path_offset);
2790+
path = (char *)mem + path_offset;
2791+
disk_size -= path_offset;
27912792
} else {
2792-
memcpy(mem, &ondisk, offsetof(struct entry_short, path));
2793-
path = ((struct entry_short*)mem)->path;
2794-
disk_size -= offsetof(struct entry_short, path);
2793+
const size_t path_offset = offsetof(struct entry_short, path);
2794+
memcpy(mem, &ondisk, path_offset);
2795+
path = (char *)mem + path_offset;
2796+
disk_size -= path_offset;
27952797
}
27962798

27972799
if (last) {

0 commit comments

Comments
 (0)