Merge pull request #570 from Snapchat/fix_snprint_uses

Fix snprintf uses to avoid hardcoded sizes
This commit is contained in:
Malavan Sotheeswaran 2023-02-14 22:20:29 -05:00 committed by GitHub
commit 5f5eb0e1b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 73 additions and 70 deletions

View File

@ -441,7 +441,7 @@ static int _anetTcpServer(char *err, int port, const char *bindaddr, int af, int
char _port[6]; /* strlen("65535") */ char _port[6]; /* strlen("65535") */
struct addrinfo hints, *servinfo, *p; struct addrinfo hints, *servinfo, *p;
snprintf(_port,6,"%d",port); snprintf(_port,sizeof(_port),"%d",port);
memset(&hints,0,sizeof(hints)); memset(&hints,0,sizeof(hints));
hints.ai_family = af; hints.ai_family = af;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;

View File

@ -1658,7 +1658,7 @@ int rewriteAppendOnlyFile(char *filename) {
{ // BEGIN GOTO SCOPED VARIABLES { // BEGIN GOTO SCOPED VARIABLES
/* Note that we have to use a different temp name here compared to the /* Note that we have to use a different temp name here compared to the
* one used by rewriteAppendOnlyFileBackground() function. */ * one used by rewriteAppendOnlyFileBackground() function. */
snprintf(tmpfile,256,"temp-rewriteaof-%d.aof", (int) getpid()); snprintf(tmpfile,sizeof(tmpfile),"temp-rewriteaof-%d.aof", (int) getpid());
fp = fopen(tmpfile,"w"); fp = fopen(tmpfile,"w");
if (!fp) { if (!fp) {
serverLog(LL_WARNING, "Opening the temp file for AOF rewrite in rewriteAppendOnlyFile(): %s", strerror(errno)); serverLog(LL_WARNING, "Opening the temp file for AOF rewrite in rewriteAppendOnlyFile(): %s", strerror(errno));
@ -1895,7 +1895,7 @@ int rewriteAppendOnlyFileBackground(void) {
/* Child */ /* Child */
redisSetProcTitle("keydb-aof-rewrite"); redisSetProcTitle("keydb-aof-rewrite");
redisSetCpuAffinity(g_pserver->aof_rewrite_cpulist); redisSetCpuAffinity(g_pserver->aof_rewrite_cpulist);
snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid()); snprintf(tmpfile,sizeof(tmpfile),"temp-rewriteaof-bg-%d.aof", (int) getpid());
if (rewriteAppendOnlyFile(tmpfile) == C_OK) { if (rewriteAppendOnlyFile(tmpfile) == C_OK) {
sendChildCowInfo(CHILD_INFO_TYPE_AOF_COW_SIZE, "AOF rewrite"); sendChildCowInfo(CHILD_INFO_TYPE_AOF_COW_SIZE, "AOF rewrite");
exitFromChild(0); exitFromChild(0);
@ -1944,10 +1944,10 @@ void bgrewriteaofCommand(client *c) {
void aofRemoveTempFile(pid_t childpid) { void aofRemoveTempFile(pid_t childpid) {
char tmpfile[256]; char tmpfile[256];
snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) childpid); snprintf(tmpfile,sizeof(tmpfile),"temp-rewriteaof-bg-%d.aof", (int) childpid);
bg_unlink(tmpfile); bg_unlink(tmpfile);
snprintf(tmpfile,256,"temp-rewriteaof-%d.aof", (int) childpid); snprintf(tmpfile,sizeof(tmpfile),"temp-rewriteaof-%d.aof", (int) childpid);
bg_unlink(tmpfile); bg_unlink(tmpfile);
} }
@ -1985,7 +1985,7 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal) {
/* Flush the differences accumulated by the parent to the /* Flush the differences accumulated by the parent to the
* rewritten AOF. */ * rewritten AOF. */
latencyStartMonitor(latency); latencyStartMonitor(latency);
snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", snprintf(tmpfile,sizeof(tmpfile),"temp-rewriteaof-bg-%d.aof",
(int)g_pserver->child_pid); (int)g_pserver->child_pid);
newfd = open(tmpfile,O_WRONLY|O_APPEND); newfd = open(tmpfile,O_WRONLY|O_APPEND);
if (newfd == -1) { if (newfd == -1) {

View File

@ -5107,7 +5107,7 @@ void createDumpPayload(rio *payload, robj_roptr o, robj *key) {
serverAssert(rdbSaveObject(payload,o,key)); serverAssert(rdbSaveObject(payload,o,key));
char szT[32]; char szT[32];
uint64_t mvcc = mvccFromObj(o); uint64_t mvcc = mvccFromObj(o);
snprintf(szT, 32, "%" PRIu64, mvcc); snprintf(szT, sizeof(szT), "%" PRIu64, mvcc);
serverAssert(rdbSaveAuxFieldStrStr(payload,"mvcc-tstamp", szT) != -1); serverAssert(rdbSaveAuxFieldStrStr(payload,"mvcc-tstamp", szT) != -1);
/* Write the footer, this is how it looks like: /* Write the footer, this is how it looks like:

View File

@ -2588,7 +2588,7 @@ static int updateMaxclients(long long val, long long prev, const char **err) {
adjustOpenFilesLimit(); adjustOpenFilesLimit();
if (g_pserver->maxclients != val) { if (g_pserver->maxclients != val) {
static char msg[128]; static char msg[128];
snprintf(msg, 128, "The operating system is not able to handle the specified number of clients, try with %d", g_pserver->maxclients); snprintf(msg, sizeof(msg), "The operating system is not able to handle the specified number of clients, try with %d", g_pserver->maxclients);
*err = msg; *err = msg;
if (g_pserver->maxclients > prev) { if (g_pserver->maxclients > prev) {
g_pserver->maxclients = prev; g_pserver->maxclients = prev;
@ -2627,7 +2627,7 @@ static int updateMaxclients(long long val, long long prev, const char **err) {
if (res != AE_OK){ if (res != AE_OK){
static char msg[128]; static char msg[128];
snprintf(msg, 128,"Failed to post the request to change setsize for Thread %d", iel); snprintf(msg, sizeof(msg),"Failed to post the request to change setsize for Thread %d", iel);
*err = msg; *err = msg;
return 0; return 0;
} }

View File

@ -1577,7 +1577,7 @@ char *stringFromLongLong(long long value) {
int len; int len;
char *s; char *s;
len = snprintf(buf,32,"%lld",value); len = snprintf(buf,sizeof(buf),"%lld",value);
s = zmalloc(len+1); s = zmalloc(len+1);
memcpy(s, buf, len); memcpy(s, buf, len);
s[len] = '\0'; s[len] = '\0';

View File

@ -112,15 +112,15 @@ int endianconvTest(int argc, char *argv[], int accurate) {
UNUSED(argv); UNUSED(argv);
UNUSED(accurate); UNUSED(accurate);
snprintf(buf,32,"ciaoroma"); snprintf(buf,sizeof(buf),"ciaoroma");
memrev16(buf); memrev16(buf);
printf("%s\n", buf); printf("%s\n", buf);
snprintf(buf,32,"ciaoroma"); snprintf(buf,sizeof(buf),"ciaoroma");
memrev32(buf); memrev32(buf);
printf("%s\n", buf); printf("%s\n", buf);
snprintf(buf,32,"ciaoroma"); snprintf(buf,sizeof(buf),"ciaoroma");
memrev64(buf); memrev64(buf);
printf("%s\n", buf); printf("%s\n", buf);

View File

@ -904,7 +904,7 @@ int main(int argc, const char **argv) {
while (self_threads < config.max_threads) { while (self_threads < config.max_threads) {
for (int i = 0; i < config.numclients; i++) { for (int i = 0; i < config.numclients; i++) {
snprintf(command, 63, "SET %d %s\r\n", self_threads * config.numclients + i, set_value); snprintf(command, sizeof(command), "SET %d %s\r\n", self_threads * config.numclients + i, set_value);
createClient(command, strlen(command), NULL,self_threads); createClient(command, strlen(command), NULL,self_threads);
} }

View File

@ -51,8 +51,9 @@ int TimerCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int
for (int j = 0; j < 10; j++) { for (int j = 0; j < 10; j++) {
int delay = rand() % 5000; int delay = rand() % 5000;
char *buf = RedisModule_Alloc(256); int bufsize = 256;
snprintf(buf,256,"After %d", delay); char *buf = RedisModule_Alloc(bufsize);
snprintf(buf,bufsize,"After %d", delay);
RedisModuleTimerID tid = RedisModule_CreateTimer(ctx,delay,timerHandler,buf); RedisModuleTimerID tid = RedisModule_CreateTimer(ctx,delay,timerHandler,buf);
REDISMODULE_NOT_USED(tid); REDISMODULE_NOT_USED(tid);
} }

View File

@ -145,7 +145,7 @@ client *createClient(connection *conn, int iel) {
client_id = g_pserver->next_client_id.fetch_add(1); client_id = g_pserver->next_client_id.fetch_add(1);
c->iel = iel; c->iel = iel;
c->id = client_id; c->id = client_id;
snprintf(c->lock.szName, 56, "client %" PRIu64, client_id); snprintf(c->lock.szName, sizeof(c->lock.szName), "client %" PRIu64, client_id);
c->resp = 2; c->resp = 2;
c->conn = conn; c->conn = conn;
c->name = NULL; c->name = NULL;
@ -777,11 +777,11 @@ void setDeferredAggregateLen(client *c, void *node, long length, char prefix) {
* we return NULL in addReplyDeferredLen() */ * we return NULL in addReplyDeferredLen() */
if (node == NULL) return; if (node == NULL) return;
char lenstr[128]; char lenstr[128];
size_t lenstr_len = snprintf(lenstr, 128, "%c%ld\r\n", prefix, length); size_t lenstr_len = snprintf(lenstr, sizeof(lenstr), "%c%ld\r\n", prefix, length);
setDeferredReply(c, node, lenstr, lenstr_len); setDeferredReply(c, node, lenstr, lenstr_len);
} else { } else {
char lenstr[128]; char lenstr[128];
int lenstr_len = snprintf(lenstr, 128, "%c%ld\r\n", prefix, length); int lenstr_len = snprintf(lenstr, sizeof(lenstr), "%c%ld\r\n", prefix, length);
size_t idxSplice = (size_t)node; size_t idxSplice = (size_t)node;
serverAssert(idxSplice <= c->replyAsync->used); serverAssert(idxSplice <= c->replyAsync->used);

View File

@ -1163,7 +1163,7 @@ int rdbSaveKeyValuePair(rio *rdb, robj_roptr key, robj_roptr val, const expireEn
char szT[32]; char szT[32];
if (g_pserver->fActiveReplica) { if (g_pserver->fActiveReplica) {
snprintf(szT, 32, "%" PRIu64, mvccFromObj(val)); snprintf(szT, sizeof(szT), "%" PRIu64, mvccFromObj(val));
if (rdbSaveAuxFieldStrStr(rdb,"mvcc-tstamp", szT) == -1) return -1; if (rdbSaveAuxFieldStrStr(rdb,"mvcc-tstamp", szT) == -1) return -1;
} }
@ -1190,7 +1190,7 @@ int rdbSaveKeyValuePair(rio *rdb, robj_roptr key, robj_roptr val, const expireEn
{ {
if (itr.subkey() == nullptr) if (itr.subkey() == nullptr)
continue; // already saved continue; // already saved
snprintf(szT, 32, "%lld", itr.when()); snprintf(szT, sizeof(szT), "%lld", itr.when());
rdbSaveAuxFieldStrStr(rdb,"keydb-subexpire-key",itr.subkey()); rdbSaveAuxFieldStrStr(rdb,"keydb-subexpire-key",itr.subkey());
rdbSaveAuxFieldStrStr(rdb,"keydb-subexpire-when",szT); rdbSaveAuxFieldStrStr(rdb,"keydb-subexpire-when",szT);
} }
@ -1490,7 +1490,7 @@ int rdbSaveFile(char *filename, const redisDbPersistentDataSnapshot **rgpdb, rdb
rio rdb; rio rdb;
int error = 0; int error = 0;
snprintf(tmpfile,256,"temp-%d-%d.rdb", getpid(), g_pserver->rdbThreadVars.tmpfileNum); snprintf(tmpfile,sizeof(tmpfile),"temp-%d-%d.rdb", getpid(), g_pserver->rdbThreadVars.tmpfileNum);
fp = fopen(tmpfile,"w"); fp = fopen(tmpfile,"w");
if (!fp) { if (!fp) {
char *cwdp = getcwd(cwd,MAXPATHLEN); char *cwdp = getcwd(cwd,MAXPATHLEN);

View File

@ -3293,8 +3293,8 @@ static redisReply *clusterManagerMigrateKeysInReply(clusterManagerNode *source,
argv_len = zcalloc(argc * sizeof(size_t), MALLOC_LOCAL); argv_len = zcalloc(argc * sizeof(size_t), MALLOC_LOCAL);
char portstr[255]; char portstr[255];
char timeoutstr[255]; char timeoutstr[255];
snprintf(portstr, 10, "%d", target->port); snprintf(portstr, sizeof(portstr), "%d", target->port);
snprintf(timeoutstr, 10, "%d", timeout); snprintf(timeoutstr, sizeof(timeoutstr), "%d", timeout);
argv[0] = "MIGRATE"; argv[0] = "MIGRATE";
argv_len[0] = 7; argv_len[0] = 7;
argv[1] = target->ip; argv[1] = target->ip;
@ -6825,7 +6825,7 @@ static long getLongInfoField(char *info, char *field) {
/* Convert number of bytes into a human readable string of the form: /* Convert number of bytes into a human readable string of the form:
* 100B, 2G, 100M, 4K, and so forth. */ * 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; double d;
if (n < 0) { if (n < 0) {
@ -6835,17 +6835,17 @@ void bytesToHuman(char *s, long long n) {
} }
if (n < 1024) { if (n < 1024) {
/* Bytes */ /* Bytes */
snprintf(s,256,"%lldB",n); snprintf(s,bufsize,"%lldB",n);
return; return;
} else if (n < (1024*1024)) { } else if (n < (1024*1024)) {
d = (double)n/(1024); d = (double)n/(1024);
snprintf(s,256,"%.2fK",d); snprintf(s,bufsize,"%.2fK",d);
} else if (n < (1024LL*1024*1024)) { } else if (n < (1024LL*1024*1024)) {
d = (double)n/(1024*1024); d = (double)n/(1024*1024);
snprintf(s,256,"%.2fM",d); snprintf(s,bufsize,"%.2fM",d);
} else if (n < (1024LL*1024*1024*1024)) { } else if (n < (1024LL*1024*1024*1024)) {
d = (double)n/(1024LL*1024*1024); d = (double)n/(1024LL*1024*1024);
snprintf(s,256,"%.2fG",d); snprintf(s,bufsize,"%.2fG",d);
} }
} }
@ -6875,38 +6875,38 @@ static void statMode(void) {
for (j = 0; j < 20; j++) { for (j = 0; j < 20; j++) {
long k; long k;
snprintf(buf,64,"db%d:keys",j); snprintf(buf,sizeof(buf),"db%d:keys",j);
k = getLongInfoField(reply->str,buf); k = getLongInfoField(reply->str,buf);
if (k == LONG_MIN) continue; if (k == LONG_MIN) continue;
aux += k; aux += k;
} }
snprintf(buf,64,"%ld",aux); snprintf(buf,sizeof(buf),"%ld",aux);
printf("%-11s",buf); printf("%-11s",buf);
/* Used memory */ /* Used memory */
aux = getLongInfoField(reply->str,"used_memory"); aux = getLongInfoField(reply->str,"used_memory");
bytesToHuman(buf,aux); bytesToHuman(buf,aux,sizeof(buf));
printf("%-8s",buf); printf("%-8s",buf);
/* Clients */ /* Clients */
aux = getLongInfoField(reply->str,"connected_clients"); aux = getLongInfoField(reply->str,"connected_clients");
snprintf(buf,64,"%ld",aux); snprintf(buf,sizeof(buf),"%ld",aux);
printf(" %-8s",buf); printf(" %-8s",buf);
/* Blocked (BLPOPPING) Clients */ /* Blocked (BLPOPPING) Clients */
aux = getLongInfoField(reply->str,"blocked_clients"); aux = getLongInfoField(reply->str,"blocked_clients");
snprintf(buf,64,"%ld",aux); snprintf(buf,sizeof(buf),"%ld",aux);
printf("%-8s",buf); printf("%-8s",buf);
/* Requests */ /* Requests */
aux = getLongInfoField(reply->str,"total_commands_processed"); aux = getLongInfoField(reply->str,"total_commands_processed");
snprintf(buf,64,"%ld (+%ld)",aux,requests == 0 ? 0 : aux-requests); snprintf(buf,sizeof(buf),"%ld (+%ld)",aux,requests == 0 ? 0 : aux-requests);
printf("%-19s",buf); printf("%-19s",buf);
requests = aux; requests = aux;
/* Connections */ /* Connections */
aux = getLongInfoField(reply->str,"total_connections_received"); aux = getLongInfoField(reply->str,"total_connections_received");
snprintf(buf,64,"%ld",aux); snprintf(buf,sizeof(buf),"%ld",aux);
printf(" %-12s",buf); printf(" %-12s",buf);
/* Children */ /* Children */

View File

@ -3736,7 +3736,7 @@ retry_connect:
while(maxtries--) { while(maxtries--) {
auto dt = std::chrono::system_clock::now().time_since_epoch(); auto dt = std::chrono::system_clock::now().time_since_epoch();
auto dtMillisecond = std::chrono::duration_cast<std::chrono::milliseconds>(dt); auto dtMillisecond = std::chrono::duration_cast<std::chrono::milliseconds>(dt);
snprintf(tmpfile,256, snprintf(tmpfile,sizeof(tmpfile),
"temp-%d.%ld.rdb",(int)dtMillisecond.count(),(long int)getpid()); "temp-%d.%ld.rdb",(int)dtMillisecond.count(),(long int)getpid());
dfd = open(tmpfile,O_CREAT|O_WRONLY|O_EXCL,0644); dfd = open(tmpfile,O_CREAT|O_WRONLY|O_EXCL,0644);
if (dfd != -1) break; if (dfd != -1) break;

View File

@ -5471,30 +5471,30 @@ NULL
/* Convert an amount of bytes into a human readable string in the form /* Convert an amount of bytes into a human readable string in the form
* of 100B, 2G, 100M, 4K, and so forth. */ * 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; double d;
if (n < 1024) { if (n < 1024) {
/* Bytes */ /* Bytes */
snprintf(s,256,"%lluB",n); snprintf(s,bufsize,"%lluB",n);
} else if (n < (1024*1024)) { } else if (n < (1024*1024)) {
d = (double)n/(1024); d = (double)n/(1024);
snprintf(s,256,"%.2fK",d); snprintf(s,bufsize,"%.2fK",d);
} else if (n < (1024LL*1024*1024)) { } else if (n < (1024LL*1024*1024)) {
d = (double)n/(1024*1024); d = (double)n/(1024*1024);
snprintf(s,256,"%.2fM",d); snprintf(s,bufsize,"%.2fM",d);
} else if (n < (1024LL*1024*1024*1024)) { } else if (n < (1024LL*1024*1024*1024)) {
d = (double)n/(1024LL*1024*1024); d = (double)n/(1024LL*1024*1024);
snprintf(s,256,"%.2fG",d); snprintf(s,bufsize,"%.2fG",d);
} else if (n < (1024LL*1024*1024*1024*1024)) { } else if (n < (1024LL*1024*1024*1024*1024)) {
d = (double)n/(1024LL*1024*1024*1024); d = (double)n/(1024LL*1024*1024*1024);
snprintf(s,256,"%.2fT",d); snprintf(s,bufsize,"%.2fT",d);
} else if (n < (1024LL*1024*1024*1024*1024*1024)) { } else if (n < (1024LL*1024*1024*1024*1024*1024)) {
d = (double)n/(1024LL*1024*1024*1024*1024); d = (double)n/(1024LL*1024*1024*1024*1024);
snprintf(s,256,"%.2fP",d); snprintf(s,bufsize,"%.2fP",d);
} else { } else {
/* Let's hope we never need this */ /* Let's hope we never need this */
snprintf(s,256,"%lluB",n); snprintf(s,bufsize,"%lluB",n);
} }
} }
@ -5670,13 +5670,13 @@ sds genRedisInfoString(const char *section) {
if (zmalloc_used > g_pserver->stat_peak_memory) if (zmalloc_used > g_pserver->stat_peak_memory)
g_pserver->stat_peak_memory = zmalloc_used; g_pserver->stat_peak_memory = zmalloc_used;
bytesToHuman(hmem,zmalloc_used); bytesToHuman(hmem,zmalloc_used,sizeof(hmem));
bytesToHuman(peak_hmem,g_pserver->stat_peak_memory); bytesToHuman(peak_hmem,g_pserver->stat_peak_memory,sizeof(peak_hmem));
bytesToHuman(total_system_hmem,total_system_mem); bytesToHuman(total_system_hmem,total_system_mem,sizeof(total_system_hmem));
bytesToHuman(used_memory_lua_hmem,memory_lua); bytesToHuman(used_memory_lua_hmem,memory_lua,sizeof(used_memory_lua_hmem));
bytesToHuman(used_memory_scripts_hmem,mh->lua_caches); 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); bytesToHuman(used_memory_rss_hmem,g_pserver->cron_malloc_stats.process_rss,sizeof(used_memory_rss_hmem));
bytesToHuman(maxmemory_hmem,g_pserver->maxmemory); bytesToHuman(maxmemory_hmem,g_pserver->maxmemory,sizeof(maxmemory_hmem));
if (sections++) info = sdscat(info,"\r\n"); if (sections++) info = sdscat(info,"\r\n");
info = sdscatprintf(info, info = sdscatprintf(info,
@ -6563,7 +6563,8 @@ void usage(void) {
void redisAsciiArt(void) { void redisAsciiArt(void) {
#include "asciilogo.h" #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; const char *mode;
if (g_pserver->cluster_enabled) mode = "cluster"; if (g_pserver->cluster_enabled) mode = "cluster";
@ -6585,7 +6586,7 @@ void redisAsciiArt(void) {
); );
} else { } else {
sds motd = fetchMOTD(true, cserver.enable_motd); sds motd = fetchMOTD(true, cserver.enable_motd);
snprintf(buf,1024*16,ascii_logo, snprintf(buf,bufsize,ascii_logo,
KEYDB_REAL_VERSION, KEYDB_REAL_VERSION,
redisGitSHA1(), redisGitSHA1(),
strtol(redisGitDirty(),NULL,10) > 0, 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 */ /* If neither the CN nor the SANs match, update the SSL error and return false */
conn->c.last_errno = 0; conn->c.last_errno = 0;
if (conn->ssl_error) zfree(conn->ssl_error); if (conn->ssl_error) zfree(conn->ssl_error);
conn->ssl_error = (char*)zmalloc(512); size_t bufsize = 512;
snprintf(conn->ssl_error, 512, "Client CN (%s) and SANs not found in allowlist.", commonName); conn->ssl_error = (char*)zmalloc(bufsize);
snprintf(conn->ssl_error, bufsize, "Client CN (%s) and SANs not found in allowlist.", commonName);
return false; return false;
} }

View File

@ -1704,17 +1704,17 @@ static unsigned char *createIntList() {
unsigned char *zl = ziplistNew(); unsigned char *zl = ziplistNew();
char buf[32]; char buf[32];
snprintf(buf, 32, "100"); snprintf(buf, sizeof(buf), "100");
zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL); zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
snprintf(buf, 32, "128000"); snprintf(buf, sizeof(buf), "128000");
zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL); zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
snprintf(buf, 32, "-100"); snprintf(buf, sizeof(buf), "-100");
zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD); zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
snprintf(buf, 32, "4294967296"); snprintf(buf, sizeof(buf), "4294967296");
zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD); zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
snprintf(buf, 32, "non integer"); snprintf(buf, sizeof(buf), "non integer");
zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL); zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
snprintf(buf, 32, "much much longer non integer"); snprintf(buf, sizeof(buf), "much much longer non integer");
zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL); zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
return zl; return zl;
} }
@ -2228,7 +2228,7 @@ int ziplistTest(int argc, char **argv, int accurate) {
char buf[32]; char buf[32];
int i,len; int i,len;
for (i = 0; i < 1000; i++) { for (i = 0; i < 1000; i++) {
len = snprintf(buf,32,"%d",i); len = snprintf(buf,sizeof(buf),"%d",i);
zl = ziplistPush(zl,(unsigned char*)buf,len,ZIPLIST_TAIL); zl = ziplistPush(zl,(unsigned char*)buf,len,ZIPLIST_TAIL);
} }
for (i = 0; i < 1000; i++) { for (i = 0; i < 1000; i++) {
@ -2375,13 +2375,13 @@ int ziplistTest(int argc, char **argv, int accurate) {
} else { } else {
switch(rand() % 3) { switch(rand() % 3) {
case 0: case 0:
buflen = snprintf(buf,1024,"%lld",(0LL + rand()) >> 20); buflen = snprintf(buf,sizeof(buf),"%lld",(0LL + rand()) >> 20);
break; break;
case 1: case 1:
buflen = snprintf(buf,1024,"%lld",(0LL + rand())); buflen = snprintf(buf,sizeof(buf),"%lld",(0LL + rand()));
break; break;
case 2: case 2:
buflen = snprintf(buf,1024,"%lld",(0LL + rand()) << 20); buflen = snprintf(buf,sizeof(buf),"%lld",(0LL + rand()) << 20);
break; break;
default: default:
assert(NULL); assert(NULL);
@ -2410,7 +2410,7 @@ int ziplistTest(int argc, char **argv, int accurate) {
assert(ziplistGet(p,&sstr,&slen,&sval)); assert(ziplistGet(p,&sstr,&slen,&sval));
if (sstr == NULL) { if (sstr == NULL) {
buflen = snprintf(buf,1024,"%lld",sval); buflen = snprintf(buf,sizeof(buf),"%lld",sval);
} else { } else {
buflen = slen; buflen = slen;
memcpy(buf,sstr,buflen); memcpy(buf,sstr,buflen);

View File

@ -370,7 +370,7 @@ size_t zmalloc_get_rss(void) {
int fd, count; int fd, count;
char *p, *x; char *p, *x;
snprintf(filename,256,"/proc/%ld/stat",(long) getpid()); snprintf(filename,sizeof(filename),"/proc/%ld/stat",(long) getpid());
if ((fd = open(filename,O_RDONLY)) == -1) return 0; if ((fd = open(filename,O_RDONLY)) == -1) return 0;
if (read(fd,buf,4096) <= 0) { if (read(fd,buf,4096) <= 0) {
close(fd); close(fd);
@ -462,7 +462,7 @@ size_t zmalloc_get_rss(void) {
char filename[256]; char filename[256];
int fd; int fd;
snprintf(filename,256,"/proc/%ld/psinfo",(long) getpid()); snprintf(filename,sizeof(filename),"/proc/%ld/psinfo",(long) getpid());
if ((fd = open(filename,O_RDONLY)) == -1) return 0; if ((fd = open(filename,O_RDONLY)) == -1) return 0;
if (ioctl(fd, PIOCPSINFO, &info) == -1) { if (ioctl(fd, PIOCPSINFO, &info) == -1) {
@ -521,7 +521,7 @@ int jemalloc_purge() {
unsigned narenas = 0; unsigned narenas = 0;
size_t sz = sizeof(unsigned); size_t sz = sizeof(unsigned);
if (!je_mallctl("arenas.narenas", &narenas, &sz, NULL, 0)) { if (!je_mallctl("arenas.narenas", &narenas, &sz, NULL, 0)) {
snprintf(tmp, 32, "arena.%d.purge", narenas); snprintf(tmp, sizeof(tmp), "arena.%d.purge", narenas);
if (!je_mallctl(tmp, NULL, 0, NULL, 0)) if (!je_mallctl(tmp, NULL, 0, NULL, 0))
return 0; return 0;
} }