Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mooncake-transfer-engine/src/topology.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ static int getPciDistance(const char *bus1, const char *bus2) {
distance += (*ptr2 == '/');
}

// If both devices report the same valid NUMA node, treat them as
// one hop closer by subtracting 1 from the computed distance.
int numa1 = -1;
int numa2 = -1;
char numa_path[PATH_MAX];
snprintf(numa_path, sizeof(numa_path), "%s/numa_node", path1);
std::ifstream(numa_path) >> numa1;
snprintf(numa_path, sizeof(numa_path), "%s/numa_node", path2);
std::ifstream(numa_path) >> numa2;
Comment on lines +162 to +165
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The snprintf calls to construct the numa_node path could result in a truncated path if the source path (path1 or path2) is very long. This would cause the file read to fail silently, and the NUMA affinity optimization would not be applied. It's safer to check the return value of snprintf to detect and handle potential truncation.

Additionally, the logic to read the NUMA node is duplicated. While the suggestion below fixes the immediate safety issue, I'd recommend refactoring this logic into a helper function to improve code maintainability and readability in a follow-up.

Suggested change
snprintf(numa_path, sizeof(numa_path), "%s/numa_node", path1);
std::ifstream(numa_path) >> numa1;
snprintf(numa_path, sizeof(numa_path), "%s/numa_node", path2);
std::ifstream(numa_path) >> numa2;
int ret = snprintf(numa_path, sizeof(numa_path), "%s/numa_node", path1);
if (ret > 0 && static_cast<size_t>(ret) < sizeof(numa_path)) {
std::ifstream(numa_path) >> numa1;
}
ret = snprintf(numa_path, sizeof(numa_path), "%s/numa_node", path2);
if (ret > 0 && static_cast<size_t>(ret) < sizeof(numa_path)) {
std::ifstream(numa_path) >> numa2;
}


if (numa1 != -1 && numa1 == numa2 && distance > 0) {
distance -= 1;
}

return distance;
}

Expand Down
Loading