From 147548988a64769c822bb8f22f8ba93d63534340 Mon Sep 17 00:00:00 2001 From: Jeff Squyres Date: Tue, 26 Aug 2025 09:14:01 -0400 Subject: [PATCH 1/2] 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 (cherry picked from commit 694e78aa864e5dc219172a710bda8e129542094a) --- ompi/mpi/fortran/base/strings.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ompi/mpi/fortran/base/strings.c b/ompi/mpi/fortran/base/strings.c index 5bbd96d5eea..63ea0db6f15 100644 --- a/ompi/mpi/fortran/base/strings.c +++ b/ompi/mpi/fortran/base/strings.c @@ -12,6 +12,7 @@ * Copyright (c) 2010-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2017 Research Organization for Information Science * and Technology (RIST). All rights reserved. + * Copyright (c) 2025 Jeffrey M. Squyres. All rights reserved. * $COPYRIGHT$ * * Additional copyrights may follow @@ -95,8 +96,19 @@ int ompi_fortran_string_c2f(const char *cstr, char *fstr, int len) int i; opal_string_copy(fstr, cstr, len); - for (i = strlen(cstr); i < len; ++i) { - fstr[i] = ' '; + + // If len < len(cstr), then opal_string_copy() will have copied a + // trailing \0 into the last position in fstr. This is not what + // Fortran wants; overwrite that \0 with the actual last character + // that will fit into fstr. + if (len < strlen(cstr)) { + fstr[len - 1] = cstr[len - 1]; + } else { + // Otherwise, pad the end of the resulting Fortran string with + // spaces. + for (i = strlen(cstr); i < len; ++i) { + fstr[i] = ' '; + } } return OMPI_SUCCESS; From cc72cc7d9679f2a36223a5f20bee1d227bcea758 Mon Sep 17 00:00:00 2001 From: Jeff Squyres Date: Wed, 27 Aug 2025 10:07:51 -0400 Subject: [PATCH 2/2] fortran: fix off-by-one string copy error Followup to commit 694e78aa8: Ben Menadue correctly pointed out that < should have been <=. Signed-off-by: Jeff Squyres (cherry picked from commit cc03d5b14ad7d30991f07a57dfb0f3d004bb631b) --- ompi/mpi/fortran/base/strings.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ompi/mpi/fortran/base/strings.c b/ompi/mpi/fortran/base/strings.c index 63ea0db6f15..1016b850aaf 100644 --- a/ompi/mpi/fortran/base/strings.c +++ b/ompi/mpi/fortran/base/strings.c @@ -101,7 +101,7 @@ int ompi_fortran_string_c2f(const char *cstr, char *fstr, int len) // trailing \0 into the last position in fstr. This is not what // Fortran wants; overwrite that \0 with the actual last character // that will fit into fstr. - if (len < strlen(cstr)) { + if (len <= strlen(cstr)) { fstr[len - 1] = cstr[len - 1]; } else { // Otherwise, pad the end of the resulting Fortran string with