Skip to content

Commit 1cfc040

Browse files
dandan
authored andcommitted
Update extension ext/misc/totext.c to avoid both ubsan warnings and dubious real->integer conversions.
FossilOrigin-Name: c626aa108a7a30cef54af8d93ac9e45749568ed38e4e06623a6bad6b4bf6e8ec
1 parent 76da0dc commit 1cfc040

4 files changed

Lines changed: 34 additions & 18 deletions

File tree

ext/misc/totype.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,20 @@ static int totypeAtoF(const char *z, double *pResult, int length){
350350
return z>=zEnd && nDigits>0 && eValid && nonNum==0;
351351
}
352352

353+
/*
354+
** Convert a floating point value to an integer. Or, if this cannot be
355+
** done in a way that avoids 'outside the range of representable values'
356+
** warnings from UBSAN, return 0.
357+
**
358+
** This function is a modified copy of internal SQLite function
359+
** sqlite3RealToI64().
360+
*/
361+
static sqlite3_int64 totypeDoubleToInt(double r){
362+
if( r<-9223372036854774784.0 ) return 0;
363+
if( r>+9223372036854774784.0 ) return 0;
364+
return (sqlite3_int64)r;
365+
}
366+
353367
/*
354368
** tointeger(X): If X is any value (integer, double, blob, or string) that
355369
** can be losslessly converted into an integer, then make the conversion and
@@ -365,7 +379,7 @@ static void tointegerFunc(
365379
switch( sqlite3_value_type(argv[0]) ){
366380
case SQLITE_FLOAT: {
367381
double rVal = sqlite3_value_double(argv[0]);
368-
sqlite3_int64 iVal = (sqlite3_int64)rVal;
382+
sqlite3_int64 iVal = totypeDoubleToInt(rVal);
369383
if( rVal==(double)iVal ){
370384
sqlite3_result_int64(context, iVal);
371385
}
@@ -440,7 +454,7 @@ static void torealFunc(
440454
case SQLITE_INTEGER: {
441455
sqlite3_int64 iVal = sqlite3_value_int64(argv[0]);
442456
double rVal = (double)iVal;
443-
if( iVal==(sqlite3_int64)rVal ){
457+
if( iVal==totypeDoubleToInt(rVal) ){
444458
sqlite3_result_double(context, rVal);
445459
}
446460
break;

manifest

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
C Avoid\serrors\swith\sSQLITE_OMIT_VIRTUALTABLE\sbuilds\sin\sjson106.test\sand\sunionall.test.
2-
D 2024-01-04T17:13:39.373
1+
C Update\sextension\sext/misc/totext.c\sto\savoid\sboth\subsan\swarnings\sand\sdubious\sreal->integer\sconversions.
2+
D 2024-01-05T15:53:58.837
33
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
44
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
55
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -408,7 +408,7 @@ F ext/misc/spellfix.c c0aa7b80d6df45f7da59d912b38752bcac1af53a5766966160e6c5cdd3
408408
F ext/misc/sqlar.c 53e7d48f68d699a24f1a92e68e71eca8b3a9ff991fe9588c2a05bde103c6e7b7
409409
F ext/misc/stmt.c b090086cd6bd6281c21271d38d576eeffe662f0e6b67536352ce32bbaa438321
410410
F ext/misc/templatevtab.c 10f15b165b95423ddef593bc5dcb915ec4eb5e0f1066d585e5435a368b8bc22b
411-
F ext/misc/totype.c fa4aedeb07f66169005dffa8de3b0a2b621779fd44f85c103228a42afa71853b
411+
F ext/misc/totype.c 75ed9827d19cc3b434fc2aeb60725d4d46e1534373615612a4d1cfdcc3d60922
412412
F ext/misc/uint.c 053fed3bce2e89583afcd4bf804d75d659879bbcedac74d0fa9ed548839a030b
413413
F ext/misc/unionvtab.c 716d385256d5fb4beea31b0efede640807e423e85c9784d21d22f0cce010a785
414414
F ext/misc/urifuncs.c f71360d14fa9e7626b563f1f781c6148109462741c5235ac63ae0f8917b9c751
@@ -1218,7 +1218,7 @@ F test/full.test 6b3c8fb43c6beab6b95438c1675374b95fab245d
12181218
F test/func.test 3a29323b640c0552f6e9f1577407ced3a68e7d8c0bc04b61dd6040fa593a3a02
12191219
F test/func2.test 772d66227e4e6684b86053302e2d74a2500e1e0f
12201220
F test/func3.test 600a632c305a88f3946d38f9a51efe145c989b2e13bd2b2a488db47fe76bab6a
1221-
F test/func4.test 2285fb5792d593fef442358763f0fd9de806eda47dbc7a5934df57ffdc484c31
1221+
F test/func4.test e8ef9b2bd6a192a213cbd5cf31a3b35e25cd6ff2fdaeea0b58d63be31b03d220
12221222
F test/func5.test 863e6d1bd0013d09c17236f8a13ea34008dd857d87d85a13a673960e4c25d82a
12231223
F test/func6.test 9cc9b1f43b435af34fe1416eb1e318c8920448ea7a6962f2121972f5215cb9b0
12241224
F test/func7.test adbfc910385a6ffd15dc47be3c619ef070c542fcb7488964badb17b2d9a4d080
@@ -2156,9 +2156,11 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
21562156
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
21572157
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
21582158
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
2159-
P fe952c12903ea2150880c8bb57cda2efc00ce9fa801568a68c619e0745f30567 90e8a233549a2d31e6959ce3fec927693b772ab3c0abce65e81d7350d2ca5cc6
2160-
R 1b0244f5731b1e66c7e1ef8d70f19d5b
2161-
T +closed 90e8a233549a2d31e6959ce3fec927693b772ab3c0abce65e81d7350d2ca5cc6
2162-
U drh
2163-
Z 5d98a22501c46b821d62bf99693fba4a
2159+
P 8940e2a1054fbc19fae3f76e743d744840c3a5aad001be8d3d56ca134226c34b
2160+
R a29b7304699ca19c7db12a157b970ee6
2161+
T *branch * totype-fix
2162+
T *sym-totype-fix *
2163+
T -sym-trunk *
2164+
U dan
2165+
Z 319246ea9e64ae191c32df6aa7328756
21642166
# Remove this line to create a well-formed Fossil manifest.

manifest.uuid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8940e2a1054fbc19fae3f76e743d744840c3a5aad001be8d3d56ca134226c34b
1+
c626aa108a7a30cef54af8d93ac9e45749568ed38e4e06623a6bad6b4bf6e8ec

test/func4.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ do_execsql_test func4-1.22 {
9292
} {{}}
9393
do_execsql_test func4-1.23 {
9494
SELECT tointeger(-9223372036854775808 - 1);
95-
} {-9223372036854775808}
95+
} {{}}
9696
do_execsql_test func4-1.24 {
9797
SELECT tointeger(-9223372036854775808);
9898
} {-9223372036854775808}
@@ -269,15 +269,15 @@ ifcapable floatingpoint {
269269
} {-9.223372036854776e+18}
270270
do_execsql_test func4-2.24 {
271271
SELECT toreal(-9223372036854775808);
272-
} {-9.223372036854776e+18}
272+
} {{}}
273273
if {$highPrecision(2)} {
274274
do_execsql_test func4-2.25 {
275275
SELECT toreal(-9223372036854775808 + 1);
276276
} {{}}
277277
}
278278
do_execsql_test func4-2.26 {
279279
SELECT toreal(-9223372036854775807 - 1);
280-
} {-9.223372036854776e+18}
280+
} {{}}
281281
if {$highPrecision(2)} {
282282
do_execsql_test func4-2.27 {
283283
SELECT toreal(-9223372036854775807);
@@ -461,7 +461,7 @@ ifcapable check {
461461
catchsql {
462462
INSERT INTO t1 (x) VALUES ('-9223372036854775809');
463463
}
464-
} {0 {}}
464+
} {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}}
465465
if {$highPrecision(1)} {
466466
do_test func4-3.19 {
467467
catchsql {
@@ -573,10 +573,10 @@ ifcapable floatingpoint {
573573
} {1}
574574
do_execsql_test func4-5.6 {
575575
SELECT tointeger(toreal(-9223372036854775808 - 1));
576-
} {-9223372036854775808}
576+
} {{}}
577577
do_execsql_test func4-5.7 {
578578
SELECT tointeger(toreal(-9223372036854775808));
579-
} {-9223372036854775808}
579+
} {{}}
580580
if {$highPrecision(2)} {
581581
do_execsql_test func4-5.8 {
582582
SELECT tointeger(toreal(-9223372036854775808 + 1));

0 commit comments

Comments
 (0)