[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 14/17] diff: use mempcpy
From: |
Paul Eggert |
Subject: |
[PATCH 14/17] diff: use mempcpy |
Date: |
Sun, 22 Aug 2021 14:03:03 -0700 |
* bootstrap.conf (gnulib_modules): Add mempcpy, stpcpy.
* src/ifdef.c (do_printf_spec):
* src/sdiff.c (expand_name, lf_snarf, temporary_file):
* src/util.c (message5):
Prefer mempcpy to memcpy plus manual size-updating.
Prefer stpcpy to mempcpy plus manual size-spec.
---
bootstrap.conf | 2 ++
src/ifdef.c | 5 ++---
src/sdiff.c | 21 +++++++++------------
src/util.c | 17 +++++++++--------
4 files changed, 22 insertions(+), 23 deletions(-)
diff --git a/bootstrap.conf b/bootstrap.conf
index f2359f6..b3c39e6 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -59,6 +59,7 @@ lstat
maintainer-makefile
manywarnings
mbrtowc
+mempcpy
mkstemp
mktime
nstrftime
@@ -73,6 +74,7 @@ stat
stat-macros
stat-time
stdint
+stpcpy
strcase
strptime
strtoimax
diff --git a/src/ifdef.c b/src/ifdef.c
index d82d2d1..b82288f 100644
--- a/src/ifdef.c
+++ b/src/ifdef.c
@@ -362,9 +362,8 @@ do_printf_spec (FILE *out, char const *spec,
size_t spec_prefix_len = f - spec - 2;
size_t pI_len = sizeof pI - 1;
char *format = xmalloca (spec_prefix_len + pI_len + 2);
- char *p = format + spec_prefix_len + pI_len;
- memcpy (format, spec, spec_prefix_len);
- memcpy (format + spec_prefix_len, pI, pI_len);
+ char *p = mempcpy (format, spec, spec_prefix_len);
+ p = stpcpy (p, pI);
*p++ = c;
*p = '\0';
fprintf (out, format, value);
diff --git a/src/sdiff.c b/src/sdiff.c
index ab12987..f6011ce 100644
--- a/src/sdiff.c
+++ b/src/sdiff.c
@@ -337,10 +337,10 @@ expand_name (char *name, bool is_dir, char const
*other_name)
size_t namelen = strlen (name), baselen = base_len (base);
bool insert_slash = *last_component (name) && name[namelen - 1] != '/';
char *r = xmalloc (namelen + insert_slash + baselen + 1);
- memcpy (r, name, namelen);
- r[namelen] = '/';
- memcpy (r + namelen + insert_slash, base, baselen);
- r[namelen + insert_slash + baselen] = '\0';
+ char *p = stpcpy (r, name);
+ *p = '/';
+ p = mempcpy (p + insert_slash, base, baselen);
+ *p = '\0';
return r;
}
}
@@ -429,17 +429,16 @@ lf_snarf (struct line_filter *lf, char *buffer, size_t
bufsize)
size_t s = next - start;
if (bufsize <= s)
return 0;
- memcpy (buffer, start, s);
+ buffer = mempcpy (buffer, start, s);
+ bufsize -= s;
if (next < lf->buflim)
{
- buffer[s] = 0;
+ *buffer = 0;
lf->bufpos = next + 1;
return 1;
}
if (! lf_refill (lf))
return s ? 0 : EOF;
- buffer += s;
- bufsize -= s;
}
}
@@ -1163,10 +1162,8 @@ temporary_file (void)
{
char const *tmpdir = getenv (TMPDIR_ENV);
char const *dir = tmpdir ? tmpdir : P_tmpdir;
- size_t dirlen = strlen (dir);
- char *buf = xmalloc (dirlen + 1 + 5 + 6 + 1);
- memcpy (buf, dir, dirlen);
- strcpy (buf + dirlen, "/sdiffXXXXXX");
+ char *buf = xmalloc (strlen (dir) + 1 + 5 + 6 + 1);
+ strcpy (stpcpy (buf, dir), "/sdiffXXXXXX");
int fd = mkstemp (buf);
if (fd < 0)
free (buf);
diff --git a/src/util.c b/src/util.c
index 9963bb4..a8b2fb1 100644
--- a/src/util.c
+++ b/src/util.c
@@ -107,8 +107,6 @@ message5 (char const *format_msgid, char const *arg1, char
const *arg2,
{
char *p;
char const *arg[5];
- int i;
- size_t size[5];
size_t total_size = offsetof (struct msg, args);
struct msg *new;
@@ -118,13 +116,17 @@ message5 (char const *format_msgid, char const *arg1,
char const *arg2,
arg[3] = arg3 ? arg3 : "";
arg[4] = arg4 ? arg4 : "";
- for (i = 0; i < 5; i++)
- total_size += size[i] = strlen (arg[i]) + 1;
+ for (int i = 0; i < 5; i++)
+ total_size += strlen (arg[i]) + 1;
new = xmalloc (total_size);
- for (i = 0, p = new->args; i < 5; p += size[i++])
- memcpy (p, arg[i], size[i]);
+ p = new->args;
+ for (int i = 0; i < 5; i++)
+ {
+ p = stpcpy (p, arg[i]);
+ *p++ = 0;
+ }
*msg_chain_end = new;
new->next = 0;
@@ -578,14 +580,13 @@ parse_diff_color (void)
const char *p; /* Pointer to character being parsed */
char *buf; /* color_buf buffer pointer */
int ind_no; /* Indicator number */
- char label[3]; /* Indicator label */
+ char label[] = "??"; /* Indicator label */
struct color_ext_type *ext; /* Extension we are working on */
if ((p = color_palette) == NULL || *p == '\0')
return;
ext = NULL;
- strcpy (label, "??");
/* This is an overly conservative estimate, but any possible
--palette string will *not* generate a color_buf longer than
--
2.31.1
- [PATCH 01/17] build: update gnulib submodule to latest, Paul Eggert, 2021/08/22
- [PATCH 06/17] maint: omit unused function if not debugging, Paul Eggert, 2021/08/22
- [PATCH 12/17] diff3: simplify process_diff, Paul Eggert, 2021/08/22
- [PATCH 09/17] diff: remove printint, Paul Eggert, 2021/08/22
- [PATCH 14/17] diff: use mempcpy,
Paul Eggert <=
- [PATCH 08/17] diff: remove INT_MAX limit on -F/-p searches, Paul Eggert, 2021/08/22
- [PATCH 03/17] diff3: suppress -fanalyzer alarms, Paul Eggert, 2021/08/22
- [PATCH 16/17] maint: refactor integer overflow checking, Paul Eggert, 2021/08/22
- [PATCH 04/17] maint: zalloc → xzalloc, Paul Eggert, 2021/08/22
- [PATCH 13/17] sdiff: fix unlikely memory leak, Paul Eggert, 2021/08/22
- [PATCH 02/17] maint: remove January workaround for Gnulib issue, Paul Eggert, 2021/08/22
- [PATCH 17/17] diff: add integer overflow checking, Paul Eggert, 2021/08/22
- [PATCH 10/17] maint: lint → GCC_LINT, Paul Eggert, 2021/08/22
- [PATCH 05/17] maint: remove prepargs, Paul Eggert, 2021/08/22
- [PATCH 07/17] maint: .gitignore updates, Paul Eggert, 2021/08/22