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.
pull/5006/head
Javier Jaramago Fernández 10 months ago
parent 6f2798abe0
commit 25d5681c61

@ -42,6 +42,7 @@ extern int (*proxy_sqlite3_close_v2)(sqlite3*);
extern int (*proxy_sqlite3_get_autocommit)(sqlite3*);
extern void (*proxy_sqlite3_free)(void*);
extern int (*proxy_sqlite3_status)(int op, int *pCurrent, int *pHighwater, int resetFlag);
extern int (*proxy_sqlite3_status64)(int op, long long *pCurrent, long long *pHighwater, int resetFlag);
extern int (*proxy_sqlite3_changes)(sqlite3*);
extern int (*proxy_sqlite3_step)(sqlite3_stmt*);
extern int (*proxy_sqlite3_config)(int, ...);
@ -89,6 +90,8 @@ int (*proxy_sqlite3_close_v2)(sqlite3*);
int (*proxy_sqlite3_get_autocommit)(sqlite3*);
void (*proxy_sqlite3_free)(void*);
int (*proxy_sqlite3_status)(int op, int *pCurrent, int *pHighwater, int resetFlag);
int (*proxy_sqlite3_status64)(int op, long long *pCurrent, long long *pHighwater, int resetFlag);
int (*proxy_sqlite3_changes)(sqlite3*);
int (*proxy_sqlite3_step)(sqlite3_stmt*);
int (*proxy_sqlite3_config)(int, ...);

@ -109,9 +109,9 @@ void ProxySQL_Admin::p_stats___memory_metrics() {
this->metrics.p_gauge_array[p_admin_gauge::connpool_memory_bytes]->Set(connpool_mem);
// proxysql_sqlite3_memory_bytes metric
int highwater = 0;
int current = 0;
(*proxy_sqlite3_status)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
long long highwater = 0;
long long current = 0;
(*proxy_sqlite3_status64)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
this->metrics.p_gauge_array[p_admin_gauge::sqlite3_memory_bytes]->Set(current);
// proxysql_jemalloc_* memory metrics
@ -206,8 +206,8 @@ void ProxySQL_Admin::stats___memory_metrics() {
if (!GloMTH) return;
SQLite3_result * resultset = NULL;
int highwater;
int current;
long long highwater = 0;
long long current = 0;
char bu[32];
char *vn=NULL;
char *query=NULL;
@ -218,9 +218,9 @@ void ProxySQL_Admin::stats___memory_metrics() {
delete resultset;
resultset=NULL;
}
(*proxy_sqlite3_status)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
(*proxy_sqlite3_status64)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
vn=(char *)"SQLite3_memory_bytes";
sprintf(bu,"%d",current);
sprintf(bu,"%lld",current);
query=(char *)malloc(strlen(a)+strlen(vn)+strlen(bu)+16);
sprintf(query,a,vn,bu);
statsdb->execute(query);
@ -492,6 +492,8 @@ const void sqlite3_global_stats_row_step(
sprintf(buf, "%lu", val);
} else if constexpr (std::is_same_v<T, unsigned long long>) {
sprintf(buf, "%llu", val);
} else if constexpr (std::is_same_v<T, long long>) {
sprintf(buf, "%lld", val);
} else if constexpr (std::is_same_v<T, bool>) {
sprintf(buf, "%s", val ? "true" : "false");
} else {
@ -547,8 +549,8 @@ void ProxySQL_Admin::stats___mysql_global() {
}
{
int highwater, current = 0;
(*proxy_sqlite3_status)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
long long highwater, current = 0;
(*proxy_sqlite3_status64)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
sqlite3_global_stats_row_step(statsdb, row_stmt, "SQLite3_memory_bytes", current);
}
@ -652,14 +654,14 @@ void ProxySQL_Admin::stats___pgsql_global() {
resultset = NULL;
}
int highwater;
int current;
(*proxy_sqlite3_status)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
long long highwater = 0;
long long current = 0;
(*proxy_sqlite3_status64)(SQLITE_STATUS_MEMORY_USED, &current, &highwater, 0);
char bu[32];
char* vn = NULL;
char* query = NULL;
vn = (char*)"SQLite3_memory_bytes";
sprintf(bu, "%d", current);
sprintf(bu, "%lld", current);
query = (char*)malloc(strlen(a) + strlen(vn) + strlen(bu) + 16);
sprintf(query, a, vn, bu);
statsdb->execute(query);

@ -1008,6 +1008,7 @@ void SQLite3DB::LoadPlugin(const char *plugin_name) {
proxy_sqlite3_get_autocommit = NULL;
proxy_sqlite3_free = NULL;
proxy_sqlite3_status = NULL;
proxy_sqlite3_status64 = NULL;
proxy_sqlite3_changes = NULL;
proxy_sqlite3_step = NULL;
proxy_sqlite3_shutdown = NULL;
@ -1086,6 +1087,7 @@ void SQLite3DB::LoadPlugin(const char *plugin_name) {
proxy_sqlite3_get_autocommit = sqlite3_get_autocommit;
proxy_sqlite3_free = sqlite3_free;
proxy_sqlite3_status = sqlite3_status;
proxy_sqlite3_status64 = sqlite3_status64;
proxy_sqlite3_changes = sqlite3_changes;
proxy_sqlite3_step = sqlite3_step;
proxy_sqlite3_shutdown = sqlite3_shutdown;
@ -1114,6 +1116,7 @@ void SQLite3DB::LoadPlugin(const char *plugin_name) {
assert(proxy_sqlite3_get_autocommit);
assert(proxy_sqlite3_free);
assert(proxy_sqlite3_status);
assert(proxy_sqlite3_status64);
assert(proxy_sqlite3_changes);
assert(proxy_sqlite3_step);
assert(proxy_sqlite3_shutdown);

Loading…
Cancel
Save