Skip to content

r3.gwflow: fix segmentation fault #6168

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jayneel-shah18
Copy link
Contributor

This PR resolves a segmentation fault in the r3.gwflow module that occurs when the optional sink parameter is not provided. The issue was caused by unconditional dereferencing of param.q->answer and use of data->q, even when the q array was never allocated.

Fix Summary

  • Memory Allocation:

    • Changed:

      N_alloc_gwflow_data3d(cols, rows, depths, 0, 0);

      to:

      N_alloc_gwflow_data3d(cols, rows, depths, 1, 0);

      to ensure data->q is always allocated.

  • Runtime Check:

    • Replaced the sink reading line:

      N_read_rast3d_to_array_3d(param.q->answer, data->q, param.mask->answer);

      with:

      if (param.q->answer != NULL) {
          N_read_rast3d_to_array_3d(param.q->answer, data->q, param.mask->answer);
          N_convert_array_3d_null_to_zero(data->q);
      } else {
          G_message(_("No sink map provided. Initializing zero sink..."));
          for (z = 0; z < geom->depths; z++) {
              for (y = 0; y < geom->rows; y++) {
                  for (x = 0; x < geom->cols; x++) {
                      N_put_array_3d_d_value(data->q, x, y, z, 0.0);
                  }
              }
          }
      }

r3.gwflow now works as expected without sink parameter. This PR fixes #6142.

@github-actions github-actions bot added C Related code is in C module raster3d labels Aug 4, 2025
@petrasovaa
Copy link
Contributor

Why the need for the change to the N_alloc_gwflow_data3d call?

@jayneel-shah18
Copy link
Contributor Author

The change ensures data->q is safely allocated when needed and aligns with how other optional conditions are handled. It also prevents undefined behavior and keeps the memory layout consistent for future extensions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C Related code is in C module raster3d
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] r3.gwflow crashes when optional sink parameter is not provided
2 participants