Skip to content

Commit 91e5647

Browse files
committed
Get rid of strcpy
On OpenBSD: ``` ld: warning: namespace.c:731(namespace.o:(rb_namespace_local_extension)): warning: strcpy() is almost always misused, please use strlcpy() ```
1 parent ff93185 commit 91e5647

File tree

5 files changed

+26
-29
lines changed

5 files changed

+26
-29
lines changed

addr2line.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,12 +637,13 @@ follow_debuglink_build_id(const char *build_id, size_t build_id_size, int num_tr
637637
obj_info_t **objp, line_info_t *lines, int offset, FILE *errout)
638638
{
639639
static const char global_debug_dir[] = "/usr/lib/debug/.build-id/";
640+
static const char debug_suffix[] = ".debug";
640641
const size_t global_debug_dir_len = sizeof(global_debug_dir) - 1;
641642
char *p;
642643
obj_info_t *o1 = *objp, *o2;
643644
size_t i;
644645

645-
if (PATH_MAX < global_debug_dir_len + 1 + build_id_size * 2 + 6) return;
646+
if (PATH_MAX < global_debug_dir_len + build_id_size * 2 + sizeof(debug_suffix)) return;
646647

647648
memcpy(binary_filename, global_debug_dir, global_debug_dir_len);
648649
p = binary_filename + global_debug_dir_len;
@@ -653,7 +654,7 @@ follow_debuglink_build_id(const char *build_id, size_t build_id_size, int num_tr
653654
*p++ = tbl[n % 16];
654655
if (i == 0) *p++ = '/';
655656
}
656-
strcpy(p, ".debug");
657+
memcpy(p, debug_suffix, sizeof(debug_suffix));
657658

658659
append_obj(objp);
659660
o2 = *objp;

ext/socket/getaddrinfo.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,7 @@ static const char *const ai_errlist[] = {
171171

172172
#define GET_CANONNAME(ai, str) \
173173
if (pai->ai_flags & AI_CANONNAME) {\
174-
if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\
175-
strcpy((ai)->ai_canonname, (str));\
176-
} else {\
174+
if (((ai)->ai_canonname = strdup(str)) == NULL) {\
177175
error = EAI_MEMORY;\
178176
goto free;\
179177
}\

ext/socket/getnameinfo.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,14 @@ getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, socklen_t ho
158158
/* what we should do? */
159159
} else if (flags & NI_NUMERICSERV) {
160160
snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
161-
if (strlen(numserv) + 1 > servlen)
161+
if (strlcpy(serv, numserv, servlen) >= servlen)
162162
return ENI_MEMORY;
163-
strcpy(serv, numserv);
164163
} else {
165164
#if defined(HAVE_GETSERVBYPORT)
166165
struct servent *sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
167166
if (sp) {
168-
if (strlen(sp->s_name) + 1 > servlen)
167+
if (strlcpy(serv, sp->s_name, servlen) >= servlen)
169168
return ENI_MEMORY;
170-
strcpy(serv, sp->s_name);
171169
} else
172170
return ENI_NOSERVNAME;
173171
#else
@@ -202,9 +200,8 @@ getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, socklen_t ho
202200
if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
203201
== NULL)
204202
return ENI_SYSTEM;
205-
if (strlen(numaddr) > hostlen)
203+
if (strlcpy(host, numaddr, hostlen) >= hostlen)
206204
return ENI_MEMORY;
207-
strcpy(host, numaddr);
208205
} else {
209206
#ifdef INET6
210207
hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
@@ -218,13 +215,12 @@ getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, socklen_t ho
218215
p = strchr(hp->h_name, '.');
219216
if (p) *p = '\0';
220217
}
221-
if (strlen(hp->h_name) + 1 > hostlen) {
218+
if (strlcpy(host, hp->h_name, hostlen) >= hostlen) {
222219
#ifdef INET6
223220
freehostent(hp);
224221
#endif
225222
return ENI_MEMORY;
226223
}
227-
strcpy(host, hp->h_name);
228224
#ifdef INET6
229225
freehostent(hp);
230226
#endif
@@ -234,9 +230,8 @@ getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, socklen_t ho
234230
if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
235231
== NULL)
236232
return ENI_NOHOSTNAME;
237-
if (strlen(numaddr) > hostlen)
233+
if (strlcpy(host, numaddr, hostlen) >= hostlen)
238234
return ENI_MEMORY;
239-
strcpy(host, numaddr);
240235
}
241236
}
242237
return SUCCESS;

