# HG changeset patch # User Jun Wu # Date 2018-06-06 19:53:26 # Node ID 3c84493556db3bffcff2fa2f24bb6738dde9fc58 # Parent 0a3ff7af24a9ee9a7ea3791258ae1f59e817d8dd 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 diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c --- a/contrib/chg/chg.c +++ b/contrib/chg/chg.c @@ -220,7 +220,10 @@ static void execcmdserver(const struct c 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)