Add bounds checking and error messages and improved comments

to the (unused) zorder extension function.
[forum:/forumpost/e3f1ede174|Forum post e3f1ede174]

FossilOrigin-Name: 6bb717acf706e6ffd4671660ca78237e6a42863f344518e6d21065bf735f971e
This commit is contained in:
drh committed 2025-08-05 10:54:56 +00:00
1 parent f9c2cff2fe
commit 332eb8e63c
3 files changed
+53 -21

No files matched your search

+46 -14
View File
@@ -16,6 +16,17 @@
**
** unzorder(Z,N,I) Extract the I-th dimension from N-dimensional
** Morton code Z.
**
** Compiling:
**
** (linux) gcc -fPIC -shared zorder.c -o zorder.so
** (mac) clang -fPIC -dynamiclib zorder.c -o zorder.dylib
** (windows) cl zorder.c -link -dll -out:zorder.dll
**
** Usage example:
**
** .load ./zorder
** SELECT zorder(1,2,3,4);
*/
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
@@ -25,43 +36,53 @@ SQLITE_EXTENSION_INIT1
/*
** Functions: zorder(X0,X1,....)
**
** Convert integers X0, X1, ... into morton code.
** Convert integers X0, X1, ... into morton code. There must be at least
** two arguments. There may be no more than 24 arguments.
**
** The output is a signed 64-bit integer. If any argument is too large,
** an error is thrown.
** The output is a signed 64-bit integer. If any argument is too large
** to be successfully encoded into a morton code, an error is raised.
*/
static void zorderFunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
sqlite3_int64 z, x[63];
sqlite3_int64 z, x[24];
int i, j;
z = 0;
if( argc<2 || argc>24 ){
sqlite3_result_error(context,
"zorder() needs between 2 and 24 arguments4", -1);
return;
}
for(i=0; i<argc; i++){
x[i] = sqlite3_value_int64(argv[i]);
}
if( argc>0 ){
for(i=0; i<63; i++){
j = i%argc;
z |= (x[j]&1)<<i;
x[j] >>= 1;
}
for(i=0; i<63; i++){
j = i%argc;
z |= (x[j]&1)<<i;
x[j] >>= 1;
}
sqlite3_result_int64(context, z);
for(i=0; i<argc; i++){
if( x[i] ){
sqlite3_result_error(context, "parameter too large", -1);
char *z = sqlite3_mprintf(
"the %r argument to zorder() (%lld) is too large "
"for a 64-bit %d-dimensional Morton code",
i+1, sqlite3_value_int64(argv[i]), argc);
sqlite3_result_error(context, z, -1);
sqlite3_free(z);
break;
}
}
}
/*
** Functions: unzorder(Z,N,I)
** Function: unzorder(Z,N,K)
**
** Assuming that Z is an N-dimensional Morton code, extract the I-th
** dimension.
** Assuming that Z is an N-dimensional Morton code, extract the K-th
** dimension. K is between 0 and N-1. N must be between 2 and 24.
*/
static void unzorderFunc(
sqlite3_context *context,
@@ -72,7 +93,18 @@ static void unzorderFunc(
int j, k;
z = sqlite3_value_int64(argv[0]);
n = sqlite3_value_int64(argv[1]);
if( n<2 || n>24 ){
sqlite3_result_error(context,
"N argument to unzorder(Z,N,K) should be between 2 and 24",
-1);
return;
}
i = sqlite3_value_int64(argv[2]);
if( i<0 || i>=n ){
sqlite3_result_error(context,
"K argument to unzorder(Z,N,K) should be between 0 and N-1", -1);
return;
}
x = 0;
for(k=0, j=i; j<63; j+=n, k++){
x |= ((z>>j)&1)<<k;
+6 -6
View File
@@ -1,5 +1,5 @@
C Improved\sdefenses\sagainst\scorrupt\sZIP\sarchives\sin\sthe\szipfile\sextension.
D 2025-08-05T01:53:03.565
C Add\sbounds\schecking\sand\serror\smessages\sand\simproved\scomments\nto\sthe\s(unused)\szorder\sextension\sfunction.\n[forum:/forumpost/e3f1ede174|Forum\spost\se3f1ede174]
D 2025-08-05T10:54:56.919
F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
@@ -461,7 +461,7 @@ F ext/misc/vtshim.c e5bce24ab8c532f4fdc600148718fe1802cb6ed57417f1c1032d8961f72b
F ext/misc/wholenumber.c 0fa0c082676b7868bf2fa918e911133f2b349bcdceabd1198bba5f65b4fc0668
F ext/misc/windirent.h 02211ce51f3034c675f2dbf4d228194d51b3ee05734678bad5106fff6292e60c
F ext/misc/zipfile.c 360cc8e0b13398a27abae2baa5d136462718994053ef918e86f4e2dd238657c7
F ext/misc/zorder.c b0ff58fa643afa1d846786d51ea8d5c4b6b35aa0254ab5a82617db92f3adda64
F ext/misc/zorder.c bddff2e1b9661a90c95c2a9a9c7ecd8908afab5763256294dd12d609d4664eee
F ext/rbu/rbu.c 801450b24eaf14440d8fd20385aacc751d5c9d6123398df41b1b5aa804bf4ce8
F ext/rbu/rbu1.test 25870dd7db7eb5597e2b4d6e29e7a7e095abf332660f67d89959552ce8f8f255
F ext/rbu/rbu10.test 7c22caa32c2ff26983ca8320779a31495a6555737684af7aba3daaf762ef3363
@@ -2213,8 +2213,8 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350
F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7
F tool/warnings.sh 1ad0169b022b280bcaaf94a7fa231591be96b514230ab5c98fbf15cd7df842dd
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 4fcdd5bdb061d550b4a35594eb16c9a1699c76caf1d906f1781b4f9cb29ac80c
R 124958b837aa8476fe348da681b7d6d6
P 642e89191deaf75db236102248c662aeef65bcd3dcbdfea694256583556be75f
R bc0ed57214c2790343d9a5ba7eec2712
U drh
Z 9661b63357db3c578107f6e9b8ce7e30
Z e5f5abe0bb299cabb1135d9f96b6d9a9
# Remove this line to create a well-formed Fossil manifest.
+1 -1
View File
@@ -1 +1 @@
642e89191deaf75db236102248c662aeef65bcd3dcbdfea694256583556be75f
6bb717acf706e6ffd4671660ca78237e6a42863f344518e6d21065bf735f971e