summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorBinbin <binloveplay1314@qq.com>2022-12-09 01:29:30 +0800
committerGitHub <noreply@github.com>2022-12-08 19:29:30 +0200
commitfa5474e15319743908e4cd32bd003272ed018ed3 (patch)
tree280c83dca5eb2d70a9ab5e9b07ef490e532536d3 /src/util.c
parent4a27aa4875250c075ad2860e9ecc88d77ef6091b (diff)
downloadredis-fa5474e15319743908e4cd32bd003272ed018ed3.tar.gz
Normalize NAN to a single nan type, like we do with inf (#11597)
From https://en.wikipedia.org/wiki/NaN#Display, it says that apart from nan and -nan, we can also get NAN and even nan(char-sequence) from libc. In #11482, our conclusion was that we wanna normalize it in Redis to a single nan type, like we already normalized inf. For this, we also reverted the assert_match part of the test added in #11506, using assert_equal to validate the changes.
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index 300d061fc..d094b3b55 100644
--- a/src/util.c
+++ b/src/util.c
@@ -779,6 +779,13 @@ int ld2string(char *buf, size_t len, long double value, ld2string_mode mode) {
memcpy(buf,"-inf",4);
l = 4;
}
+ } else if (isnan(value)) {
+ /* Libc in some systems will format nan in a different way,
+ * like nan, -nan, NAN, nan(char-sequence).
+ * So we normalize it and create a single nan form in an explicit way. */
+ if (len < 4) goto err; /* No room. 4 is "nan\0" */
+ memcpy(buf, "nan", 3);
+ l = 3;
} else {
switch (mode) {
case LD_STR_AUTO:
@@ -1253,6 +1260,17 @@ static void test_ll2string(void) {
assert(!strcmp(buf, "9223372036854775807"));
}
+static void test_ld2string(void) {
+ char buf[32];
+ long double v;
+ int sz;
+
+ v = 0.0 / 0.0;
+ sz = ld2string(buf, sizeof(buf), v, LD_STR_AUTO);
+ assert(sz == 3);
+ assert(!strcmp(buf, "nan"));
+}
+
static void test_fixedpoint_d2string(void) {
char buf[32];
double v;
@@ -1309,6 +1327,7 @@ int utilTest(int argc, char **argv, int flags) {
test_string2ll();
test_string2l();
test_ll2string();
+ test_ld2string();
test_fixedpoint_d2string();
return 0;
}