Skip to content

BUG: Fix cdfbin's argument range check, which could never fire - #114

Open
hjmjohnson wants to merge 2 commits into
masterfrom
pr/fix-cdfbin-range-check
Open

hjmjohnson wants to merge 2 commits into
masterfrom
pr/fix-cdfbin-range-check

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

Re-submission of #39, reverted from master on 2026-09-24 so it can be
reviewed before merging. Content is unchanged from the original.

Base: master. Independent: nothing has to land before it.

Commits
  • BUG: Fix cdfbin's argument range check, which could never fire
  • ENH: Cover cdfbin's out-of-range selector return

Ordering for all the re-submitted work is tracked in #84.

gdevenyi and others added 2 commits September 24, 2026 07:10
cdfbin() validates its `which` selector like this:

    if(!(*which < 1 && *which > 4)) goto S30;

A value cannot be both less than 1 and greater than 4, so the condition
is always false, the negation is always true, and the jump to S30 is
always taken -- skipping the entire range check.  gcc reports it as
-Wlogical-op, "logical 'and' of mutually exclusive tests is always
false".

The ten sibling functions in this file all spell the same guard with
`||`:

    cdfbet  1527:  if(!(*which < 1 || *which > 4)) goto S30;
    cdfchi  2255:  if(!(*which < 1 || *which > 3)) goto S30;
    cdfchn  2553:  if(!(*which < 1 || *which > 4)) goto S30;
    ... 7 more

so this is a single-character typo rather than an intentional deviation.

The effect is visible from the public API.  cdfbin is declared in the
installed nifticdf.h, and its contract is that an out-of-range input sets
*status to -1 and *bound to the limit that was violated.  Calling it with
which = 9:

    before:  status=999  bound=-999     (both left as the caller set them)
    after:   status=-1   bound=4        (the documented error return)

Before the fix the caller has no indication anything was wrong and the
function proceeds to compute with an unhandled selector.

(cherry picked from commit 5c63a01)
The range check is reachable from the installed nifticdf.h, so assert
the documented contract directly: which=9 and which=0 must set
*status to -1 and *bound to the limit that was violated.  A which=1
call is asserted to still return status 0, so a guard that rejected
everything would not pass.

(cherry picked from commit 493a317)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants