BUG #19603: Vuln47: distance_taxicab and distance_chebyshev silently return 0 instead of NaN when a cube coordin

Поиск
Список
Период
Сортировка
Искать

BUG #19603: Vuln47: distance_taxicab and distance_chebyshev silently return 0 instead of NaN when a cube coordin

От:
PG Bug reporting form <noreply@postgresql.org>
Дата:
The following bug has been logged on the website:

Bug reference:      19603
Logged by:          Yuelin Wang
Email address:      1217816127@qq.com
PostgreSQL version: 19beta2
Operating system:   Linux (Ubuntu 24.04, x86_64)
Description:        

### Summary

The static helper distance_1D() in contrib/cube/cube.c classifies two
intervals as "left of", "right of", or "intersecting" using direct floating
point comparisons. When a coordinate is NaN, every comparison evaluates to
false, so the interval falls through to the intersecting branch and the
function returns 0.0 instead of NaN. distance_taxicab and distance_chebyshev
call distance_1D per dimension and sum or max the results, so a single NaN
coordinate silently produces a finite, plausible-looking distance instead of
propagating NaN as IEEE 754 arithmetic normally would.

CWE: CWE-1339. Severity: Low.

### PoC

```sql
CREATE EXTENSION cube;
SELECT distance_chebyshev('(nan,nan)'::cube, '(1,1)'::cube);
SELECT distance_chebyshev('(5,5)'::cube, '(1,nan)'::cube);
SELECT distance_taxicab('(nan)'::cube, '(1)'::cube);
```

### Result

Real captured output from the independent verification run:

```
CREATE EXTENSION
 distance_chebyshev
--------------------
                  0
(1 row)

 distance_chebyshev
--------------------
                  4
(1 row)

 distance_taxicab
------------------
                0
(1 row)
```

### Impact

A database user who stores or queries cube values containing NaN coordinates
can get silently wrong distance results (e.g. 0 instead of NaN) from
distance_taxicab and distance_chebyshev, which can corrupt nearest-neighbor
search results, ranking, or KNN-index-backed queries that rely on these
operators.




Re: BUG #19603: Vuln47: distance_taxicab and distance_chebyshev silently return 0 instead of NaN when a cube coordin

От:
Aleksander Alekseev <aleksander@tigerdata.com>
Дата:
Hi Ayush,

> Thanks for the report and repro.
>
> It looks to me like the root is distance_1D(): with a NaN coordinate none of
> its comparisons can be true, so it seems to fall through to the overlapping
> case and return 0.  My first thought was to fix it there and return NaN when
> any endpoint is NaN, which seems enough for taxicab and Euclidean (<->), which
> accumulate with +=.  Chebyshev needs a bit more, since its running max uses
> "d > distance" and NaN > x is false, so I added an explicit NaN check there too.

I noticed the patch on the commitfest and decided to take a look.

All in all it's good but there are several similar bugs that it
doesn't address (and which were not reported above):

```
=# CREATE EXTENSION cube;

=# SELECT '(1,1)'::cube = '(nan,nan)'::cube;
 ?column?
----------
 t

=# SELECT '(nan)'::cube > '(1)'::cube;  -- wrong because 'NaN' :: real
> 1.0 :: real
 ?column?
----------
 f
```

I think it should.

--
Best regards,
Aleksander Alekseev


Re: BUG #19603: Vuln47: distance_taxicab and distance_chebyshev silently return 0 instead of NaN when a cube coordin

От:
Aleksander Alekseev <aleksander@tigerdata.com>
Дата:
Hi,

> All in all it's good but there are several similar bugs that it
> doesn't address (and which were not reported above):
>
> ```
> =# CREATE EXTENSION cube;
>
> =# SELECT '(1,1)'::cube = '(nan,nan)'::cube;
>  ?column?
> ----------
>  t
>
> =# SELECT '(nan)'::cube > '(1)'::cube;  -- wrong because 'NaN' :: real
> > 1.0 :: real
>  ?column?
> ----------
>  f
> ```
>
> I think it should.

