View Ticket
Not logged in
Ticket Hash: 89346e3b692a770ac3f2b9a20e41c19f5aff6a71
Title: Segfault in ST_Length()
Status: Closed Type: Code_Defect
Severity: Critical Priority: Immediate
Subsystem: Resolution: Not_A_Bug
Last Modified: 2015-12-22 11:11:42
Version Found In: 4.3.0a
User Comments:
anonymous added on 2015-12-16 11:51:08:
I some very specific cases, ST_Length(ST_GeomFromWKB()) produces a segfault.
Plus, when it does NOT segfault, it does not produce a result.

I've written a PHP script that I'm attaching below, that reproduces the problem.

First of all, you'll notice that the WKT version returns empty results, instead of returning the length.

Then, the WKB version segfaults.

If you remove the WKT tests, the WKB tests do not segfault, but they do return empty results as well. This bug appears in specific conditions and was hard to track.

--------------------

<?php

// Initialize

$sqlite3 = new SQLite3(':memory:');
$sqlite3->loadExtension('mod_spatialite.so');

// Display versions

$statement = $sqlite3->query('SELECT sqlite_version(), spatialite_version()');
list ($sqliteVersion, $spatialiteVersion) = $statement->fetchArray(SQLITE3_NUM);

echo 'PHP version: ', PHP_VERSION, PHP_EOL;
echo 'SQLite version: ', $sqliteVersion, PHP_EOL;
echo 'SpatiaLite version: ', $spatialiteVersion, PHP_EOL;

// Reproduce the bug

$wkts = [
    'LINESTRING (0 0, 1 1)',
    'CIRCULARSTRING (0 0, 1 1, 2 2)',
    'CIRCULARSTRING (0 0, 1 1, 2 1)',
    'CIRCULARSTRING (0 0, 1 1, 3 1, 4 3, 5 3)',
    'COMPOUNDCURVE ((1 1, 2 1), CIRCULARSTRING (2 1, 1 1, 2 2))',
    'COMPOUNDCURVE (CIRCULARSTRING (0 0, 1 1, 2 1), (2 1, 2 2, 3 2, 3 3))'
];

$wkbs = [
    '01080000000300000000000000000000000000000000000000000000000000f03f000000000000f03f00000000000000400000000000000040',
    '01080000000300000000000000000000000000000000000000000000000000f03f000000000000f03f0000000000000040000000000000f03f',
    '010900000002000000010200000002000000000000000000f03f000000000000f03f0000000000000040000000000000f03f0108000000030000000000000000000040000000000000f03f000000000000f03f000000000000f03f00000000000000400000000000000040',
    '01090000000200000001080000000300000000000000000000000000000000000000000000000000f03f000000000000f03f0000000000000040000000000000f03f0102000000040000000000000000000040000000000000f03f000000000000004000000000000000400000000000000840000000000000004000000000000008400000000000000840'
];

// WKT tests

echo "WKT\n";

$sql = 'SELECT ST_Length(ST_GeomFromText(?, ?))';
$statement = $sqlite3->prepare($sql);

foreach ($wkts as $wkt) {
    $statement->bindValue(1, $wkt, SQLITE3_BLOB);
    $statement->bindValue(2, 0, SQLITE3_INTEGER);
    $result = $statement->execute();
    print_r($result->fetchArray(SQLITE3_NUM));
}

echo "WKB\n";

// WKB tests

$sql = 'SELECT ST_Length(ST_GeomFromWKB(?, ?))';
$statement = $sqlite3->prepare($sql);

foreach ($wkbs as $wkb) {
    $statement->bindValue(1, $wkb, SQLITE3_BLOB);
    $statement->bindValue(2, 0, SQLITE3_INTEGER);
    $result = $statement->execute();
    print_r($result->fetchArray(SQLITE3_NUM));
}

sandro added on 2015-12-16 23:21:32:
Please note: 
both CIRCULARSTRING and COMPOUNDCURVE are not supported. valid Geometry classes supported by SpatiaLite are the following:
POINT
LINESTRING
POLYGON
MULTIPOINT
MULTILINESTRING
MULTIPOLYGON
GEOMETRYCOLLECTION

all WKT expressions containing an unsupported class will obviously return a NULL geometry; and WKB expressions will possibly be badly parsed.

anonymous added on 2015-12-22 11:11:42:
Thanks for this precision.
Anyway, the segfault was a bug in the SQLite3 bindings in PHP 7.0.0, now fixed in 7.0.1.