aboutsummaryrefslogtreecommitdiffstats
path: root/exec
diff options
context:
space:
mode:
authorPaul Eggert2023-08-12 12:50:15 -0700
committerPaul Eggert2023-08-12 12:57:35 -0700
commit5315e6e8d7e7233d54cce2b4c1bc8cf3b7acf4dc (patch)
tree5dc35aa709e829e1a67dcb868e643eee85c3449a /exec
parentf3868cb9d1806b35186eabc0262393316ebe689a (diff)
downloademacs-5315e6e8d7e7233d54cce2b4c1bc8cf3b7acf4dc.tar.gz
emacs-5315e6e8d7e7233d54cce2b4c1bc8cf3b7acf4dc.zip
Avoid stpncpy
It’s not worth the porting hassle, and as the glibc manual says, “this function is generally a poor choice for processing strings”. * admin/merge-gnulib (GNULIB_MODULES): Remove stpncpy. * exec/configure.ac: Do not check for stpncpy. * exec/exec.c (rpl_stpncpy, stpncpy): Remove this replacement. (exec_0): Properly clear buffer1. Use memcpy instead of stpncpy to add the trailing name. This code is clearly still suboptimal but efficiency is not that important here and I tried to minimize the change. * lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
Diffstat (limited to 'exec')
-rw-r--r--exec/configure.ac4
-rw-r--r--exec/exec.c77
2 files changed, 7 insertions, 74 deletions
diff --git a/exec/configure.ac b/exec/configure.ac
index e78d8ebea90..180c200d13d 100644
--- a/exec/configure.ac
+++ b/exec/configure.ac
@@ -62,8 +62,8 @@ AC_TYPE_SSIZE_T
62AC_TYPE_PID_T 62AC_TYPE_PID_T
63 63
64AC_HEADER_STDBOOL 64AC_HEADER_STDBOOL
65AC_CHECK_FUNCS([getpagesize stpcpy stpncpy]) 65AC_CHECK_FUNCS([getpagesize stpcpy])
66AC_CHECK_DECLS([stpcpy, stpncpy]) 66AC_CHECK_DECLS([stpcpy])
67AC_CHECK_FUNC([process_vm_readv], 67AC_CHECK_FUNC([process_vm_readv],
68 [AC_CHECK_FUNC([process_vm_writev], 68 [AC_CHECK_FUNC([process_vm_writev],
69 [AC_CHECK_DECL([process_vm_readv], 69 [AC_CHECK_DECL([process_vm_readv],
diff --git a/exec/exec.c b/exec/exec.c
index 935c94a59af..dae05755675 100644
--- a/exec/exec.c
+++ b/exec/exec.c
@@ -66,74 +66,6 @@ rpl_stpcpy (char *dest, const char *src)
66#define stpcpy rpl_stpcpy 66#define stpcpy rpl_stpcpy
67#endif /* !defined HAVE_STPCPY || !defined HAVE_DECL_STPCPY */ 67#endif /* !defined HAVE_STPCPY || !defined HAVE_DECL_STPCPY */
68 68
69#if !defined HAVE_STPNCPY || !defined HAVE_DECL_STPNCPY
70
71/* Copy no more than N bytes of SRC to DST, returning a pointer past
72 the last non-NUL byte written into DST. */
73
74static char *
75rpl_stpncpy (char *dest, const char *src, size_t n)
76{
77 char c, *s;
78 size_t n4;
79
80 s = dest;
81
82 if (n >= 4)
83 {
84 n4 = n >> 2;
85
86 for (;;)
87 {
88 c = *src++;
89 *dest++ = c;
90 if (c == '\0')
91 break;
92 c = *src++;
93 *dest++ = c;
94 if (c == '\0')
95 break;
96 c = *src++;
97 *dest++ = c;
98 if (c == '\0')
99 break;
100 c = *src++;
101 *dest++ = c;
102 if (c == '\0')
103 break;
104 if (--n4 == 0)
105 goto last_chars;
106 }
107 n -= dest - s;
108 goto zero_fill;
109 }
110
111 last_chars:
112 n &= 3;
113 if (n == 0)
114 return dest;
115
116 for (;;)
117 {
118 c = *src++;
119 --n;
120 *dest++ = c;
121 if (c == '\0')
122 break;
123 if (n == 0)
124 return dest;
125 }
126
127 zero_fill:
128 while (n-- > 0)
129 dest[n] = '\0';
130
131 return dest - 1;
132}
133
134#define stpncpy rpl_stpncpy
135#endif /* !defined HAVE_STPNCPY || !defined HAVE_DECL_STPNCPY */
136
137 69
138 70
139/* Executable reading functions. 71/* Executable reading functions.
@@ -1005,13 +937,14 @@ exec_0 (char *name, struct exec_tracee *tracee,
1005 else 937 else
1006 { 938 {
1007 /* If name is not absolute, then make it relative to TRACEE's 939 /* If name is not absolute, then make it relative to TRACEE's
1008 cwd. Use stpcpy, as sprintf is not reentrant. */ 940 cwd. Do not use sprintf at it is not reentrant and it
941 mishandles results longer than INT_MAX. */
1009 942
1010 if (name[0] && name[0] != '/') 943 if (name[0] && name[0] != '/')
1011 { 944 {
1012 /* Clear `buffer'. */ 945 /* Clear both buffers. */
1013 memset (buffer, 0, sizeof buffer); 946 memset (buffer, 0, sizeof buffer);
1014 memset (buffer1, 0, sizeof buffer); 947 memset (buffer1, 0, sizeof buffer1);
1015 948
1016 /* Copy over /proc, the PID, and /cwd/. */ 949 /* Copy over /proc, the PID, and /cwd/. */
1017 rewrite = stpcpy (buffer, "/proc/"); 950 rewrite = stpcpy (buffer, "/proc/");
@@ -1042,7 +975,7 @@ exec_0 (char *name, struct exec_tracee *tracee,
1042 975
1043 rewrite = buffer1 + link_size; 976 rewrite = buffer1 + link_size;
1044 remaining = buffer1 + sizeof buffer1 - rewrite - 1; 977 remaining = buffer1 + sizeof buffer1 - rewrite - 1;
1045 rewrite = stpncpy (rewrite, name, remaining); 978 memcpy (rewrite, name, strnlen (name, remaining));
1046 979
1047 /* Replace name with buffer1. */ 980 /* Replace name with buffer1. */
1048#ifndef REENTRANT 981#ifndef REENTRANT