I experimented more and discovered that GiST is broken in the same
fashion. As an example:

```
CREATE TABLE cube_nan (c cube);

-- 100-dimensional cubes, so that 100 rows are already enough to build a
-- two-level GiST index; every 7th row carries NaN coordinates
INSERT INTO cube_nan
SELECT CASE WHEN g % 7 = 0
            THEN cube(array_fill('nan'::float8, ARRAY[100]))
            ELSE cube(array_fill(g::float8,     ARRAY[100]))
       END
  FROM generate_series(1, 100) g;

CREATE INDEX ON cube_nan USING gist (c);

SET enable_seqscan = off;

SELECT c <-> cube(array_fill(0::float8, ARRAY[100])) AS dist
FROM cube_nan ORDER BY dist;
```

I believe we should treat this as an originally reported bug and fix it too.

-- 
Best regards,
Aleksander Alekseev


Re: BUG #19603: Vuln47: distance_taxicab and distance_chebyshev silently return 0 instead of NaN when a cube coordin

От:
Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Дата:
Вложения:
Hi,

On Wed, 4 Nov 2026 at 15:06, Aleksander Alekseev
 wrote:
>
> Hi,
>
> > All in all it's good but there are several similar bugs that it
> > doesn't address (and which were not reported above):
> >
> > ```
> > =# CREATE EXTENSION cube;
> >
> > =# SELECT '(1,1)'::cube = '(nan,nan)'::cube;
> >  ?column?
> > ----------
> >  t
> >
> > =# SELECT '(nan)'::cube > '(1)'::cube;  -- wrong because 'NaN' :: real
> > > 1.0 :: real
> >  ?column?
> > ----------
> >  f
> > ```
> >
> > I think it should.

Thanks for checking, I missed those in v1. Attached is v2 in two parts.
The first uses the float8 helpers for comparisons and bound selection.

>
> I experimented more and discovered that GiST is broken in the same
> fashion. As an example:
>
> ```
> CREATE TABLE cube_nan (c cube);
>
> -- 100-dimensional cubes, so that 100 rows are already enough to build a
> -- two-level GiST index; every 7th row carries NaN coordinates
> INSERT INTO cube_nan
> SELECT CASE WHEN g % 7 = 0
>             THEN cube(array_fill('nan'::float8, ARRAY[100]))
>             ELSE cube(array_fill(g::float8,     ARRAY[100]))
>        END
>   FROM generate_series(1, 100) g;
>
> CREATE INDEX ON cube_nan USING gist (c);
>
> SET enable_seqscan = off;
>
> SELECT c <-> cube(array_fill(0::float8, ARRAY[100])) AS dist
> FROM cube_nan ORDER BY dist;
> ```
>
> I believe we should treat this as an originally reported bug and fix it too.

Yes, I've included this in the second patch. AFAICS, NaN isn't a valid
lower bound for an internal key that also covers finite values, so I've
used -Infinity there.

FWIW, replacing the library and restarting doesn't repair existing
indexes with NaNs. REINDEX does, so I think we should mention that in
the release notes if we take these commits in.

Thoughts on v2?

Regards,
Ayush

Re: BUG #19603: Vuln47: distance_taxicab and distance_chebyshev silently return 0 instead of NaN when a cube coordin

От:
Aleksander Alekseev <aleksander@tigerdata.com>
Дата:
Hi Ayush,

Thanks for the quick update.

> Thoughts on v2?

v2 is much better but there is still some room for improvement:

```
=# SELECT '(nan)'::cube AS a,
            cube(ARRAY['nan'::float8], ARRAY['nan'::float8]) AS b;
   a   |      b
-------+-------------
 (NaN) | (NaN),(NaN)
(1 row)

=# SELECT '(nan)'::cube = cube(ARRAY['nan'::float8],
                                 ARRAY['nan'::float8]);
 ?column?
----------
 t   <-- so they are "the same value"

=#  SELECT cube_is_point('(nan)'::cube),
            cube_is_point(cube(ARRAY['nan'::float8],
                               ARRAY['nan'::float8]));
 cube_is_point | cube_is_point
---------------+---------------
 t             | f  <-- but one is a point and one is not

=# SELECT cube_size('(nan)'::cube),
            cube_size(cube(ARRAY['nan'::float8],
                           ARRAY['nan'::float8]));
 cube_size | cube_size
-----------+-----------
         0 |       NaN
```

