Skip to content

Commit 694e78a

Browse files
committed
fortran: fix ompi string c2f where len(fstr) < len(cstr)
Thanks to Ben Menadue for pointing out that ompi_fortran_string_c2f() missed a case to properly terminate the resulting Fortran string when copying from a longer C source string. Signed-off-by: Jeff Squyres <jeff@squyres.com>
1 parent 59191a7 commit 694e78a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

ompi/mpi/fortran/base/strings.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Copyright (c) 2010-2018 Cisco Systems, Inc. All rights reserved
1313
* Copyright (c) 2017 Research Organization for Information Science
1414
* and Technology (RIST). All rights reserved.
15+
* Copyright (c) 2025 Jeffrey M. Squyres. All rights reserved.
1516
* $COPYRIGHT$
1617
*
1718
* Additional copyrights may follow
@@ -95,8 +96,19 @@ int ompi_fortran_string_c2f(const char *cstr, char *fstr, int len)
9596
int i;
9697

9798
opal_string_copy(fstr, cstr, len);
98-
for (i = strlen(cstr); i < len; ++i) {
99-
fstr[i] = ' ';
99+
100+
// If len < len(cstr), then opal_string_copy() will have copied a
101+
// trailing \0 into the last position in fstr. This is not what
102+
// Fortran wants; overwrite that \0 with the actual last character
103+
// that will fit into fstr.
104+
if (len < strlen(cstr)) {
105+
fstr[len - 1] = cstr[len - 1];
106+
} else {
107+
// Otherwise, pad the end of the resulting Fortran string with
108+
// spaces.
109+
for (i = strlen(cstr); i < len; ++i) {
110+
fstr[i] = ' ';
111+
}
100112
}
101113

102114
return OMPI_SUCCESS;

0 commit comments

Comments
 (0)