espressomd / espresso

The ESPResSo package
https://espressomd.org
GNU General Public License v3.0
222 stars 183 forks source link

Vector-valued input parameters may be replaced by uninitialized data #4911

Closed jngrad closed 2 months ago

jngrad commented 2 months ago

In the ESPResSo 4.2 line, we generalized the use of the script interface methods make_Vector3d() and make_Vector3i, but we forgot to add the except * keyword to propagate exceptions. When we call methods with arguments that are not homologous to a list of size 3, such as a list of size 2 or a string of length 2 or an integer or a float, a TypeError is raised and immediately silenced with a message printed to the terminal. The returned Vector3d or Vector3i then contains uninitialized data and the simulation script continues running.

Here is a MWE:

import espressomd.lb
system = espressomd.System(box_l=3*[10.])
system.time_step = 0.01
lb_fluid = espressomd.lb.LBFluid(
    agrid=1.0, dens=1.0, visc=1.0, tau=0.01,
    ext_force_density=1., kT=1.0, seed=32)
system.actors.add(lb_fluid)
print("script continues")
print(lb_fluid.get_params()["ext_force_density"])  # shows random values

Output:

TypeError: 'float' object is not iterable
Exception ignored in: 'espressomd.utils.make_Vector3d'
Traceback (most recent call last):
  File "/work/jgrad/espresso/build/src/python/espressomd/actors.py", line 59, in add
    actor._activate()
TypeError: 'float' object is not iterable
script continues
[1.00000000e-002 1.00000000e+000 1.10940916e-310]

Expected output:

Traceback (most recent call last):
  File "/work/jgrad/espresso/build/mwe.py", line 7, in <module>
    system.actors.add(lb_fluid)
  File "/work/jgrad/espresso/build/src/python/espressomd/actors.py", line 59, in add
    actor._activate()
  File "lb.pyx", line 69, in espressomd.lb.FluidActor._activate
  File "lb.pyx", line 556, in espressomd.lb.LBFluid._activate_method
  File "lb.pyx", line 293, in espressomd.lb.HydrodynamicInteraction._set_params_in_es_core
  File "lb.pyx", line 489, in espressomd.lb.HydrodynamicInteraction.ext_force_density.__set__
  File "utils.pyx", line 246, in espressomd.utils.make_Vector3d
TypeError: 'float' object is not iterable

While the bug was also present in the 4.1 line, all call sites would first check whether the argument was homologous to a vector of size 3 before passing it to make_Vector3d(). In the python branch, these functions were replaced by a safer alternative that raises a RuntimeError.

Many thanks to @pm-blanco for reporting this issue to us.

jngrad commented 2 months ago

Fixed in 4.2 by 89737a8cc6b6668b5881a598f691f08fa3930482.