This can be refactored as distance = float8_max(distance, d):

```
@@ -1377,7 +1375,7 @@ distance_chebyshev(PG_FUNCTION_ARGS)
        {
                d = fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i),
                                                         LL_COORD(b,
i), UR_COORD(b, i)));
-               if (d > distance)
+               if (isnan(d) || d > distance)
                        distance = d;
        }

@@ -1385,7 +1383,7 @@ distance_chebyshev(PG_FUNCTION_ARGS)
        for (i = DIM(b); i < DIM(a); i++)
        {
                d = fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i), 0.0, 0.0));
-               if (d > distance)
+               if (isnan(d) || d > distance)
                        distance = d;
        }
```

The tests still pass if I remove:

```
@@ -1503,6 +1503,11 @@ g_cube_distance(PG_FUNCTION_ARGS)
                                break;
                }
        }
+
+       /* NaN does not bound the subtree's distances from below */
+       if (!GIST_LEAF(entry) && isnan(retval))
+               retval = -get_float8_infinity();
+
        PG_RETURN_FLOAT8(retval);
 }
```

Which means the patch doesn't have a proper test case for this.

--
Best regards,
Aleksander Alekseev


Re: BUG #19603: Vuln47: distance_taxicab and distance_chebyshev silently return 0 instead of NaN when a cube coordin

От:
Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Дата:
Вложения:
Hi,

On Thu, 5 Nov 2026 at 13:47, Aleksander Alekseev
 wrote:
>
> Hi Ayush,
>
> Thanks for the quick update.

Thanks a lot for your review.

> > Thoughts on v2?
>
> v2 is much better but there is still some room for improvement:
>
> ```
> =# SELECT '(nan)'::cube AS a,
>             cube(ARRAY['nan'::float8], ARRAY['nan'::float8]) AS b;
>    a   |      b
> -------+-------------
>  (NaN) | (NaN),(NaN)
> (1 row)
>
> =# SELECT '(nan)'::cube = cube(ARRAY['nan'::float8],
>                                  ARRAY['nan'::float8]);
>  ?column?
> ----------
>  t   <-- so they are "the same value"
>
> =#  SELECT cube_is_point('(nan)'::cube),
>             cube_is_point(cube(ARRAY['nan'::float8],
>                                ARRAY['nan'::float8]));
>  cube_is_point | cube_is_point
> ---------------+---------------
>  t             | f  <-- but one is a point and one is not

Ah, my bad. I took the comment in cubeparse.y as a reason to leave
those checks alone, but they need to agree with the comparisons.
Fixed in v3.

>
> =# SELECT cube_size('(nan)'::cube),
>             cube_size(cube(ARRAY['nan'::float8],
>                            ARRAY['nan'::float8]));
>  cube_size | cube_size
> -----------+-----------
>          0 |       NaN
> ```
>
> This can be refactored as distance = float8_max(distance, d):

Yup, that's simpler. Changed both loops, thanks.

> ```
> @@ -1377,7 +1375,7 @@ distance_chebyshev(PG_FUNCTION_ARGS)
>         {
>                 d = fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i),
>                                                          LL_COORD(b,
> i), UR_COORD(b, i)));
> -               if (d > distance)
> +               if (isnan(d) || d > distance)
>                         distance = d;
>         }
>
> @@ -1385,7 +1383,7 @@ distance_chebyshev(PG_FUNCTION_ARGS)
>         for (i = DIM(b); i < DIM(a); i++)
>         {
>                 d = fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i), 0.0, 0.0));
> -               if (d > distance)
> +               if (isnan(d) || d > distance)
>                         distance = d;
>         }
> ```
>
> The tests still pass if I remove:
>
> ```
> @@ -1503,6 +1503,11 @@ g_cube_distance(PG_FUNCTION_ARGS)
>                                 break;
>                 }
>         }
> +
> +       /* NaN does not bound the subtree's distances from below */
> +       if (!GIST_LEAF(entry) && isnan(retval))
> +               retval = -get_float8_infinity();
> +
>         PG_RETURN_FLOAT8(retval);
>  }
> ```
>
> Which means the patch doesn't have a proper test case for this.

