From f7e6eea28bd4f73315d312f857dc21748171b395 Mon Sep 17 00:00:00 2001 From: Enrico Giordani Date: Fri, 1 Jul 2016 15:50:28 +0200 Subject: [PATCH] [Portability] strtol and strtoul fixes. --- deps/hiredis/async.c | 2 +- src/Win32_Interop/Win32_APIs.h | 5 +++++ src/Win32_Interop/win32_types_hiredis.h | 6 ------ src/aof.c | 6 +++--- src/config.c | 24 ++++++++++++------------ src/object.c | 2 +- src/rdb.c | 2 +- src/redis-check-aof.c | 2 +- src/redis-check-rdb.c | 2 +- src/redis-cli.c | 10 +++++----- src/replication.c | 4 ++-- src/sentinel.c | 2 +- src/server.c | 6 +++--- src/util.c | 4 ++-- src/zmalloc.c | 4 ++-- 15 files changed, 40 insertions(+), 41 deletions(-) diff --git a/deps/hiredis/async.c b/deps/hiredis/async.c index 27c9d6cabca..0a6d367ea42 100644 --- a/deps/hiredis/async.c +++ b/deps/hiredis/async.c @@ -648,7 +648,7 @@ static char *nextArgument(char *start, char **str, size_t *len) { if (p == NULL) return NULL; } - *len = (int)PORT_STRTOL(p+1,NULL,10); + *len = (int)strtol(p+1,NULL,10); p = strchr(p,'\r'); assert(p); *str = p+2; diff --git a/src/Win32_Interop/Win32_APIs.h b/src/Win32_Interop/Win32_APIs.h index 9012df438cf..08232d8d5f2 100644 --- a/src/Win32_Interop/Win32_APIs.h +++ b/src/Win32_Interop/Win32_APIs.h @@ -34,6 +34,11 @@ #define strcasecmp _stricmp #define strtoll _strtoi64 +#ifdef _WIN64 +#define strtol _strtoi64 +#define strtoul _strtoui64 +#endif + #define sleep(x) Sleep((x)*1000) /* Redis calls usleep(1) to give thread some time. * Sleep(0) should do the same on Windows. diff --git a/src/Win32_Interop/win32_types_hiredis.h b/src/Win32_Interop/win32_types_hiredis.h index 3690b2f66a6..2c83a3a9bb5 100644 --- a/src/Win32_Interop/win32_types_hiredis.h +++ b/src/Win32_Interop/win32_types_hiredis.h @@ -70,10 +70,4 @@ typedef int pid_t; typedef unsigned __int32 u_int32_t; #endif -#ifdef _WIN64 -#define PORT_STRTOL strtoll -#else -#define PORT_STRTOL strtol -#endif - #endif diff --git a/src/aof.c b/src/aof.c index ff574cb8b70..a9ed7095f1a 100644 --- a/src/aof.c +++ b/src/aof.c @@ -498,9 +498,9 @@ sds catAppendOnlyExpireAtCommand(sds buf, struct redisCommand *cmd, robj *key, r PORT_LONGLONG when; robj *argv[3]; - /* Make sure we can use PORT_STRTOL */ + /* Make sure we can use strtol */ seconds = getDecodedObject(seconds); - when = PORT_STRTOL(seconds->ptr,NULL,10); + when = strtol(seconds->ptr,NULL,10); /* Convert argument into milliseconds for EXPIRE, SETEX, EXPIREAT */ if (cmd->proc == expireCommand || cmd->proc == setexCommand || cmd->proc == expireatCommand) @@ -688,7 +688,7 @@ int loadAppendOnlyFile(char *filename) { goto readerr; } if (buf[0] != '$') goto fmterr; - len = PORT_STRTOL(buf+1,NULL,10); + len = strtol(buf+1,NULL,10); argsds = sdsnewlen(NULL,len); if (len && fread(argsds,len,1,fp) == 0) { sdsfree(argsds); diff --git a/src/config.c b/src/config.c index 2db9163a728..2268a1637f2 100644 --- a/src/config.c +++ b/src/config.c @@ -231,7 +231,7 @@ void loadServerConfigFromString(char *config) { server.unixsocket = zstrdup(argv[1]); } else if (!strcasecmp(argv[0],"unixsocketperm") && argc == 2) { errno = 0; - server.unixsocketperm = (mode_t)PORT_STRTOL(argv[1], NULL, 8); + server.unixsocketperm = (mode_t)strtol(argv[1], NULL, 8); if (errno || server.unixsocketperm > 0777) { err = "Invalid socket file permissions"; goto loaderr; } @@ -549,7 +549,7 @@ void loadServerConfigFromString(char *config) { err = "argument must be 'yes' or 'no'"; goto loaderr; } } else if (!strcasecmp(argv[0],"cluster-node-timeout") && argc == 2) { - server.cluster_node_timeout = PORT_STRTOL(argv[1],NULL,10); + server.cluster_node_timeout = strtol(argv[1],NULL,10); if (server.cluster_node_timeout <= 0) { err = "cluster node timeout must be 1 or greater"; goto loaderr; } @@ -570,21 +570,21 @@ void loadServerConfigFromString(char *config) { goto loaderr; } } else if (!strcasecmp(argv[0],"lua-time-limit") && argc == 2) { - server.lua_time_limit = PORT_STRTOL(argv[1],NULL,10); + server.lua_time_limit = strtol(argv[1],NULL,10); } else if (!strcasecmp(argv[0],"slowlog-log-slower-than") && argc == 2) { - server.slowlog_log_slower_than = PORT_STRTOL(argv[1],NULL,10); + server.slowlog_log_slower_than = strtol(argv[1],NULL,10); } else if (!strcasecmp(argv[0],"latency-monitor-threshold") && argc == 2) { - server.latency_monitor_threshold = PORT_STRTOL(argv[1],NULL,10); + server.latency_monitor_threshold = strtol(argv[1],NULL,10); if (server.latency_monitor_threshold < 0) { err = "The latency threshold can't be negative"; goto loaderr; } } else if (!strcasecmp(argv[0],"slowlog-max-len") && argc == 2) { - server.slowlog_max_len = (PORT_ULONG)(PORT_STRTOL(argv[1],NULL,10)); WIN_PORT_FIX /* cast (PORT_ULONG) */ + server.slowlog_max_len = (PORT_ULONG)(strtol(argv[1],NULL,10)); WIN_PORT_FIX /* cast (PORT_ULONG) */ } else if (!strcasecmp(argv[0],"client-output-buffer-limit") && argc == 5) { @@ -871,7 +871,7 @@ void configSetCommand(client *c) { char *eptr; PORT_LONG val; - val = PORT_STRTOL(v[j], &eptr, 10); + val = strtol(v[j], &eptr, 10); if (eptr[0] != '\0' || ((j & 1) == 0 && val < 1) || ((j & 1) == 1 && val < 0)) { @@ -885,8 +885,8 @@ void configSetCommand(client *c) { time_t seconds; int changes; - seconds = PORT_STRTOL(v[j],NULL,10); - changes = PORT_STRTOL(v[j+1],NULL,10); + seconds = strtol(v[j],NULL,10); + changes = strtol(v[j+1],NULL,10); appendServerSaveParams(seconds, changes); } sdsfreesplitres(v,vlen); @@ -931,9 +931,9 @@ void configSetCommand(client *c) { int soft_seconds; class = getClientTypeByName(v[j]); - hard = PORT_STRTOL(v[j+1],NULL,10); - soft = PORT_STRTOL(v[j+2],NULL,10); - soft_seconds = PORT_STRTOL(v[j+3],NULL,10); + hard = strtol(v[j+1],NULL,10); + soft = strtol(v[j+2],NULL,10); + soft_seconds = strtol(v[j+3],NULL,10); server.client_obuf_limits[class].hard_limit_bytes = hard; server.client_obuf_limits[class].soft_limit_bytes = soft; diff --git a/src/object.c b/src/object.c index 9d5a6f444de..75e58c6dd3d 100644 --- a/src/object.c +++ b/src/object.c @@ -629,7 +629,7 @@ int getLongLongFromObject(robj *o, PORT_LONGLONG *target) { serverAssertWithInfo(NULL,o,o->type == OBJ_STRING); if (sdsEncodedObject(o)) { errno = 0; - value = PORT_STRTOL(o->ptr, &eptr, 10); + value = strtol(o->ptr, &eptr, 10); if (isspace(((char*)o->ptr)[0]) || eptr[0] != '\0' || errno == ERANGE) return C_ERR; diff --git a/src/rdb.c b/src/rdb.c index 68ce4a57917..ba33f347921 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -235,7 +235,7 @@ int rdbTryIntegerEncoding(char *s, size_t len, unsigned char *enc) { char *endptr, buf[32]; /* Check if it's possible to encode this value as a number */ - value = PORT_STRTOL(s, &endptr, 10); + value = strtol(s, &endptr, 10); if (endptr[0] != '\0') return 0; ll2string(buf,32,value); diff --git a/src/redis-check-aof.c b/src/redis-check-aof.c index ac8907c4105..8a9145b6d86 100644 --- a/src/redis-check-aof.c +++ b/src/redis-check-aof.c @@ -75,7 +75,7 @@ int readLong(FILE *fp, char prefix, PORT_LONG *target) { ERROR("Expected prefix '%c', got: '%c'",buf[0],prefix); return 0; } - *target = PORT_STRTOL(buf+1,&eptr,10); + *target = strtol(buf+1,&eptr,10); return consumeNewline(eptr); } diff --git a/src/redis-check-rdb.c b/src/redis-check-rdb.c index 653f5cccc3f..2124b8c9f59 100644 --- a/src/redis-check-rdb.c +++ b/src/redis-check-rdb.c @@ -159,7 +159,7 @@ int processHeader(void) { ERROR("Wrong signature in header"); } - dump_version = (int)PORT_STRTOL(buf + 5, NULL, 10); + dump_version = (int)strtol(buf + 5, NULL, 10); if (dump_version < 1 || dump_version > 6) { ERROR("Unknown RDB format version: %d", dump_version); } diff --git a/src/redis-cli.c b/src/redis-cli.c index 0f40d58a604..8b844c4c471 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -250,9 +250,9 @@ static sds cliVersion(void) { version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION); /* Add git commit and working tree status when available */ - if (PORT_STRTOL(redisGitSHA1(),NULL,16)) { + if (strtol(redisGitSHA1(),NULL,16)) { version = sdscatprintf(version, " (git:%s", redisGitSHA1()); - if (PORT_STRTOL(redisGitDirty(),NULL,10)) + if (strtol(redisGitDirty(),NULL,10)) version = sdscatprintf(version, "-dirty"); version = sdscat(version, ")"); } @@ -1021,7 +1021,7 @@ static int parseOptions(int argc, char **argv) { } else if (!strcmp(argv[i],"-s") && !lastarg) { config.hostsocket = argv[++i]; } else if (!strcmp(argv[i],"-r") && !lastarg) { - config.repeat = (PORT_LONG) PORT_STRTOL(argv[++i],NULL,10); + config.repeat = (PORT_LONG) strtol(argv[++i],NULL,10); } else if (!strcmp(argv[i],"-i") && !lastarg) { double seconds = atof(argv[++i]); config.interval = (PORT_LONG) (seconds*1000000); @@ -1047,7 +1047,7 @@ static int parseOptions(int argc, char **argv) { config.latency_history = 1; } else if (!strcmp(argv[i],"--lru-test") && !lastarg) { config.lru_test_mode = 1; - config.lru_test_sample_size = PORT_STRTOL(argv[++i],NULL,10); + config.lru_test_sample_size = strtol(argv[++i],NULL,10); } else if (!strcmp(argv[i],"--slave")) { config.slave_mode = 1; } else if (!strcmp(argv[i],"--stat")) { @@ -2265,7 +2265,7 @@ static PORT_LONG getLongInfoField(char *info, char *field) { PORT_LONG l; if (!value) return PORT_LONG_MIN; - l = PORT_STRTOL(value,NULL,10); + l = strtol(value,NULL,10); zfree(value); return l; } diff --git a/src/replication.c b/src/replication.c index f515930aa00..5489d5cca0b 100644 --- a/src/replication.c +++ b/src/replication.c @@ -1119,7 +1119,7 @@ void readSyncBulkPayload(aeEventLoop *el, int fd, void *privdata, int mask) { "MASTER <-> SLAVE sync: receiving streamed RDB from master"); } else { usemark = 0; - server.repl_transfer_size = PORT_STRTOL(buf+1,NULL,10); + server.repl_transfer_size = strtol(buf+1,NULL,10); serverLog(LL_NOTICE, "MASTER <-> SLAVE sync: receiving %lld bytes from master", (PORT_LONGLONG) server.repl_transfer_size); @@ -1443,7 +1443,7 @@ int slaveTryPartialResynchronization(int fd, int read_reply) { } else { memcpy(server.repl_master_runid, runid, offset-runid-1); server.repl_master_runid[CONFIG_RUN_ID_SIZE] = '\0'; - server.repl_master_initial_offset = PORT_STRTOL(offset,NULL,10); + server.repl_master_initial_offset = strtol(offset,NULL,10); serverLog(LL_NOTICE,"Full resync from master: %s:%lld", server.repl_master_runid, server.repl_master_initial_offset); diff --git a/src/sentinel.c b/src/sentinel.c index 1403f851e23..ea43279f1f9 100644 --- a/src/sentinel.c +++ b/src/sentinel.c @@ -2205,7 +2205,7 @@ void sentinelRefreshInstanceInfo(sentinelRedisInstance *ri, const char *info) { if (sdslen(l) >= 32 && !memcmp(l,"master_link_down_since_seconds",30)) { - ri->master_link_down_time = PORT_STRTOL(l+31,NULL,10)*1000; + ri->master_link_down_time = strtol(l+31,NULL,10)*1000; } /* role: */ diff --git a/src/server.c b/src/server.c index 3148119b7f6..cdf228e7fc5 100644 --- a/src/server.c +++ b/src/server.c @@ -2927,7 +2927,7 @@ sds genRedisInfoString(char *section) { "config_file:%s\r\n", REDIS_VERSION, redisGitSHA1(), - PORT_STRTOL(redisGitDirty(),NULL,10) > 0, + strtol(redisGitDirty(),NULL,10) > 0, (PORT_ULONGLONG) redisBuildId(), mode, #ifdef _WIN32 @@ -3755,7 +3755,7 @@ void redisAsciiArt(void) { "Redis %s (%s/%d) %s bit, %s mode, port %d, pid %ld ready to start.", REDIS_VERSION, redisGitSHA1(), - PORT_STRTOL(redisGitDirty(),NULL,10) > 0, + strtol(redisGitDirty(),NULL,10) > 0, (sizeof(PORT_LONG) == 8) ? "64" : "32", mode, server.port, (PORT_LONG) getpid() @@ -3764,7 +3764,7 @@ void redisAsciiArt(void) { snprintf(buf,1024*16,ascii_logo, REDIS_VERSION, redisGitSHA1(), - PORT_STRTOL(redisGitDirty(),NULL,10) > 0, + strtol(redisGitDirty(),NULL,10) > 0, (sizeof(PORT_LONG) == 8) ? "64" : "32", mode, server.port, (PORT_LONG) getpid() diff --git a/src/util.c b/src/util.c index 62c4e710217..46e15895118 100644 --- a/src/util.c +++ b/src/util.c @@ -218,7 +218,7 @@ PORT_LONGLONG memtoll(const char *p, int *err) { return 0; } - /* Copy the digits into a buffer, we'll use PORT_STRTOL() to convert + /* Copy the digits into a buffer, we'll use strtol() to convert * the digit (without the unit) into a number. */ digits = (unsigned int)(u-p); WIN_PORT_FIX /* cast (unsigned int) */ if (digits >= sizeof(buf)) { @@ -230,7 +230,7 @@ PORT_LONGLONG memtoll(const char *p, int *err) { char *endptr; errno = 0; - val = PORT_STRTOL(buf,&endptr,10); + val = strtol(buf,&endptr,10); if ((val == 0 && errno == EINVAL) || *endptr != '\0') { if (err) *err = 1; return 0; diff --git a/src/zmalloc.c b/src/zmalloc.c index 9069d15c0ae..3329ad9306e 100644 --- a/src/zmalloc.c +++ b/src/zmalloc.c @@ -319,7 +319,7 @@ size_t zmalloc_get_rss(void) { if (!x) return 0; *x = '\0'; - rss = PORT_STRTOL(p,NULL,10); + rss = strtol(p,NULL,10); rss *= page; return rss; } @@ -378,7 +378,7 @@ size_t zmalloc_get_smap_bytes_by_field(char *field) { char *p = strchr(line,'k'); if (p) { *p = '\0'; - bytes += PORT_STRTOL(line+flen,NULL,10) * 1024; + bytes += strtol(line+flen,NULL,10) * 1024; } } }