Skip to content

Use of uninitialized parameter can lead to use of uninitialized array in MOM_thickness_diffuse (possible segfault) #1696

Description

@pittwolfe

The parameter thickness_diffuse_CS@Use_KH_in_MEKE is given a value only if use_meke is true:

Lines 2385-2388 in MOM_thickness_diffuse.F90

  if (use_meke) then
    call get_param(param_file, mdl, "USE_KH_IN_MEKE", CS%Use_KH_in_MEKE, &
                   "If true, uses the thickness diffusivity calculated here to diffuse MEKE.", &
                   default=.false.)

However, Use_KH_in_MEKE is later tested in a way that doens't depend on the value of use_meke:

Line 553

  if (CS%id_KH_t > 0 .or. CS%id_KH_t1 > 0 .or. CS%Use_KH_in_MEKE) then

and lines 581-582

      if (CS%Use_KH_in_MEKE) then
        MEKE%Kh_diff(:,:) = 0.0

This last instance can lead to a segmentation fault since MEKE%Kh_diff is only allocated if use_meke is true. Note that lines 581-582 are only reached if the user requests diffusivity diagnostics (so line 553 evaluates to true), but I don't see any reason why diffusivity diagnostics should require MEKE.

The actual outcome seems to depend on the compiler, which is probably due to how they give uninitialized logical variables values. I get a crash due to segfault with the GNU compiler, but not with the Intel compiler.

The simplest solution would be to move the get_param call above the if (use_meke) then test. I could do a pull request with this change, but it'd probably be faster to have a more active developer do this than for me to try to figure out the respository & pull request policies.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions