From 819bcc1f2aedc504e0d5b5d9c4f3b092d3c906d9 Mon Sep 17 00:00:00 2001 From: Malavan Sotheeswaran Date: Fri, 3 Feb 2023 12:04:41 -0800 Subject: [PATCH] don't sizeof on a pointer --- src/redis-cli.c | 12 ++++++------ src/server.cpp | 35 ++++++++++++++++++----------------- src/tls.cpp | 5 +++-- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/redis-cli.c b/src/redis-cli.c index bf12dd1ef..09891e7a0 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -6825,7 +6825,7 @@ static long getLongInfoField(char *info, char *field) { /* Convert number of bytes into a human readable string of the form: * 100B, 2G, 100M, 4K, and so forth. */ -void bytesToHuman(char *s, long long n) { +void bytesToHuman(char *s, long long n, size_t bufsize) { double d; if (n < 0) { @@ -6835,17 +6835,17 @@ void bytesToHuman(char *s, long long n) { } if (n < 1024) { /* Bytes */ - snprintf(s,sizeof(s),"%lldB",n); + snprintf(s,bufsize,"%lldB",n); return; } else if (n < (1024*1024)) { d = (double)n/(1024); - snprintf(s,sizeof(s),"%.2fK",d); + snprintf(s,bufsize,"%.2fK",d); } else if (n < (1024LL*1024*1024)) { d = (double)n/(1024*1024); - snprintf(s,sizeof(s),"%.2fM",d); + snprintf(s,bufsize,"%.2fM",d); } else if (n < (1024LL*1024*1024*1024)) { d = (double)n/(1024LL*1024*1024); - snprintf(s,sizeof(s),"%.2fG",d); + snprintf(s,bufsize,"%.2fG",d); } } @@ -6885,7 +6885,7 @@ static void statMode(void) { /* Used memory */ aux = getLongInfoField(reply->str,"used_memory"); - bytesToHuman(buf,aux); + bytesToHuman(buf,aux,sizeof(buf)); printf("%-8s",buf); /* Clients */ diff --git a/src/server.cpp b/src/server.cpp index 0ccfcad12..6fd7c2f89 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -5471,30 +5471,30 @@ NULL /* Convert an amount of bytes into a human readable string in the form * of 100B, 2G, 100M, 4K, and so forth. */ -void bytesToHuman(char *s, unsigned long long n) { +void bytesToHuman(char *s, unsigned long long n, size_t bufsize) { double d; if (n < 1024) { /* Bytes */ - snprintf(s,sizeof(s),"%lluB",n); + snprintf(s,bufsize,"%lluB",n); } else if (n < (1024*1024)) { d = (double)n/(1024); - snprintf(s,sizeof(s),"%.2fK",d); + snprintf(s,bufsize,"%.2fK",d); } else if (n < (1024LL*1024*1024)) { d = (double)n/(1024*1024); - snprintf(s,sizeof(s),"%.2fM",d); + snprintf(s,bufsize,"%.2fM",d); } else if (n < (1024LL*1024*1024*1024)) { d = (double)n/(1024LL*1024*1024); - snprintf(s,sizeof(s),"%.2fG",d); + snprintf(s,bufsize,"%.2fG",d); } else if (n < (1024LL*1024*1024*1024*1024)) { d = (double)n/(1024LL*1024*1024*1024); - snprintf(s,sizeof(s),"%.2fT",d); + snprintf(s,bufsize,"%.2fT",d); } else if (n < (1024LL*1024*1024*1024*1024*1024)) { d = (double)n/(1024LL*1024*1024*1024*1024); - snprintf(s,sizeof(s),"%.2fP",d); + snprintf(s,bufsize,"%.2fP",d); } else { /* Let's hope we never need this */ - snprintf(s,sizeof(s),"%lluB",n); + snprintf(s,bufsize,"%lluB",n); } } @@ -5670,13 +5670,13 @@ sds genRedisInfoString(const char *section) { if (zmalloc_used > g_pserver->stat_peak_memory) g_pserver->stat_peak_memory = zmalloc_used; - bytesToHuman(hmem,zmalloc_used); - bytesToHuman(peak_hmem,g_pserver->stat_peak_memory); - bytesToHuman(total_system_hmem,total_system_mem); - bytesToHuman(used_memory_lua_hmem,memory_lua); - bytesToHuman(used_memory_scripts_hmem,mh->lua_caches); - bytesToHuman(used_memory_rss_hmem,g_pserver->cron_malloc_stats.process_rss); - bytesToHuman(maxmemory_hmem,g_pserver->maxmemory); + bytesToHuman(hmem,zmalloc_used,sizeof(hmem)); + bytesToHuman(peak_hmem,g_pserver->stat_peak_memory,sizeof(peak_hmem)); + bytesToHuman(total_system_hmem,total_system_mem,sizeof(total_system_hmem)); + bytesToHuman(used_memory_lua_hmem,memory_lua,sizeof(used_memory_lua_hmem)); + bytesToHuman(used_memory_scripts_hmem,mh->lua_caches,sizeof(used_memory_scripts_hmem)); + bytesToHuman(used_memory_rss_hmem,g_pserver->cron_malloc_stats.process_rss,sizeof(used_memory_rss_hmem)); + bytesToHuman(maxmemory_hmem,g_pserver->maxmemory,sizeof(maxmemory_hmem)); if (sections++) info = sdscat(info,"\r\n"); info = sdscatprintf(info, @@ -6563,7 +6563,8 @@ void usage(void) { void redisAsciiArt(void) { #include "asciilogo.h" - char *buf = (char*)zmalloc(1024*16, MALLOC_LOCAL); + size_t bufsize = 1024*16; + char *buf = (char*)zmalloc(bufsize, MALLOC_LOCAL); const char *mode; if (g_pserver->cluster_enabled) mode = "cluster"; @@ -6585,7 +6586,7 @@ void redisAsciiArt(void) { ); } else { sds motd = fetchMOTD(true, cserver.enable_motd); - snprintf(buf,sizeof(buf),ascii_logo, + snprintf(buf,bufsize,ascii_logo, KEYDB_REAL_VERSION, redisGitSHA1(), strtol(redisGitDirty(),NULL,10) > 0, diff --git a/src/tls.cpp b/src/tls.cpp index 4dbc9f26b..d3d549b01 100644 --- a/src/tls.cpp +++ b/src/tls.cpp @@ -616,8 +616,9 @@ bool tlsValidateCertificateName(tls_connection* conn){ /* If neither the CN nor the SANs match, update the SSL error and return false */ conn->c.last_errno = 0; if (conn->ssl_error) zfree(conn->ssl_error); - conn->ssl_error = (char*)zmalloc(512); - snprintf(conn->ssl_error, sizeof(conn->ssl_error), "Client CN (%s) and SANs not found in allowlist.", commonName); + size_t bufsize = 512; + conn->ssl_error = (char*)zmalloc(bufsize); + snprintf(conn->ssl_error, bufsize, "Client CN (%s) and SANs not found in allowlist.", commonName); return false; }