On 04/06/2017 10:49 PM, Johannes Sixt wrote:
> Am 06.04.2017 um 19:31 schrieb René Scharfe:
>> Am 06.04.2017 um 18:33 schrieb Johannes Sixt:
>>> Am 06.04.2017 um 17:42 schrieb Martin Liška:
>>>> +static inline void *sane_memmove(void *dest, const void *src, size_t n)
>>>> +{
>>>> +    if (n > 0)
>>>> +        return memmove(dest, src, n);
>>>> +    else
>>>> +        return dest;
>>>> +}
>>>
>>> Huh? memmove with n == 0 is well-defined. This wrapper is pointless.
>>
>> memmove(3) with NULL pointers is undefined.
> 
> Then don't hide this helper behind a macro with a suspiciously similar name. 
> Using the name sane_mmemove at the call site is preferable. memmove_or_null 
> or something similar perhaps even more so.
> 
> -- Hannes
> 

Good. There's tested v4.

Martin
>From 0bdf4d717d3d368dd9676d15d20f8592c4d22fde Mon Sep 17 00:00:00 2001
From: marxin <mli...@suse.cz>
Date: Wed, 5 Apr 2017 14:31:32 +0200
Subject: [PATCH 1/2] Fix nonnull errors reported by UBSAN with GCC 7.

Replace call to memmove with newly introduced function memmove_or_null
and call to memcpy with COPY_ARRAY macro.

Signed-off-by: Martin Liska <mli...@suse.cz>
---
 apply.c            | 4 +---
 builtin/ls-files.c | 2 +-
 git-compat-util.h  | 8 ++++++++
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/apply.c b/apply.c
index e6dbab26a..121f3f414 100644
--- a/apply.c
+++ b/apply.c
@@ -2802,9 +2802,7 @@ static void update_image(struct apply_state *state,
 			img->line + applied_pos + preimage_limit,
 			(img->nr - (applied_pos + preimage_limit)) *
 			sizeof(*img->line));
-	memcpy(img->line + applied_pos,
-	       postimage->line,
-	       postimage->nr * sizeof(*img->line));
+	COPY_ARRAY(img->line + applied_pos, postimage->line, postimage->nr);
 	if (!state->allow_overlap)
 		for (i = 0; i < postimage->nr; i++)
 			img->line[applied_pos + i].flag |= LINE_PATCHED;
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index d449e46db..0a6cc1e8a 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -391,7 +391,7 @@ static void prune_cache(const char *prefix, size_t prefixlen)
 		}
 		last = next;
 	}
-	memmove(active_cache, active_cache + pos,
+	memmove_or_null(active_cache, active_cache + pos,
 		(last - pos) * sizeof(struct cache_entry *));
 	active_nr = last - pos;
 }
diff --git a/git-compat-util.h b/git-compat-util.h
index 8a4a3f85e..81f6e56ac 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -1002,6 +1002,14 @@ int git_qsort_s(void *base, size_t nmemb, size_t size,
 		die("BUG: qsort_s() failed");			\
 } while (0)
 
+static inline void *memmove_or_null(void *dest, const void *src, size_t n)
+{
+	if (n > 0)
+		return memmove(dest, src, n);
+	else
+		return dest;
+}
+
 #ifndef REG_STARTEND
 #error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
 #endif
-- 
2.12.2

Reply via email to