mirror of
https://github.com/sqlite/sqlite.git
synced 2026-10-23 21:49:02 +03:00
fpdecode() to avoid a (harmless) signed integer overflow. [bugs:/info/2026-10-18T03:43:20Z|Bug 2026-10-18T03:43:20Z] FossilOrigin-Name: 72187f18fe8b207e5955f269a5e299e5b18d5b919ae9073b208454c994e9b18d
122 lines
3.9 KiB
Plaintext
122 lines
3.9 KiB
Plaintext
# 2023-07-03
|
|
#
|
|
# The author disclaims copyright to this source code. In place of
|
|
# a legal notice, here is a blessing:
|
|
#
|
|
# May you do good and not evil.
|
|
# May you find forgiveness for yourself and forgive others.
|
|
# May you share freely, never taking more than you give.
|
|
#
|
|
#***********************************************************************
|
|
#
|
|
# This file contains a test that attempts to verify the claim that the
|
|
# floatpoint-to-text conversion routines built into SQLite maintain at
|
|
# least 15 significant digits of accuracy.
|
|
#
|
|
|
|
set testdir [file dirname $argv0]
|
|
source $testdir/tester.tcl
|
|
|
|
|
|
# Unusual rendering cases:
|
|
#
|
|
do_execsql_test fpconv1-1.0 {
|
|
SELECT 1.23 - 2.34;
|
|
} {-1.1099999999999999}
|
|
# ^--- Not -1.11 as you would expect. -1.11 has a different bit pattern
|
|
|
|
do_execsql_test fpconv1-1.1 {
|
|
SELECT 1.23 * 2.34;
|
|
} {2.8781999999999996}
|
|
# ^--- Not 2.8782 as you would expect. 2.8782 has a different bit pattern
|
|
|
|
# Change significant digits to 15 and get a different result.
|
|
sqlite3_db_config db FP_DIGITS 15
|
|
do_execsql_test fpconv1-1.2 {
|
|
SELECT 1.23 - 2.34;
|
|
} {-1.11}
|
|
do_execsql_test fpconv1-1.3 {
|
|
SELECT 1.23 * 2.34;
|
|
} {2.8782}
|
|
sqlite3_db_config db FP_DIGITS 17
|
|
|
|
|
|
if {[catch {load_static_extension db decimal} error]} {
|
|
puts "Skipping decimal tests, hit load error: $error"
|
|
finish_test; return
|
|
}
|
|
|
|
sqlite3_create_function db
|
|
do_execsql_test fpconv1-2.0 {
|
|
WITH RECURSIVE
|
|
/* Number of random floating-point values to try.
|
|
** On a circa 2021 Ryzen 5950X running Mint Linux, and
|
|
** compiled with -O0 -DSQLITE_DEBUG, this test runs at
|
|
** about 150000 cases per second ------------------vvvvvvv */
|
|
c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<500_000),
|
|
fp(y) AS MATERIALIZED (
|
|
SELECT CAST( format('%+d.%019d0e%+03d',
|
|
random()%10,abs(random()),random()%200) AS real)
|
|
FROM c
|
|
)
|
|
SELECT y FROM fp
|
|
WHERE -log10(abs(decimal_sub(dtostr(y,24),format('%!.24e',y))/y))<17.0;
|
|
/* Number of digits of accuracy required -------^^^^ */
|
|
} {}
|
|
# ^---- Expect a empty set as the result. The output is all tested numbers
|
|
# that fail to preserve at least 16 significant digits of accuracy.
|
|
|
|
########################################################################
|
|
# Random test to ensure that double -> text -> double conversions
|
|
# round-trip exactly.
|
|
#
|
|
|
|
load_static_extension db ieee754
|
|
|
|
do_execsql_test fpconv1-3.0 {
|
|
WITH RECURSIVE
|
|
c(x,s) AS MATERIALIZED (VALUES(1,random()&0xffefffffffffffff)
|
|
UNION ALL
|
|
SELECT x+1,random()&0xffefffffffffffff
|
|
FROM c WHERE x<1_000_000),
|
|
fp(y,s) AS (
|
|
SELECT ieee754_from_int(s),s FROM c
|
|
),
|
|
fp2(yt,y,s) AS (
|
|
SELECT y||'', y, s FROM fp
|
|
)
|
|
SELECT format('%#016x',s) aS 'orig-hex',
|
|
format('%#016x',ieee754_to_int(CAST(yt AS real))) AS 'full-roundtrip',
|
|
yt AS 'rendered-as',
|
|
decimal_exp(yt,30) AS 'text-decimal',
|
|
decimal_exp(ieee754_from_int(s),30) AS 'float-decimal'
|
|
FROM fp2
|
|
WHERE ieee754_to_int(CAST(yt AS real))<>s;
|
|
} {}
|
|
# ^---- Values that fail to round-trip will be reported
|
|
|
|
# 2026-10-18 https://sqlite.org/forum/forumpost/98c37a671c
|
|
# Signed integer overflow in sqlite3FpDecode() when the debugging-only
|
|
# fpdecode() SQL function is given a very negative precision.
|
|
#
|
|
ifcapable debug {
|
|
do_execsql_test fpconv1-4.1 {
|
|
SELECT fpdecode(1.5, -2147483648, 10);
|
|
} {+15/1}
|
|
do_execsql_test fpconv1-4.2 {
|
|
SELECT fpdecode(1.5e300, -2147483600, 10),
|
|
fpdecode(1.5e300, -100000000, 10);
|
|
} {+15/301 +15/301}
|
|
do_execsql_test fpconv1-4.3 {
|
|
SELECT fpdecode(0.1, -2147483648, 16),
|
|
fpdecode(0.1, -2147483647, 16),
|
|
fpdecode(0.1, -100000001, 16),
|
|
fpdecode(0.1, -1000, 16);
|
|
} {+1/0 +1/0 +1/0 +1/0}
|
|
do_execsql_test fpconv1-4.4 {
|
|
SELECT fpdecode(-1.0e-300, -2147483648, 5);
|
|
} {-1/-299}
|
|
}
|
|
|
|
finish_test
|