Opened 6 years ago

Closed 6 years ago

#4093 closed defect (fixed)

Inconsistent results from qsort callback

Reported by: yugr Owned by: pramsey
Priority: medium Milestone: PostGIS 2.4.5
Component: postgis Version: 2.4.x
Keywords: Cc:

Description

Hi,

qsort callbacks struct_cmp_by_measure and cmpfunc may return invalid result when arguments are swapped. Such bugs may causes inconsistent order or even crashes in some qsort implementations (https://bugzilla.samba.org/show_bug.cgi?id=3959).

The issue has been detected when running standard testsuite under SortChecker (https://github.com/yugr/sortcheck):

lt-cu_tester[425]: qsort: comparison function is not symmetric (comparison function 0x7f1913a75ef0 (/build/postgis-2.2.1+dfsg/liblwgeom/.libs/liblwgeom-2.2.so.5.0.0+0x10ef0), called from 0x7f1913a77456 (/build/postgis-2.2.1+dfsg/liblwgeom/.libs/liblwgeom-2.2.so.5.0.0+0x12456), cmdline is "/build/postgis-2.2.1+dfsg/liblwgeom/cunit/.libs/lt-cu_tester")

The fix is to replace

return ( ia→themeasure>ib→themeasure ) ? 1 : -1;

with

return ( ia→themeasure>ib→themeasure ) ? 1 : ( ia→themeasure<ib→themeasure ) ? -1 : 0;

and

return (v1>v2 ) ? 1 : -1;

with

return (v1>v2 ) ? 1 : (v1<v2) ? -1 : 0;

Change History (4)

comment:1 by pramsey, 6 years ago

In 16583:

Inconsistent results from qsort callback (references #4093)

comment:2 by pramsey, 6 years ago

In 16584:

Inconsistent results from qsort callback (yugr)
References #4093

comment:3 by pramsey, 6 years ago

In 16585:

Inconsistent results from qsort callback (yugr)
References #4093

comment:4 by pramsey, 6 years ago

Resolution: fixed
Status: newclosed

In 16586:

Inconsistent results from qsort callback (yugr)
Closes #4093

Note: See TracTickets for help on using tickets.