View Ticket
Not logged in
Ticket Hash: 53e451acc7e2f2ae15c99a429a8cbbd5a279ae05
Title: Consistent use of sqlite3_int64 for SQLITE_INTEGER
Status: Fixed Type: Code_Defect
Severity: Critical Priority: Immediate
Subsystem: Resolution: Fixed
Last Modified: 2018-03-28 15:23:47
Version Found In:
User Comments:
mj10777 added on 2018-03-21 05:26:32:

Based on the issue:

    https://www.gaia-gis.it/fossil/virtualpg/tktview/7d4b236572e4302d8275920bbda5c2c4f811db69

I think thoughts should be made about replacing sqlite3_column_int with sqlite3_column_int64, which in the case of Primary-Keys will cause a falsifying of results.

Fundamental Datatypes
    https://www.sqlite.org/c3ref/c_blob.html
Every value in SQLite has one of five fundamental datatypes:
SQLITE_INTEGER
...
64-bit signed integer
..
spatialite.c:check_spatial_index
Defines:
sqlite3_int64 count_geom = 0;
sqlite3_int64 count_rtree = 0;
sqlite3_int64 count_rev = 0;
but uses sqlite3_column_int to retrieve the values.

In most cases, such as columns, srid, tile_size or anything using the result of sqlite3_column_bytes the use of an int32 is no problem.

But for any user data defined as SQLITE_INTEGER a int64 should be assumed (and is done in table_cloner.c) .

spatialite.c:fnct_sp_var_update_value and fnct_sp_var_arg
uses '%d' for an SQLITE_INTEGER, but if '110100014258002' is use as a value to be set,
    fnct_sp_var_get_value would, I assume, return '-1472374958'.

Width and Height of images and feature counts
(se_helpers.c: "&WIDTH=%d&HEIGHT=%d&X=%d&Y=%d&FEATURE_COUNT=%d")
are int64 candidates.
A quick browsing through the code (in libspatialite) showed nothing more that I could see.


sandro added on 2018-03-28 15:23:47:
The real problem is that SQLite has only a generic data-type SQLITE_INTEGER, but Integers are internally stored with a variable precision (1, 2, 3, 4, 6 or 8 bytes) depending on the magnitude of the value. 
So it's never possible to know in advance if an Integer value should be fetched by calling sqlite3_column_int() or sqlite3_column_int64(), it's kind of a blind guess.
Assuming that all Integers are indiscriminately Int64 surely is an overkill and will simply waste a lot of storage; assuming Int32 seems to be much more realistic in the vast majority of cases.
The unique case where applying an Int64 interpretation seems to be the most reasonable option is when handling INTEGER PRIMARY KEY or ROWID values.

Accordingly to all the above premises I've accordingly patched check_spatial_index, fnct_sp_var_update_value and fnct_sp_var_arg.
But I've left untouched se_helpers, because an image width/height expressed by an Int64 value is extremely unrealistic.

fixed on 2018-03-21 by commit e209de7717