Skip to content

Commit 30d6099

Browse files
committed
opal_hwloc_base_cset2str() off-by-1 in its strncat()
I think the strncat() calls here need to be of the form strncat(str, new_str_to_add, len - strlen(new_str_to_addstr) - 1); since in the OMPI calls len is being used as total number of bytes in str. strncat(dest,src,n) on the other hand is documented as writing up to n chars from the incoming string plus 1 for the null, for n+1 total bytes it can write. Signed-off-by: Mark Allen <markalle@us.ibm.com>
1 parent 804a517 commit 30d6099

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

opal/mca/hwloc/base/hwloc_base_util.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Copyright (C) 2018 Mellanox Technologies, Ltd.
2020
* All rights reserved.
2121
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
22+
* Copyright (c) 2019 IBM Corporation. All rights reserved.
2223
* $COPYRIGHT$
2324
*
2425
* Additional copyrights may follow
@@ -1728,14 +1729,14 @@ int opal_hwloc_base_cset2str(char *str, int len,
17281729
for (core_index = 0; core_index < num_cores; ++core_index) {
17291730
if (map[socket_index][core_index] > 0) {
17301731
if (!first) {
1731-
strncat(str, ", ", len - strlen(str));
1732+
strncat(str, ", ", len - strlen(str) - 1);
17321733
}
17331734
first = false;
17341735

17351736
snprintf(tmp, stmp, "socket %d[core %d[hwt %s]]",
17361737
socket_index, core_index,
17371738
bitmap2rangestr(map[socket_index][core_index]));
1738-
strncat(str, tmp, len - strlen(str));
1739+
strncat(str, tmp, len - strlen(str) - 1);
17391740
}
17401741
}
17411742
}
@@ -1791,7 +1792,7 @@ int opal_hwloc_base_cset2mapstr(char *str, int len,
17911792
for (socket = hwloc_get_obj_by_type(topo, HWLOC_OBJ_SOCKET, 0);
17921793
NULL != socket;
17931794
socket = socket->next_cousin) {
1794-
strncat(str, "[", len - strlen(str));
1795+
strncat(str, "[", len - strlen(str) - 1);
17951796

17961797
/* Iterate over all existing cores in this socket */
17971798
core_index = 0;
@@ -1803,7 +1804,7 @@ int opal_hwloc_base_cset2mapstr(char *str, int len,
18031804
socket->cpuset,
18041805
HWLOC_OBJ_CORE, ++core_index)) {
18051806
if (core_index > 0) {
1806-
strncat(str, "/", len - strlen(str));
1807+
strncat(str, "/", len - strlen(str) - 1);
18071808
}
18081809

18091810
/* Iterate over all existing PUs in this core */
@@ -1818,13 +1819,13 @@ int opal_hwloc_base_cset2mapstr(char *str, int len,
18181819

18191820
/* Is this PU in the cpuset? */
18201821
if (hwloc_bitmap_isset(cpuset, pu->os_index)) {
1821-
strncat(str, "B", len - strlen(str));
1822+
strncat(str, "B", len - strlen(str) - 1);
18221823
} else {
1823-
strncat(str, ".", len - strlen(str));
1824+
strncat(str, ".", len - strlen(str) - 1);
18241825
}
18251826
}
18261827
}
1827-
strncat(str, "]", len - strlen(str));
1828+
strncat(str, "]", len - strlen(str) - 1);
18281829
}
18291830

18301831
return OPAL_SUCCESS;

0 commit comments

Comments
 (0)