ext/socket/raddrinfo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,15 @@ allocate_getaddrinfo_arg(const char *hostp, const char *portp, const struct addr
387387

388388
if (hostp) {
389389
arg->node = buf + hostp_offset;
390-
strcpy(arg->node, hostp);
390+
memcpy(arg->node, hostp, portp_offset - hostp_offset);
391391
}
392392
else {
393393
arg->node = NULL;
394394
}
395395

396396
if (portp) {
397397
arg->service = buf + portp_offset;
398-
strcpy(arg->service, portp);
398+
memcpy(arg->service, portp, bufsize - portp_offset);
399399
}
400400
else {
401401
arg->service = NULL;

namespace.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -725,28 +725,31 @@ copy_ext_file(char *src_path, char *dst_path)
725725
#define IS_DLEXT(e) (strcmp((e), DLEXT) == 0)
726726

727727
static void
728-
fname_without_suffix(char *fname, char *rvalue)
728+
fname_without_suffix(const char *fname, char *rvalue, size_t rsize)
729729
{
730-
char *pos;
731-
strcpy(rvalue, fname);
732-
for (pos = rvalue + strlen(fname); pos > rvalue; pos--) {
730+
size_t len = strlen(fname);
731+
const char *pos;
732+
for (pos = fname + len; pos > fname; pos--) {
733733
if (IS_SOEXT(pos) || IS_DLEXT(pos)) {
734-
*pos = '\0';
735-
return;
734+
len = pos - fname;
735+
break;
736736
}
737737
}
738+
if (len > rsize - 1) len = rsize - 1;
739+
memcpy(rvalue, fname, len);
740+
rvalue[len] = '\0';
738741
}
739742

740743
static void
741-
escaped_basename(char *path, char *fname, char *rvalue)
744+
escaped_basename(const char *path, const char *fname, char *rvalue, size_t rsize)
742745
{
743-
char *pos, *leaf, *found;
744-
leaf = path;
746+
char *pos;
747+
const char *leaf = path, *found;
745748
// `leaf + 1` looks uncomfortable (when leaf == path), but fname must not be the top-dir itself
746749
while ((found = strstr(leaf + 1, fname)) != NULL) {
747750
leaf = found; // find the last occurrence for the path like /etc/my-crazy-lib-dir/etc.so
748751
}
749-
strcpy(rvalue, leaf);
752+
strlcpy(rvalue, leaf, rsize);
750753
for (pos = rvalue; *pos; pos++) {
751754
if (isdirsep(*pos)) {
752755
*pos = '+';
@@ -762,8 +765,8 @@ rb_namespace_local_extension(VALUE namespace, VALUE fname, VALUE path)
762765
char *src_path = RSTRING_PTR(path), *fname_ptr = RSTRING_PTR(fname);
763766
rb_namespace_t *ns = rb_get_namespace_t(namespace);
764767

765-
fname_without_suffix(fname_ptr, fname2);
766-
escaped_basename(src_path, fname2, basename);
768+
fname_without_suffix(fname_ptr, fname2, sizeof(fname2));
769+
escaped_basename(src_path, fname2, basename, sizeof(basename));
767770

768771
wrote = sprint_ext_filename(ext_path, sizeof(ext_path), ns->ns_id, NAMESPACE_TMP_PREFIX, basename);
769772
if (wrote >= (int)sizeof(ext_path)) {

0 commit comments

Comments
 (0)