Skip to content

Commit 1b6d67b

Browse files
authored
Merge pull request #367 from linhu-nv/sonarqube-fix
fix some potential buffer overflow problems as suggested by sonarqube
2 parents 6d1a8de + 5a9accc commit 1b6d67b

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

cpp/src/wholememory/communicator.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,7 @@ struct rank_info {
422422

423423
static void get_host_name(char* hostname, int maxlen, const char delim)
424424
{
425-
if (gethostname(hostname, maxlen) != 0) {
426-
strncpy(hostname, "unknown", maxlen);
427-
WHOLEMEMORY_FATAL("gethostname failed.");
428-
}
425+
if (gethostname(hostname, maxlen) != 0) { WHOLEMEMORY_FATAL("gethostname failed."); }
429426
int i = 0;
430427
while ((hostname[i] != delim) && (hostname[i] != '\0') && (i < maxlen - 1))
431428
i++;
@@ -448,22 +445,25 @@ void get_boot_id(char* host_id, size_t len)
448445

449446
if ((env_host_id = getenv("WHOLEMEMORY_BOOTID")) != nullptr) {
450447
WHOLEMEMORY_LOG(LEVEL_INFO, "WHOLEMEMORY_BOOTID set by environment to %s", env_host_id);
451-
strncpy(host_id, env_host_id, len - 1);
452-
offset = strlen(env_host_id);
448+
size_t copy_len = std::min(strlen(env_host_id), len - 1);
449+
memcpy(host_id, env_host_id, copy_len);
450+
offset = copy_len;
453451
} else {
454452
FILE* file = fopen(BOOTID_FILE, "r");
455453
if (file != nullptr) {
456454
char* p;
457455
if (fscanf(file, "%ms", &p) == 1) {
458-
strncpy(host_id + offset, p, len - offset - 1);
459-
offset += strlen(p);
456+
size_t remaining = len - offset - 1;
457+
size_t copy_len = std::min(strlen(p), remaining);
458+
memcpy(host_id + offset, p, copy_len);
459+
offset += copy_len;
460460
free(p);
461461
}
462+
fclose(file);
462463
}
463-
fclose(file);
464464
}
465465

466-
#undef HOSTID_FILE
466+
#undef BOOTID_FILE
467467

468468
host_id[offset] = '\0';
469469
}

cpp/src/wholememory/memory_handle.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,13 @@ class continuous_device_wholememory_impl : public wholememory_impl {
831831
// Construct client address to send this Shareable handle to
832832
bzero(&cliaddr, sizeof(cliaddr));
833833
cliaddr.sun_family = AF_UNIX;
834+
if (dst_name.length() >= sizeof(cliaddr.sun_path)) {
835+
WHOLEMEMORY_FATAL(
836+
"IPC socket path length (%zu) larger than sockaddr_un.sun_path length (%lu), full_path: %s",
837+
dst_name.length(),
838+
sizeof(cliaddr.sun_path),
839+
dst_name.c_str());
840+
}
834841
strcpy(cliaddr.sun_path, dst_name.c_str());
835842

836843
// Send corresponding shareable handle to the client

0 commit comments

Comments
 (0)