db: allow non-integer argument for safeincr

This commit is contained in:
CismonX 2025-01-25 21:25:37 +08:00
parent 35d4a93a41
commit 3b45900157
No known key found for this signature in database
GPG key ID: 3094873E29A482FB
2 changed files with 11 additions and 11 deletions

View file

@ -98,16 +98,16 @@ safeincr (
debug_assert(argc == 1);
sqlite3_value *val = argv[0];
if (unlikely(SQLITE_INTEGER != sqlite3_value_type(val))) {
sqlite3_result_error(dbctx, "value is not an integer", -1);
return;
int64_t ival = 0;
if (SQLITE_INTEGER == sqlite3_value_type(val)) {
ival = sqlite3_value_int64(val);
if (unlikely(ival == INT64_MAX)) {
sqlite3_result_error(dbctx, "integer overflow", -1);
return;
}
++ival;
}
int64_t ival = sqlite3_value_int64(val);
if (unlikely(ival == INT64_MAX)) {
sqlite3_result_error(dbctx, "integer overflow", -1);
return;
}
sqlite3_result_int64(dbctx, ival + 1);
sqlite3_result_int64(dbctx, ival);
}
int

View file

@ -155,8 +155,8 @@ db_query_i64_cb (
* SQLite converts the result to REAL upon integer overflow.
* See: <https://www.sqlite.org/datatype3.html#operators>.
*
* The `safeincr()` function fails if given a non-integer
* argument, or if the argument value equals to INT64_MAX.
* The `safeincr()` function returns 0 if given a non-integer
* argument, and fails if the argument value equals to INT64_MAX.
*/
int
db_register_safeincr (