SQLite Forum

sqlite3_result_text64 3rd parameter
Login

sqlite3_result_text64 3rd parameter

(1) By carlbauh on 2022-11-07 09:40:17 [source]

Hello,

Document states: "If the 3rd parameter to the sqlite3_result_text* interfaces is negative, then SQLite takes result text from the 2nd parameter through the first zero character. If the 3rd parameter to the sqlite3_result_text* interfaces is non-negative, then as many bytes (not characters) of the text pointed to by the 2nd parameter are taken as the application-defined function result."

But for sqlite3_result_text64 the 3rd parameter is sqlite3_uint64 and it can not get a negative number.

So I want to ask if sqlite3_uint64 is wrong and it need to be sqlite3_int64 or the document need to be more clear?

(2.2) By Stephan Beal (stephan) on 2022-11-07 10:56:47 edited from 2.1 in reply to 1 [link] [source]

So I want to ask if sqlite3_uint64 is wrong and it need to be sqlite3_int64 or the document need to be more clear?

The docs are extracted from the sources and are correct, but yes, this does seem like a design quirk when compared to the rest of the API. You can pass -1 as a uint64 by using a cast, and it will be treated properly in this case. Internally, that int64 gets converted to an integer because blob sizes are limited to sizes <= 2147483647 bytes. A small test app can show that it will behave properly with -1: (see correction below)

$ cat x.c
#include <stdio.h>
#include <stdint.h>

int main(void){
  printf("%d\n", (int)((uint64_t)-1));
  return 0;
}


$ gcc -o x x.c
$ ./x 
-1

When passed a value greater than the max size, it invokes the error handler for that function, setting the result to SQLITE_TOOBIG and cleaning up the string if needed.

Edit: correction: because the size check necessarily happens before the cast, and uint64 -1 is >= 2gb, sqlite3_bind_text64() and sqlite3_result_text64() will not behave as advertised with a (uint64_t)-1 argument. The core devs are aware of the problem and are deciding on a solution.

(3) By Gunter Hick (gunter_hick) on 2022-11-07 10:44:47 in reply to 1 [link] [source]

As you rightly noticed, sqlite3_uint64 is always positive, thus the "count it yourself" feature is only available via the value 0.

Any value greater than 0x7fffffff does not actually set a result, but destroys any previuosly set value and sets the "too big" error.

(4) By Richard Hipp (drh) on 2022-11-07 11:23:32 in reply to 1 [link] [source]

The documentation will be corrected in the next release.

(5) By carlbauh on 2022-11-07 11:47:49 in reply to 4 [link] [source]

Thank you for the answers.

@drh, so for now, should I say that one always needs to set the length of text with sqlite3_result_text64 and there is no auto detection of length for it?

(6) By carlbauh on 2022-11-07 11:49:39 in reply to 2.2 [link] [source]

@stephan thank you.

You said generated from the sources. I can not find the source of docs. Are they publicly available? If yes, for example where is the line about sqlite3_result_text64 we are disguising?

(7) By Richard Hipp (drh) on 2022-11-07 12:36:26 in reply to 6 [link] [source]

(8.1) By Kees Nuyt (knu) on 2022-11-07 16:34:23 edited from 8.0 in reply to 5 [link] [source]

Deleted

(9) By Kees Nuyt (knu) on 2022-11-07 16:31:33 in reply to 7 [link] [source]

Minor typo:

-if the string where NUL terminated.
+if the string were NUL terminated.

https://sqlite.org/src/info/46052b3bcab8d34?&ln=5885-5886