Skip to content

Commit 04439c6

Browse files
committed
Get rid of strcpy
1 parent 9fedae9 commit 04439c6

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

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)