Skip to content

Commit 25d5681

Browse files
committed
Fix potential overflow for 'sqlite3_status' memory metrics
The previous call have been replaced by 'sqlite3_status64' when querying 'SQLITE_STATUS_MEMORY_USED' to avoid potential integer overflows.
1 parent 6f2798a commit 25d5681

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

include/sqlite3db.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern int (*proxy_sqlite3_close_v2)(sqlite3*);
4242
extern int (*proxy_sqlite3_get_autocommit)(sqlite3*);
4343
extern void (*proxy_sqlite3_free)(void*);
4444
extern int (*proxy_sqlite3_status)(int op, int *pCurrent, int *pHighwater, int resetFlag);
45+
extern int (*proxy_sqlite3_status64)(int op, long long *pCurrent, long long *pHighwater, int resetFlag);
4546
extern int (*proxy_sqlite3_changes)(sqlite3*);
4647
extern int (*proxy_sqlite3_step)(sqlite3_stmt*);
4748
extern int (*proxy_sqlite3_config)(int, ...);
@@ -89,6 +90,8 @@ int (*proxy_sqlite3_close_v2)(sqlite3*);
8990
int (*proxy_sqlite3_get_autocommit)(sqlite3*);
9091
void (*proxy_sqlite3_free)(void*);
9192
int (*proxy_sqlite3_status)(int op, int *pCurrent, int *pHighwater, int resetFlag);
93+
int (*proxy_sqlite3_status64)(int op, long long *pCurrent, long long *pHighwater, int resetFlag);
94+
9295
int (*proxy_sqlite3_changes)(sqlite3*);
9396
int (*proxy_sqlite3_step)(sqlite3_stmt*);
9497
int (*proxy_sqlite3_config)(int, ...);

lib/ProxySQL_Admin_Stats.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ void ProxySQL_Admin::p_stats___memory_metrics() {
109109
this->metrics.p_gauge_array[p_admin_gauge::connpool_memory_bytes]->Set(connpool_mem);
110110

111111
// proxysql_sqlite3_memory_bytes metric
112-
int highwater = 0;
113-
int current = 0;
114-
(*proxy_sqlite3_status)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
112+
long long highwater = 0;
113+
long long current = 0;
114+
(*proxy_sqlite3_status64)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
115115
this->metrics.p_gauge_array[p_admin_gauge::sqlite3_memory_bytes]->Set(current);
116116

117117
// proxysql_jemalloc_* memory metrics
@@ -206,8 +206,8 @@ void ProxySQL_Admin::stats___memory_metrics() {
206206
if (!GloMTH) return;
207207
SQLite3_result * resultset = NULL;
208208

209-
int highwater;
210-
int current;
209+
long long highwater = 0;
210+
long long current = 0;
211211
char bu[32];
212212
char *vn=NULL;
213213
char *query=NULL;
@@ -218,9 +218,9 @@ void ProxySQL_Admin::stats___memory_metrics() {
218218
delete resultset;
219219
resultset=NULL;
220220
}
221-
(*proxy_sqlite3_status)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
221+
(*proxy_sqlite3_status64)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
222222
vn=(char *)"SQLite3_memory_bytes";
223-
sprintf(bu,"%d",current);
223+
sprintf(bu,"%lld",current);
224224
query=(char *)malloc(strlen(a)+strlen(vn)+strlen(bu)+16);
225225
sprintf(query,a,vn,bu);
226226
statsdb->execute(query);
@@ -492,6 +492,8 @@ const void sqlite3_global_stats_row_step(
492492
sprintf(buf, "%lu", val);
493493
} else if constexpr (std::is_same_v<T, unsigned long long>) {
494494
sprintf(buf, "%llu", val);
495+
} else if constexpr (std::is_same_v<T, long long>) {
496+
sprintf(buf, "%lld", val);
495497
} else if constexpr (std::is_same_v<T, bool>) {
496498
sprintf(buf, "%s", val ? "true" : "false");
497499
} else {
@@ -547,8 +549,8 @@ void ProxySQL_Admin::stats___mysql_global() {
547549
}
548550

549551
{
550-
int highwater, current = 0;
551-
(*proxy_sqlite3_status)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
552+
long long highwater, current = 0;
553+
(*proxy_sqlite3_status64)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
552554
sqlite3_global_stats_row_step(statsdb, row_stmt, "SQLite3_memory_bytes", current);
553555
}
554556

@@ -652,14 +654,14 @@ void ProxySQL_Admin::stats___pgsql_global() {
652654
resultset = NULL;
653655
}
654656

655-
int highwater;
656-
int current;
657-
(*proxy_sqlite3_status)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
657+
long long highwater = 0;
658+
long long current = 0;
659+
(*proxy_sqlite3_status64)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
658660
char bu[32];
659661
char* vn = NULL;
660662
char* query = NULL;
661663
vn = (char*)"SQLite3_memory_bytes";
662-
sprintf(bu, "%d", current);
664+
sprintf(bu, "%lld", current);
663665
query = (char*)malloc(strlen(a) + strlen(vn) + strlen(bu) + 16);
664666
sprintf(query, a, vn, bu);
665667
statsdb->execute(query);

lib/sqlite3db.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ void SQLite3DB::LoadPlugin(const char *plugin_name) {
10081008
proxy_sqlite3_get_autocommit = NULL;
10091009
proxy_sqlite3_free = NULL;
10101010
proxy_sqlite3_status = NULL;
1011+
proxy_sqlite3_status64 = NULL;
10111012
proxy_sqlite3_changes = NULL;
10121013
proxy_sqlite3_step = NULL;
10131014
proxy_sqlite3_shutdown = NULL;
@@ -1086,6 +1087,7 @@ void SQLite3DB::LoadPlugin(const char *plugin_name) {
10861087
proxy_sqlite3_get_autocommit = sqlite3_get_autocommit;
10871088
proxy_sqlite3_free = sqlite3_free;
10881089
proxy_sqlite3_status = sqlite3_status;
1090+
proxy_sqlite3_status64 = sqlite3_status64;
10891091
proxy_sqlite3_changes = sqlite3_changes;
10901092
proxy_sqlite3_step = sqlite3_step;
10911093
proxy_sqlite3_shutdown = sqlite3_shutdown;
@@ -1114,6 +1116,7 @@ void SQLite3DB::LoadPlugin(const char *plugin_name) {
11141116
assert(proxy_sqlite3_get_autocommit);
11151117
assert(proxy_sqlite3_free);
11161118
assert(proxy_sqlite3_status);
1119+
assert(proxy_sqlite3_status64);
11171120
assert(proxy_sqlite3_changes);
11181121
assert(proxy_sqlite3_step);
11191122
assert(proxy_sqlite3_shutdown);

0 commit comments

Comments
 (0)