Skip to content

Commit d20bc01

Browse files
peffgitster
authored andcommitted
avoid computing zero offsets from NULL pointer
The Undefined Behavior Sanitizer in clang-11 seems to have learned a new trick: it complains about computing offsets from a NULL pointer, even if that offset is 0. This causes numerous test failures. For example, from t1090: unpack-trees.c:1355:41: runtime error: applying zero offset to null pointer ... not ok 6 - in partial clone, sparse checkout only fetches needed blobs The code in question looks like this: struct cache_entry **cache_end = cache + nr; ... while (cache != cache_end) and we sometimes pass in a NULL and 0 for "cache" and "nr". This is conceptually fine, as "cache_end" would be equal to "cache" in this case, and we wouldn't enter the loop at all. But computing even a zero offset violates the C standard. And given the fact that UBSan is noticing this behavior, this might be a potential problem spot if the compiler starts making unexpected assumptions based on undefined behavior. So let's just avoid it, which is pretty easy. In some cases we can just switch to iterating with a numeric index (as we do in sequencer.c here). In other cases (like the cache_end one) the use of an end pointer is more natural; we can keep that by just explicitly checking for the NULL/0 case when assigning the end pointer. Note that there are two ways you can write this latter case, checking for the pointer: cache_end = cache ? cache + nr : cache; or the size: cache_end = nr ? cache + nr : cache; For the case of a NULL/0 ptr/len combo, they are equivalent. But writing it the second way (as this patch does) has the property that if somebody were to incorrectly pass a NULL pointer with a non-zero length, we'd continue to notice and segfault, rather than silently pretending the length was zero. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4c616c2 commit d20bc01

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

sequencer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ static int do_recursive_merge(struct repository *r,
588588
struct merge_options o;
589589
struct tree *next_tree, *base_tree, *head_tree;
590590
int clean;
591-
char **xopt;
591+
int i;
592592
struct lock_file index_lock = LOCK_INIT;
593593

594594
if (repo_hold_locked_index(r, &index_lock, LOCK_REPORT_ON_ERROR) < 0)
@@ -608,8 +608,8 @@ static int do_recursive_merge(struct repository *r,
608608
next_tree = next ? get_commit_tree(next) : empty_tree(r);
609609
base_tree = base ? get_commit_tree(base) : empty_tree(r);
610610

611-
for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++)
612-
parse_merge_opt(&o, *xopt);
611+
for (i = 0; i < opts->xopts_nr; i++)
612+
parse_merge_opt(&o, opts->xopts[i]);
613613

614614
clean = merge_trees(&o,
615615
head_tree,

unpack-trees.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ static int clear_ce_flags_1(struct index_state *istate,
13481348
enum pattern_match_result default_match,
13491349
int progress_nr)
13501350
{
1351-
struct cache_entry **cache_end = cache + nr;
1351+
struct cache_entry **cache_end = nr ? cache + nr : cache;
13521352

13531353
/*
13541354
* Process all entries that have the given prefix and meet

xdiff-interface.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ static void trim_common_tail(mmfile_t *a, mmfile_t *b)
8484
{
8585
const int blk = 1024;
8686
long trimmed = 0, recovered = 0;
87-
char *ap = a->ptr + a->size;
88-
char *bp = b->ptr + b->size;
87+
char *ap = a->size ? a->ptr + a->size : a->ptr;
88+
char *bp = b->size ? b->ptr + b->size : b->ptr;
8989
long smaller = (a->size < b->size) ? a->size : b->size;
9090

9191
while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {

0 commit comments

Comments
 (0)