Skip to content

Clean up string return values - #979

Open
eschnett wants to merge 4 commits into
masterfrom
eschnett/Clean-up-string-return-values
Open

Clean up string return values#979
eschnett wants to merge 4 commits into
masterfrom
eschnett/Clean-up-string-return-values

Conversation

@eschnett

@eschnett eschnett commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Open MPI sometimes include a trailing NUL into string values it returns. C doesn't see this. Remove all trailing NULs explicitly.

Comment thread src/MPI.jl
written, *excluding* the terminating NUL. Open MPI's `MPI_Get_library_version`
includes it, so trim instead of letting a NUL into the string.
"""
function _string_from_buffer(buf::Vector{UInt8}, len::Integer)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm mildly sure Base has something for this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Apparently Base doesn't for Vector{UInt8}. We can convert to String first, then strip (which gives a SubString), then maybe convert to String again?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can use chopsuffix which would remove at most one NUL. That's probably good enough.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Doesn't

GC.@preserve buf unsafe_string(pointer(buf, len))

do what you want? May still need to clamp len though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

unsafe_string would ignore everything after the first NUL. I'm looking for a function that only removes a trailing NUL, if one is there.

Yes, clamping would be necessary anyway.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not sure I understand? When would a NUL occur in a valid string?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

unsafe_string without a length argument would be unsafe because the buffer might not be NUL terminated.

unsafe_string with a length argument doesn't stop at NUL, it uses exactly that many bytes.

I think the current approach is good.

Comment thread src/datatypes.jl
lenref = Ref{Cint}()
API.MPI_Type_get_name(datatype, buffer, lenref)
return String(resize!(buffer, lenref[]))
return _string_from_buffer(buffer, lenref[])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks like an upstream bug.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It does, doesn't it? But Open MPI is so widely used that we should introduce a work-around, especially since all C/C++ users will never have notices this bug.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I filed an upstream report at open-mpi/ompi#14411.

Open MPI sometimes include a trailing NUL into string values it returns.
C doesn't see this. Remove all trailing NULs explicitly.
chopsuffix returns a SubString{String}, so Get_processor_name,
Get_library_version, error_string and get_name stopped returning a
String. test_misc.jl asserts `MPI.Get_processor_name() isa String`,
which failed on every CI job.

The resize!/findlast form also drops *all* trailing NULs, where
chopsuffix removed only one.
Documenter checks all docstrings in the MPI module against the manual
and errors on ones with no @docs entry, failing docs-build. This is an
internal helper, so make it a plain comment instead.
@eschnett
eschnett force-pushed the eschnett/Clean-up-string-return-values branch from 0aa804a to 26fadc9 Compare September 9, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants