Better validation for NaN in $percentile / $median

XMLWordPrintableJSON

    • Query Integration
    • ALL
    • v9.0, v8.3, v8.2, v8.0, v7.0
    • 0
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Overview

      The $percentile and $median aggregation operators validate the p (percentile) parameter with an ordered range comparison of the form p < 0 || p > 1 in parseP() (src/mongo/db/pipeline/accumulator_percentile.cpp).

      Because IEEE-754 ordered comparisons against NaN are always false, a NaN value for p is not rejected by this check. A non-finite percentile has no well-defined rank, so it should be rejected during input validation.

      Fix

      Use a negated in-range test (!(p >= 0 && p <= 1)) so that NaN is rejected while behavior is unchanged for all finite values (+/-Infinity were already rejected). This is the single validation chokepoint shared by the accumulator, expression, and window-function forms of $percentile/$median.

      As defense-in-depth, the window-function rank-to-index conversion is hardened with representAs<int>() plus a tripwire assertion so a non-finite or out-of-range rank can never reach an undefined-behavior float-to-int conversion.

      Tests

      • A noPassthrough regression test exercising a bounded $setWindowFields window.
      • NaN/+Infinity/-Infinity rejection cases added to the accumulator and expression syntax tests.

            Assignee:
            Charlie Swanson
            Reporter:
            Charlie Swanson
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: