chg: fix an undefined behavior about memcpy stable
authorJun Wu <quark@fb.com>
Wed, 06 Jun 2018 12:53:26 -0700
branchstable
changeset 38198 3c84493556db
parent 38197 0a3ff7af24a9
child 38292 6cc5d01a58a6
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
contrib/chg/chg.c
--- 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)