don't sizeof on a pointer

This commit is contained in:
Malavan Sotheeswaran 2023-02-03 12:04:41 -08:00
parent 7fcbfac103
commit 819bcc1f2a
3 changed files with 27 additions and 25 deletions

View File

@ -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 */

View File

@ -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,

View File

@ -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;
}