From 25d5681c617df4f961812cf11f45082ebf51fffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jaramago=20Fern=C3=A1ndez?= Date: Tue, 1 Jul 2025 17:21:39 +0200 Subject: [PATCH] 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. --- include/sqlite3db.h | 3 +++ lib/ProxySQL_Admin_Stats.cpp | 28 +++++++++++++++------------- lib/sqlite3db.cpp | 3 +++ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/include/sqlite3db.h b/include/sqlite3db.h index 679aa4e5f..ad9d0107b 100644 --- a/include/sqlite3db.h +++ b/include/sqlite3db.h @@ -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, ...); diff --git a/lib/ProxySQL_Admin_Stats.cpp b/lib/ProxySQL_Admin_Stats.cpp index 08069d3a9..e5a7d750a 100644 --- a/lib/ProxySQL_Admin_Stats.cpp +++ b/lib/ProxySQL_Admin_Stats.cpp @@ -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, ¤t, &highwater, 0); + long long highwater = 0; + long long current = 0; + (*proxy_sqlite3_status64)(SQLITE_STATUS_MEMORY_USED, ¤t, &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, ¤t, &highwater, 0); + (*proxy_sqlite3_status64)(SQLITE_STATUS_MEMORY_USED, ¤t, &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) { sprintf(buf, "%llu", val); + } else if constexpr (std::is_same_v) { + sprintf(buf, "%lld", val); } else if constexpr (std::is_same_v) { 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, ¤t, &highwater, 0); + long long highwater, current = 0; + (*proxy_sqlite3_status64)(SQLITE_STATUS_MEMORY_USED, ¤t, &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, ¤t, &highwater, 0); + long long highwater = 0; + long long current = 0; + (*proxy_sqlite3_status64)(SQLITE_STATUS_MEMORY_USED, ¤t, &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); diff --git a/lib/sqlite3db.cpp b/lib/sqlite3db.cpp index 3b435ac53..3acae23c0 100644 --- a/lib/sqlite3db.cpp +++ b/lib/sqlite3db.cpp @@ -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);