Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Fix UB in Inputs Passing #2726

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Python/pywarpx/_libwarpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
_path_libc = _find_library('c')
_libc = ctypes.CDLL(_path_libc)
_LP_c_int = ctypes.POINTER(ctypes.c_int)
_LP_c_char = ctypes.POINTER(ctypes.c_char)
_LP_c_char = ctypes.c_char_p


class LibWarpX():
Expand Down Expand Up @@ -397,10 +397,15 @@ def get_nattr_species(self, species_name):
def amrex_init(self, argv, mpi_comm=None):
# --- Construct the ctype list of strings to pass in
argc = len(argv)

# note: +1 since there is an extra char-string array element,
# that ANSII C requires to be a simple NULL entry
# https://stackoverflow.com/a/39096006/2719194
argvC = (_LP_c_char * (argc+1))()
ax3l marked this conversation as resolved.
Show resolved Hide resolved
for i, arg in enumerate(argv):
enc_arg = arg.encode('utf-8')
argvC[i] = ctypes.create_string_buffer(enc_arg)
argvC[i] = _LP_c_char(enc_arg)
argvC[argc] = _LP_c_char(b"\0") # +1 element must be NULL

if mpi_comm is None or MPI is None:
self.libwarpx_so.amrex_init(argc, argvC)
Expand Down