From 2fe96e4a410c346483a20376e58c40e63887d652 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sat, 9 Jul 2022 13:26:30 -0400 Subject: [PATCH] Take advantage of mempcpy() and stpcpy() * src/makeint.h (stpcpy): Add missing declaration. * src/amiga.c (MyExecute): Avoid extra strlen using stpcpy. * src/function.c (func_shell_base): Ditto. (func_error): Use memcpy() not strcpy() when we know the length. * src/job.c (construct_command_argv_internal): Use stpcpy(). * src/main.c (main): Ditto. (define_makeflags): Ditto. * src/variable.c (print_target_variables): Use memcpy() when we know the length. * src/commands.c (set_file_variables): Use mempcpy(). * src/expand.c (variable_buffer_output): Ditto. * src/file.c (expand_deps): Ditto. * src/function.c (abspath): Ditto. (handle_function): Ditto. * src/implicit.c (pattern_search): Ditto. * src/job.c (construct_command_argv_internal): Use mempcpy() and don't add multiple spaces when there are no shell flags. * src/main.c (decode_env_switches): Use mempcpy() to simplify. (define_makeflags): Ditto. * src/variable.c (selective_vpath_search): Ditto. --- src/amiga.c | 10 +++------- src/commands.c | 12 ++++-------- src/expand.c | 3 +-- src/file.c | 6 ++---- src/function.c | 14 ++++++-------- src/implicit.c | 28 +++++++++------------------- src/job.c | 10 +++++----- src/main.c | 27 +++++++++++---------------- src/makeint.h | 5 +++++ src/variable.c | 5 ++--- src/vpath.c | 6 ++---- 11 files changed, 50 insertions(+), 76 deletions(-) diff --git a/src/amiga.c b/src/amiga.c index ec0ca143..10a19cde 100644 --- a/src/amiga.c +++ b/src/amiga.c @@ -51,9 +51,8 @@ MyExecute (char **argv) if (((*aptr)[0] == ';' && !(*aptr)[1])) { *ptr ++ = '"'; - strcpy (ptr, *aptr); - ptr += strlen (ptr); - *ptr ++ = '"'; + ptr = stpcpy (ptr, *aptr); + *(ptr++) = '"'; } else if ((*aptr)[0] == '@' && (*aptr)[1] == '@' && !(*aptr)[2]) { @@ -61,10 +60,7 @@ MyExecute (char **argv) continue; } else - { - strcpy (ptr, *aptr); - ptr += strlen (ptr); - } + ptr = stpcpy (ptr, *aptr); *ptr ++ = ' '; *ptr = 0; } diff --git a/src/commands.c b/src/commands.c index 154d5dea..3dd1953f 100644 --- a/src/commands.c +++ b/src/commands.c @@ -213,8 +213,7 @@ set_file_variables (struct file *file, const char *stem) #endif len = strlen (c); - memcpy (cp, c, len); - cp += len; + cp = mempcpy (cp, c, len); *cp++ = FILE_LIST_SEPARATOR; if (! (d->changed || always_make_flag)) qmark_len -= len + 1; /* Don't space in $? for this one. */ @@ -284,19 +283,16 @@ set_file_variables (struct file *file, const char *stem) if (d->ignore_mtime) { - memcpy (bp, c, len); - bp += len; + bp = mempcpy (bp, c, len); *bp++ = FILE_LIST_SEPARATOR; } else { - memcpy (cp, c, len); - cp += len; + cp = mempcpy (cp, c, len); *cp++ = FILE_LIST_SEPARATOR; if (d->changed || always_make_flag) { - memcpy (qp, c, len); - qp += len; + qp = mempcpy (qp, c, len); *qp++ = FILE_LIST_SEPARATOR; } } diff --git a/src/expand.c b/src/expand.c index 6397bb22..6bc2ac59 100644 --- a/src/expand.c +++ b/src/expand.c @@ -67,8 +67,7 @@ variable_buffer_output (char *ptr, const char *string, size_t length) ptr = variable_buffer + offset; } - memcpy (ptr, string, length); - return ptr + length; + return mempcpy (ptr, string, length); } /* Return a pointer to the beginning of the variable buffer. diff --git a/src/file.c b/src/file.c index ab46c4a1..3960cdab 100644 --- a/src/file.c +++ b/src/file.c @@ -620,8 +620,7 @@ expand_deps (struct file *f) while (cs) { - memcpy (s, pcs, cs - pcs); - s += cs - pcs; + s = mempcpy (s, pcs, cs - pcs); *(s++) = '$'; *(s++) = '*'; pcs = ++cs; @@ -1273,8 +1272,7 @@ build_target_list (char *value) p = &value[off]; } - memcpy (p, f->name, l); - p += l; + p = mempcpy (p, f->name, l); *(p++) = ' '; } *(p-1) = '\0'; diff --git a/src/function.c b/src/function.c index 9247fc3f..1f490fa2 100644 --- a/src/function.c +++ b/src/function.c @@ -1185,7 +1185,7 @@ func_error (char *o, char **argv, const char *funcname) { size_t len = strlen (argv[0]); char *msg = alloca (len + 2); - strcpy (msg, argv[0]); + memcpy (msg, argv[0], len); msg[len] = '\n'; msg[len + 1] = '\0'; outputs (0, msg); @@ -2060,8 +2060,8 @@ func_shell_base (char *o, char **argv, int trim_newlines) { strcpy (ptr, *aptr); ptr += strlen (ptr) + 1; - *ptr ++ = ' '; - *ptr = 0; + *(ptr++) = ' '; + *ptr = '\0'; } ptr[-1] = '\n'; @@ -2249,8 +2249,7 @@ abspath (const char *name, char *apath) if (dest + len >= apath_limit) return NULL; - dest = memcpy (dest, start, len); - dest += len; + dest = mempcpy (dest, start, len); *dest = '\0'; } } @@ -2646,9 +2645,8 @@ handle_function (char **op, const char **stringp) char *p, *aend; abeg = xmalloc (len+1); - memcpy (abeg, beg, len); - abeg[len] = '\0'; - aend = abeg + len; + aend = mempcpy (abeg, beg, len); + *aend = '\0'; for (p=abeg, nargs=0; p <= aend; ++argvp) { diff --git a/src/implicit.c b/src/implicit.c index a89994b9..d3853726 100644 --- a/src/implicit.c +++ b/src/implicit.c @@ -560,14 +560,9 @@ pattern_search (struct file *file, int archive, { char *o = depname; if (check_lastslash) - { - memcpy (o, filename, pathlen); - o += pathlen; - } - memcpy (o, nptr, cp - nptr); - o += cp - nptr; - memcpy (o, stem, stemlen); - o += stemlen; + o = mempcpy (o, filename, pathlen); + o = mempcpy (o, nptr, cp - nptr); + o = mempcpy (o, stem, stemlen); strcpy (o, cp + 1); is_explicit = 0; } @@ -651,20 +646,17 @@ pattern_search (struct file *file, int archive, { size_t i = cp - nptr; assert (o + i < dend); - memcpy (o, nptr, i); - o += i; + o = mempcpy (o, nptr, i); if (check_lastslash) { add_dir = 1; assert (o + 5 < dend); - memcpy (o, "$(*F)", 5); - o += 5; + o = mempcpy (o, "$(*F)", 5); } else { assert (o + 2 < dend); - memcpy (o, "$*", 2); - o += 2; + o = mempcpy (o, "$*", 2); } assert (o < dend); ++cp; @@ -1108,11 +1100,9 @@ pattern_search (struct file *file, int archive, struct dep *new = alloc_dep (); /* GKM FIMXE: handle '|' here too */ - memcpy (p, rule->targets[ri], - rule->suffixes[ri] - rule->targets[ri] - 1); - p += rule->suffixes[ri] - rule->targets[ri] - 1; - memcpy (p, file->stem, fullstemlen); - p += fullstemlen; + p = mempcpy (p, rule->targets[ri], + rule->suffixes[ri] - rule->targets[ri] - 1); + p = mempcpy (p, file->stem, fullstemlen); memcpy (p, rule->suffixes[ri], rule->lens[ri] - (rule->suffixes[ri] - rule->targets[ri])+1); new->name = strcache_add (nm); diff --git a/src/job.c b/src/job.c index 402d409f..8837b744 100644 --- a/src/job.c +++ b/src/job.c @@ -3413,9 +3413,10 @@ construct_command_argv_internal (char *line, char **restp, const char *shell, } *(ap++) = ' '; if (shellflags) - memcpy (ap, shellflags, sflags_len); - ap += sflags_len; - *(ap++) = ' '; + { + ap = mempcpy (ap, shellflags, sflags_len); + *(ap++) = ' '; + } #ifdef WINDOWS32 command_ptr = ap; #endif @@ -3460,8 +3461,7 @@ construct_command_argv_internal (char *line, char **restp, const char *shell, else if (unixy_shell && strneq (p, "...", 3)) { /* The case of '...' wildcard again. */ - strcpy (ap, "\\.\\.\\"); - ap += 5; + ap = stpcpy (ap, "\\.\\.\\"); p += 2; } #endif diff --git a/src/main.c b/src/main.c index a999ee89..ef2d2c41 100644 --- a/src/main.c +++ b/src/main.c @@ -1977,8 +1977,7 @@ main (int argc, char **argv, char **envp) p = endp = value = alloca (len); for (i = 0; i < eval_strings->idx; ++i) { - strcpy (p, "--eval="); - p += CSTRLEN ("--eval="); + p = stpcpy (p, "--eval="); p = quote_for_env (p, eval_strings->list[i]); endp = p++; *endp = ' '; @@ -3251,11 +3250,12 @@ decode_env_switches (const char *envar, size_t len) const char **argv; /* Get the variable's value. */ - varref[0] = '$'; - varref[1] = '('; - memcpy (&varref[2], envar, len); - varref[2 + len] = ')'; - varref[2 + len + 1] = '\0'; + p = varref; + *(p++) = '$'; + *(p++) = '('; + p = mempcpy (p, envar, len); + *(p++) = ')'; + *p = '\0'; value = variable_expand (varref); /* Skip whitespace, and check for an empty value. */ @@ -3489,8 +3489,7 @@ define_makeflags (int all, int makefile) { /* Long options require a double-dash. */ *p++ = '-'; - strcpy (p, flags->cs->long_name); - p += strlen (p); + p = stpcpy (p, flags->cs->long_name); } /* An omitted optional argument has an ARG of "". */ if (flags->arg && flags->arg[0] != '\0') @@ -3522,8 +3521,7 @@ define_makeflags (int all, int makefile) if (eval_strings) { *p++ = ' '; - memcpy (p, evalref, CSTRLEN (evalref)); - p += CSTRLEN (evalref); + p = mempcpy (p, evalref, CSTRLEN (evalref)); } if (all) @@ -3538,13 +3536,10 @@ define_makeflags (int all, int makefile) if (v && v->value && v->value[0] != '\0') { - strcpy (p, " -- "); - p += 4; - + p = stpcpy (p, " -- "); *(p++) = '$'; *(p++) = '('; - memcpy (p, r, l); - p += l; + p = mempcpy (p, r, l); *(p++) = ')'; } } diff --git a/src/makeint.h b/src/makeint.h index d117240b..e355051d 100644 --- a/src/makeint.h +++ b/src/makeint.h @@ -670,6 +670,11 @@ int strncasecmp (const char *s1, const char *s2, int n); void *mempcpy (void *dest, const void *src, size_t n); #endif +#if !HAVE_STPCPY +/* Create our own, in misc.c */ +char *stpcpy (char *dest, const char *src); +#endif + #define OUTPUT_SYNC_NONE 0 #define OUTPUT_SYNC_LINE 1 #define OUTPUT_SYNC_TARGET 2 diff --git a/src/variable.c b/src/variable.c index c53ce5a7..c51f41dc 100644 --- a/src/variable.c +++ b/src/variable.c @@ -435,8 +435,7 @@ lookup_special_var (struct variable *var) p = &var->value[off]; } - memcpy (p, v->name, l); - p += l; + p = mempcpy (p, v->name, l); *(p++) = ' '; } *(p-1) = '\0'; @@ -1855,7 +1854,7 @@ print_target_variables (const struct file *file) size_t l = strlen (file->name); char *t = alloca (l + 3); - strcpy (t, file->name); + memcpy (t, file->name, l); t[l] = ':'; t[l+1] = ' '; t[l+2] = '\0'; diff --git a/src/vpath.c b/src/vpath.c index 336aee2f..203fca1b 100644 --- a/src/vpath.c +++ b/src/vpath.c @@ -379,8 +379,7 @@ selective_vpath_search (struct vpath *path, const char *file, size_t vlen = strlen (vpath[i]); /* Put the next VPATH entry into NAME at P and increment P past it. */ - memcpy (p, vpath[i], vlen); - p += vlen; + p = mempcpy (p, vpath[i], vlen); /* Add the directory prefix already in *FILE. */ if (name_dplen > 0) @@ -392,8 +391,7 @@ selective_vpath_search (struct vpath *path, const char *file, if ((*p != ':') && (*p != ']') && (*p != '>')) *p++ = '/'; #endif - memcpy (p, file, name_dplen); - p += name_dplen; + p = mempcpy (p, file, name_dplen); } #ifdef HAVE_DOS_PATHS -- 2.47.3