changeset 38198:3c84493556db stable

chg: fix an undefined behavior about memcpy Spot by Wez Furlong. `memcpy(x, NULL, 0)` is undefined according to [1]. [1]: https://stackoverflow.com/questions/5243012 Differential Revision: https://phab.mercurial-scm.org/D3698
author Jun Wu <quark@fb.com>
date Wed, 06 Jun 2018 12:53:26 -0700
parents 0a3ff7af24a9
children 6cc5d01a58a6
files contrib/chg/chg.c
diffstat 1 files changed, 4 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/chg/chg.c	Wed Jun 06 13:28:49 2018 -0400
+++ b/contrib/chg/chg.c	Wed Jun 06 12:53:26 2018 -0700
@@ -220,7 +220,10 @@
 
 	const char **argv = mallocx(sizeof(char *) * argsize);
 	memcpy(argv, baseargv, sizeof(baseargv));
-	memcpy(argv + baseargvsize, opts->args, sizeof(char *) * opts->argsize);
+	if (opts->args) {
+		size_t size = sizeof(char *) * opts->argsize;
+		memcpy(argv + baseargvsize, opts->args, size);
+	}
 	argv[argsize - 1] = NULL;
 
 	if (putenv("CHGINTERNALMARK=") != 0)