Skip to content

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

Merged
hjmjohnson merged 2 commits into
masterfrom
pr/fix-cdfbin-range-check
Nov 9, 2026
Merged

hjmjohnson merged 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.

@hjmjohnson
hjmjohnson marked this pull request as ready for review September 24, 2026 12:17
gdevenyi and others added 2 commits October 6, 2026 09:34
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)
@hjmjohnson
hjmjohnson force-pushed the pr/fix-cdfbin-range-check branch from d80a071 to ec0a461 Compare October 6, 2026 14:34

@seanm seanm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boy LLMs sure like verbose commit messages. :) And "this is a single-character typo" isn't even correct, it's a 2 character typo. :)

But anyway seems correct, since indeed all the other cases use ||.

@hjmjohnson
hjmjohnson merged commit 81728c0 into master Nov 9, 2026
4 checks passed
@hjmjohnson
hjmjohnson deleted the pr/fix-cdfbin-range-check branch November 9, 2026 14:24
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