Skip to content

Commit 0486952

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 <[email protected]> (cherry picked from commit 30d6099) Conflicts: opal/mca/hwloc/base/hwloc_base_util.c
1 parent db1239a commit 0486952

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

opal/mca/hwloc/base/hwloc_base_util.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
* Copyright (c) 2013-2017 Intel, Inc. All rights reserved.
1717
* Copyright (c) 2015-2017 Research Organization for Information Science
1818
* and Technology (RIST). All rights reserved.
19+
* Copyright (C) 2018 Mellanox Technologies, Ltd.
20+
* All rights reserved.
21+
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
22+
* Copyright (c) 2019 IBM Corporation. All rights reserved.
1923
* $COPYRIGHT$
2024
*
2125
* Additional copyrights may follow
@@ -1891,14 +1895,14 @@ int opal_hwloc_base_cset2str(char *str, int len,
18911895
for (core_index = 0; core_index < num_cores; ++core_index) {
18921896
if (map[socket_index][core_index] > 0) {
18931897
if (!first) {
1894-
strncat(str, ", ", len - strlen(str));
1898+
strncat(str, ", ", len - strlen(str) - 1);
18951899
}
18961900
first = false;
18971901

18981902
snprintf(tmp, stmp, "socket %d[core %d[hwt %s]]",
18991903
socket_index, core_index,
19001904
bitmap2rangestr(map[socket_index][core_index]));
1901-
strncat(str, tmp, len - strlen(str));
1905+
strncat(str, tmp, len - strlen(str) - 1);
19021906
}
19031907
}
19041908
}
@@ -1956,7 +1960,7 @@ int opal_hwloc_base_cset2mapstr(char *str, int len,
19561960
for (socket = hwloc_get_obj_by_type(topo, HWLOC_OBJ_SOCKET, 0);
19571961
NULL != socket;
19581962
socket = socket->next_cousin) {
1959-
strncat(str, "[", len - strlen(str));
1963+
strncat(str, "[", len - strlen(str) - 1);
19601964

19611965
/* Iterate over all existing cores in this socket */
19621966
core_index = 0;
@@ -1968,7 +1972,7 @@ int opal_hwloc_base_cset2mapstr(char *str, int len,
19681972
socket->cpuset,
19691973
HWLOC_OBJ_CORE, ++core_index)) {
19701974
if (core_index > 0) {
1971-
strncat(str, "/", len - strlen(str));
1975+
strncat(str, "/", len - strlen(str) - 1);
19721976
}
19731977

19741978
/* Iterate over all existing PUs in this core */
@@ -1983,13 +1987,13 @@ int opal_hwloc_base_cset2mapstr(char *str, int len,
19831987

19841988
/* Is this PU in the cpuset? */
19851989
if (hwloc_bitmap_isset(cpuset, pu->os_index)) {
1986-
strncat(str, "B", len - strlen(str));
1990+
strncat(str, "B", len - strlen(str) - 1);
19871991
} else {
1988-
strncat(str, ".", len - strlen(str));
1992+
strncat(str, ".", len - strlen(str) - 1);
19891993
}
19901994
}
19911995
}
1992-
strncat(str, "]", len - strlen(str));
1996+
strncat(str, "]", len - strlen(str) - 1);
19931997
}
19941998

19951999
return OPAL_SUCCESS;

0 commit comments

Comments
 (0)