Fix AttributeError in facebudget when front is omitted (fixes #1097)#1849
Open
gaoflow wants to merge 2 commits into
Open
Fix AttributeError in facebudget when front is omitted (fixes #1097)#1849gaoflow wants to merge 2 commits into
gaoflow wants to merge 2 commits into
Conversation
facebudget requires only one of front/lower/right (it raises if all three are None), but two code paths dereferenced front unconditionally: * the transient loop bound `range(front.coords["time"].size)`, and * the no-zones branch `dask.array.full(front.shape, ...)` (and the matching lower/right lines). So calling e.g. `facebudget(zone, lower=lower)` raised `AttributeError: 'NoneType' object has no attribute 'coords'` (or 'shape'). This is the null-dereference flagged by Coverity in Deltares#1097. Pick the first supplied DataArray as a reference for the time axis and the output shape (all three are validated to share budgetzone's shape). Add regression tests covering the omitted-front case with and without a time dimension, including the no-zones branch.
Contributor
|
Looks good to me, could you also add your fix to the changelog? Write about it with users in mind, who are not software developers. |
Contributor
Author
|
Done — added a Fixed entry to the changelog under [Unreleased], phrased for users: it notes that |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
imod.evaluate.facebudgetdocuments and enforces that at least one offront,lower,rightmust be supplied (it raisesValueErrorif all threeare
None). However two code paths dereferencedfrontunconditionally, soomitting
frontcrashed:So a perfectly valid call such as
facebudget(budgetzone, lower=lower)raised:This is exactly the null-dereference Coverity flagged in #1097
(
REVERSE_INULL:frontis dereferenced on all paths before theif front is not Noneguard inside the loop). The other location named in#1097 (
package.py:292) no longer exists — that code was refactored intoimod/evaluate/budget.py, which is the surviving occurrence fixed here.Fix
Pick the first supplied DataArray as a reference for the time axis and the
output shape. All three inputs are already validated to share
budgetzone'sspatial shape, so any of them is a valid reference:
Reproduction / verification
Added
test_omit_frontandtest_omit_front_transienttoimod/tests/test_evaluate/test_budget.py. The transient test exercises bothcrash sites (the time loop and the no-zones branch) via a partial zone and an
all-inactive zone. Verified locally: both tests fail on
master(
AttributeError) and pass with this change; all pre-existing budget tests(9) still pass.
Fixes #1097.