Merge branch 'jk/clang-sanitizer-fixes'

C pedantry ;-) fix.

* jk/clang-sanitizer-fixes:
  obstack: avoid computing offsets from NULL pointer
  xdiff: avoid computing non-zero offset from NULL pointer
  avoid computing zero offsets from NULL pointer
  merge-recursive: use subtraction to flip stage
  merge-recursive: silence -Wxor-used-as-pow warning
This commit is contained in:
Junio C Hamano 2020-02-12 12:41:36 -08:00
commit b783391018
5 changed files with 29 additions and 15 deletions

View File

@ -135,8 +135,10 @@ extern "C" {
alignment relative to 0. */
#define __PTR_ALIGN(B, P, A) \
__BPTR_ALIGN (sizeof (PTR_INT_TYPE) < sizeof (void *) ? (B) : (char *) 0, \
P, A)
(sizeof (PTR_INT_TYPE) < sizeof(void *) ? \
__BPTR_ALIGN((B), (P), (A)) : \
(void *)__BPTR_ALIGN((PTR_INT_TYPE)(void *)0, (PTR_INT_TYPE)(P), (A)) \
)
#include <string.h>

View File

@ -1712,6 +1712,14 @@ static char *find_path_for_conflict(struct merge_options *opt,
return new_path;
}
/*
* Toggle the stage number between "ours" and "theirs" (2 and 3).
*/
static inline int flip_stage(int stage)
{
return (2 + 3) - stage;
}
static int handle_rename_rename_1to2(struct merge_options *opt,
struct rename_conflict_info *ci)
{
@ -1756,14 +1764,14 @@ static int handle_rename_rename_1to2(struct merge_options *opt,
* such cases, we should keep the added file around,
* resolving the conflict at that path in its favor.
*/
add = &ci->ren1->dst_entry->stages[2 ^ 1];
add = &ci->ren1->dst_entry->stages[flip_stage(2)];
if (is_valid(add)) {
if (update_file(opt, 0, add, a->path))
return -1;
}
else
remove_file_from_index(opt->repo->index, a->path);
add = &ci->ren2->dst_entry->stages[3 ^ 1];
add = &ci->ren2->dst_entry->stages[flip_stage(3)];
if (is_valid(add)) {
if (update_file(opt, 0, add, b->path))
return -1;
@ -1776,7 +1784,7 @@ static int handle_rename_rename_1to2(struct merge_options *opt,
* rename/add collision. If not, we can write the file out
* to the specified location.
*/
add = &ci->ren1->dst_entry->stages[2 ^ 1];
add = &ci->ren1->dst_entry->stages[flip_stage(2)];
if (is_valid(add)) {
add->path = mfi.blob.path = a->path;
if (handle_file_collision(opt, a->path,
@ -1797,7 +1805,7 @@ static int handle_rename_rename_1to2(struct merge_options *opt,
return -1;
}
add = &ci->ren2->dst_entry->stages[3 ^ 1];
add = &ci->ren2->dst_entry->stages[flip_stage(3)];
if (is_valid(add)) {
add->path = mfi.blob.path = b->path;
if (handle_file_collision(opt, b->path,
@ -1846,7 +1854,7 @@ static int handle_rename_rename_2to1(struct merge_options *opt,
path_side_1_desc = xstrfmt("version of %s from %s", path, a->path);
path_side_2_desc = xstrfmt("version of %s from %s", path, b->path);
ostage1 = ci->ren1->branch == opt->branch1 ? 3 : 2;
ostage2 = ostage1 ^ 1;
ostage2 = flip_stage(ostage1);
ci->ren1->src_entry->stages[ostage1].path = a->path;
ci->ren2->src_entry->stages[ostage2].path = b->path;
if (merge_mode_and_contents(opt, a, c1,

View File

@ -588,7 +588,7 @@ static int do_recursive_merge(struct repository *r,
struct merge_options o;
struct tree *next_tree, *base_tree, *head_tree;
int clean;
char **xopt;
int i;
struct lock_file index_lock = LOCK_INIT;
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,
next_tree = next ? get_commit_tree(next) : empty_tree(r);
base_tree = base ? get_commit_tree(base) : empty_tree(r);
for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++)
parse_merge_opt(&o, *xopt);
for (i = 0; i < opts->xopts_nr; i++)
parse_merge_opt(&o, opts->xopts[i]);
clean = merge_trees(&o,
head_tree,

View File

@ -1352,7 +1352,7 @@ static int clear_ce_flags_1(struct index_state *istate,
enum pattern_match_result default_match,
int progress_nr)
{
struct cache_entry **cache_end = cache + nr;
struct cache_entry **cache_end = nr ? cache + nr : cache;
/*
* Process all entries that have the given prefix and meet

View File

@ -84,8 +84,8 @@ static void trim_common_tail(mmfile_t *a, mmfile_t *b)
{
const int blk = 1024;
long trimmed = 0, recovered = 0;
char *ap = a->ptr + a->size;
char *bp = b->ptr + b->size;
char *ap = a->size ? a->ptr + a->size : a->ptr;
char *bp = b->size ? b->ptr + b->size : b->ptr;
long smaller = (a->size < b->size) ? a->size : b->size;
while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
@ -250,9 +250,13 @@ void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
ALLOC_ARRAY(regs->array, regs->nr);
for (i = 0; i < regs->nr; i++) {
struct ff_reg *reg = regs->array + i;
const char *ep = strchr(value, '\n'), *expression;
const char *ep, *expression;
char *buffer = NULL;
if (!value)
BUG("mismatch between line count and parsing");
ep = strchr(value, '\n');
reg->negate = (*value == '!');
if (reg->negate && i == regs->nr - 1)
die("Last expression must not be negated: %s", value);
@ -265,7 +269,7 @@ void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
if (regcomp(&reg->re, expression, cflags))
die("Invalid regexp to look for hunk header: %s", expression);
free(buffer);
value = ep + 1;
value = ep ? ep + 1 : NULL;
}
}