refactor: use xstpcpy() instead of strcat() (#25572)

This commit is contained in:
James 2023-10-10 21:03:55 +07:00 committed by GitHub
parent 9ff6f73f83
commit 468a3a1407
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 9 deletions

View File

@ -899,7 +899,6 @@ char *ExpandOne(expand_T *xp, char *str, char *orig, int options, int mode)
// Concatenate all matching names. Unless interrupted, this can be slow // Concatenate all matching names. Unless interrupted, this can be slow
// and the result probably won't be used. // and the result probably won't be used.
// TODO(philix): use xstpcpy instead of strcat in a loop (ExpandOne)
if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int) { if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int) {
size_t len = 0; size_t len = 0;
for (int i = 0; i < xp->xp_numfiles; i++) { for (int i = 0; i < xp->xp_numfiles; i++) {
@ -914,18 +913,19 @@ char *ExpandOne(expand_T *xp, char *str, char *orig, int options, int mode)
} }
ss = xmalloc(len); ss = xmalloc(len);
*ss = NUL; *ss = NUL;
char *ssp = ss;
for (int i = 0; i < xp->xp_numfiles; i++) { for (int i = 0; i < xp->xp_numfiles; i++) {
if (i > 0) { if (i > 0) {
if (xp->xp_prefix == XP_PREFIX_NO) { if (xp->xp_prefix == XP_PREFIX_NO) {
STRCAT(ss, "no"); ssp = xstpcpy(ssp, "no");
} else if (xp->xp_prefix == XP_PREFIX_INV) { } else if (xp->xp_prefix == XP_PREFIX_INV) {
STRCAT(ss, "inv"); ssp = xstpcpy(ssp, "inv");
} }
} }
STRCAT(ss, xp->xp_files[i]); ssp = xstpcpy(ssp, xp->xp_files[i]);
if (i != xp->xp_numfiles - 1) { if (i != xp->xp_numfiles - 1) {
STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); ssp = xstpcpy(ssp, (options & WILD_USE_NL) ? "\n" : " ");
} }
} }
} }

View File

@ -983,10 +983,12 @@ static int stuff_yank(int regname, char *p)
yankreg_T *reg = get_yank_register(regname, YREG_YANK); yankreg_T *reg = get_yank_register(regname, YREG_YANK);
if (is_append_register(regname) && reg->y_array != NULL) { if (is_append_register(regname) && reg->y_array != NULL) {
char **pp = &(reg->y_array[reg->y_size - 1]); char **pp = &(reg->y_array[reg->y_size - 1]);
char *lp = xmalloc(strlen(*pp) + strlen(p) + 1); const size_t ppl = strlen(*pp);
STRCPY(lp, *pp); const size_t pl = strlen(p);
// TODO(philix): use xstpcpy() in stuff_yank() char *lp = xmalloc(ppl + pl + 1);
STRCAT(lp, p); memcpy(lp, *pp, ppl);
memcpy(lp + ppl, p, pl);
*(lp + ppl + pl) = NUL;
xfree(p); xfree(p);
xfree(*pp); xfree(*pp);
*pp = lp; *pp = lp;