Thanks, I've changed the test in v3.  I just wanted not to overboard
the test suite hence had left a few things out.

v3 attached.

Regards,
Ayush

Re: BUG #19603: Vuln47: distance_taxicab and distance_chebyshev silently return 0 instead of NaN when a cube coordin

От:
Aleksander Alekseev <aleksander@tigerdata.com>
Дата:
Hi Ayush,

> Thanks, I've changed the test in v3.  I just wanted not to overboard
> the test suite hence had left a few things out.
>
> v3 attached.

v3 is a significant improvement over v2. One thing that catches my eye:

```
@@ -1145,7 +1152,7 @@ SELECT cube_is_point('(0,1,2)'::cube);
  t
 (1 row)

-SELECT cube_is_point('(0,1,2),(0,1,2)'::cube);
+SELECT cube_is_point(cube(ARRAY['NaN'::float8], ARRAY['NaN'::float8]));
  cube_is_point
 ---------------
  t
@@ -1375,7 +1382,7 @@ SELECT cube_size('(4,8),(15,16)'::cube);
         88
 (1 row)

-SELECT cube_size('(42,137)'::cube);
+SELECT cube_size(cube_subset('(NaN,0),(NaN,1)'::cube, ARRAY[1]));
  cube_size
 -----------
          0
```

The patch shouldn't remove any existing tests, only add new ones.

IMO two patches should be squashed into one. They are addressing
basically one issue. One patch would be much easier to test and
review. In particular, one shouldn't make sure that 0001 is a correct
patch without 0002 applied.

-- 
Best regards,
Aleksander Alekseev


Re: BUG #19603: Vuln47: distance_taxicab and distance_chebyshev silently return 0 instead of NaN when a cube coordin

От:
Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Дата:
Вложения:
Hi,

On Fri, 6 Nov 2026 at 14:30, Aleksander Alekseev
 wrote:
>
> > Thanks, I've changed the test in v3.  I just wanted not to overboard
> > the test suite hence had left a few things out.
> >
> > v3 attached.
>
> v3 is a significant improvement over v2. One thing that catches my eye:
>
> ```
> @@ -1145,7 +1152,7 @@ SELECT cube_is_point('(0,1,2)'::cube);
>   t
>  (1 row)
>
> -SELECT cube_is_point('(0,1,2),(0,1,2)'::cube);
> +SELECT cube_is_point(cube(ARRAY['NaN'::float8], ARRAY['NaN'::float8]));
>   cube_is_point
>  ---------------
>   t
> @@ -1375,7 +1382,7 @@ SELECT cube_size('(4,8),(15,16)'::cube);
>          88
>  (1 row)
>
> -SELECT cube_size('(42,137)'::cube);
> +SELECT cube_size(cube_subset('(NaN,0),(NaN,1)'::cube, ARRAY[1]));
>   cube_size
>  -----------
>           0
> ```
>
> The patch shouldn't remove any existing tests, only add new ones.

Hmm, I was thinking of making it shorter by removing some older
ones which did have some coverage, but you are right.  Added it back.

> IMO two patches should be squashed into one. They are addressing
> basically one issue. One patch would be much easier to test and
> review. In particular, one shouldn't make sure that 0001 is a correct
> patch without 0002 applied.

Yeah that was for ease of review, incase earlier someone was just
referring to the bug details for review.  Squashed into one now.

v4 attached. Thanks again for the review.

Regards,
Ayush
FAQ