comparison contrib/chg/chg.c @ 37871: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 9724f54923ec
children 04a3ae7aba14
comparison
equal deleted inserted replaced
37870:0a3ff7af24a9 37871:3c84493556db
218 size_t baseargvsize = sizeof(baseargv) / sizeof(baseargv[0]); 218 size_t baseargvsize = sizeof(baseargv) / sizeof(baseargv[0]);
219 size_t argsize = baseargvsize + opts->argsize + 1; 219 size_t argsize = baseargvsize + opts->argsize + 1;
220 220
221 const char **argv = mallocx(sizeof(char *) * argsize); 221 const char **argv = mallocx(sizeof(char *) * argsize);
222 memcpy(argv, baseargv, sizeof(baseargv)); 222 memcpy(argv, baseargv, sizeof(baseargv));
223 memcpy(argv + baseargvsize, opts->args, sizeof(char *) * opts->argsize); 223 if (opts->args) {
224 size_t size = sizeof(char *) * opts->argsize;
225 memcpy(argv + baseargvsize, opts->args, size);
226 }
224 argv[argsize - 1] = NULL; 227 argv[argsize - 1] = NULL;
225 228
226 if (putenv("CHGINTERNALMARK=") != 0) 229 if (putenv("CHGINTERNALMARK=") != 0)
227 abortmsgerrno("failed to putenv"); 230 abortmsgerrno("failed to putenv");
228 if (execvp(hgcmd, (char **)argv) < 0) 231 if (execvp(hgcmd, (char **)argv) < 0)