Skip to content

Commit

Permalink
Fix security issue (#305)
Browse files Browse the repository at this point in the history
Change sprintf to snprintf to avoid potential security issue
  • Loading branch information
Binyang2014 authored May 26, 2024
1 parent f76eae4 commit 3a18068
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/utils_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ static bool matchPort(const int port1, const int port2) {
namespace mscclpp {
std::string int64ToBusId(int64_t id) {
char busId[20];
std::sprintf(busId, "%04lx:%02lx:%02lx.%01lx", (id) >> 20, (id & 0xff000) >> 12, (id & 0xff0) >> 4, (id & 0xf));
std::snprintf(busId, sizeof(busId), "%04lx:%02lx:%02lx.%01lx", (id) >> 20, (id & 0xff000) >> 12, (id & 0xff0) >> 4,
(id & 0xf));
return std::string(busId);
}

Expand Down Expand Up @@ -111,7 +112,7 @@ uint64_t getHostHash(void) {
uint64_t computePidHash(void) {
char pname[1024];
// Start off with our pid ($$)
sprintf(pname, "%ld", (long)getpid());
std::snprintf(pname, sizeof(pname), "%ld", (long)getpid());
int plen = strlen(pname);
int len = readlink("/proc/self/ns/pid", pname + plen, sizeof(pname) - 1 - plen);
if (len < 0) len = 0;
Expand Down

0 comments on commit 3a18068

Please sign in to comment.