Tarantool development patches archive
 help / color / mirror / Atom feed
From: Serge Petrenko <sergepetrenko@tarantool.org>
To: vdavydov.dev@gmail.com
Cc: tarantool-patches@freelists.org, kostja@tarantool.org,
	Serge Petrenko <sergepetrenko@tarantool.org>
Subject: [PATCH v2 2/8] decimal: fix encoding numbers with positive exponent.
Date: Thu,  8 Aug 2019 14:55:53 +0300	[thread overview]
Message-ID: <53d58fa5cad01273d3e60fe034c8e74f023d3c0e.1565263272.git.sergepetrenko@tarantool.org> (raw)
In-Reply-To: <cover.1565263272.git.sergepetrenko@tarantool.org>

When a number having a positive exponent is encoded, the internal
decPackedFromNumber function returns a negative scale, which differs
from the scale, returned by decimal_scale(). This leads to errors in
decoding. Account for negative scale in decimal_pack() and
decimal_unpack().

Follow-up #692
---
 src/lib/core/decimal.c   | 10 +++++++---
 test/unit/decimal.c      | 15 +++++++++++++--
 test/unit/decimal.result | 11 ++++++++---
 3 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/src/lib/core/decimal.c b/src/lib/core/decimal.c
index 6ef351f81..840aa5dfe 100644
--- a/src/lib/core/decimal.c
+++ b/src/lib/core/decimal.c
@@ -33,6 +33,7 @@
 #include "third_party/decNumber/decContext.h"
 #include "third_party/decNumber/decPacked.h"
 #include "lib/core/tt_static.h"
+#include "lib/msgpuck/msgpuck.h"
 #include <stddef.h>
 #include <stdlib.h>
 #include <float.h> /* DBL_DIG */
@@ -340,12 +341,15 @@ char *
 decimal_pack(char *data, const decimal_t *dec)
 {
 	uint32_t len = decimal_len(dec);
-	*data++ = decimal_scale(dec);
+	/* reserve space for resulting scale */
+	char *svp = data++;
 	len--;
 	int32_t scale;
 	char *tmp = (char *)decPackedFromNumber((uint8_t *)data, len, &scale, dec);
 	assert(tmp == data);
-	assert(scale == (int32_t)decimal_scale(dec));
+	/* scale may be negative, when exponent is > 0 */
+	assert(scale == (int32_t)decimal_scale(dec) || scale < 0);
+	mp_store_u8(svp, (int8_t)scale);
 	(void)tmp;
 	data += len;
 	return data;
@@ -354,7 +358,7 @@ decimal_pack(char *data, const decimal_t *dec)
 decimal_t *
 decimal_unpack(const char **data, uint32_t len, decimal_t *dec)
 {
-	int32_t scale = *((*data)++);
+	int32_t scale = (int8_t)mp_load_u8(data);
 	len--;
 	decimal_t *res = decPackedToNumber((uint8_t *)*data, len, &scale, dec);
 	if (res)
diff --git a/test/unit/decimal.c b/test/unit/decimal.c
index b587e1f14..b55333ed7 100644
--- a/test/unit/decimal.c
+++ b/test/unit/decimal.c
@@ -91,7 +91,7 @@ char buf[32];
 static int
 test_pack_unpack(void)
 {
-	plan(146);
+	plan(151);
 
 	test_decpack("0");
 	test_decpack("-0");
@@ -112,13 +112,24 @@ test_pack_unpack(void)
 	test_decpack("99999999999999999999999999999999999999");
 	test_decpack("-99999999999999999999999999999999999999");
 
+	/* Check correct encoding of positive exponent numbers. */
+	decimal_t dec, d1;
+	decimal_from_string(&dec, "1e10");
+	uint32_t l1 = decimal_len(&dec);
+	ok(l1 == 2, "decimal_len() is small for positive exponent decimal");
+	char *b1 = decimal_pack(buf, &dec);
+	is(b1, buf + l1, "positive exponent decimal length");
+	const char *b2 = buf;
+	is(decimal_unpack(&b2, l1, &d1), &d1, "decimal_unpack() of a positive exponent decimal");
+	is(b1, b2, "decimal_unpack uses every byte packed by decimal_pack");
+	is(decimal_compare(&dec, &d1), 0, "positive exponent number is packed/unpacked correctly");
+
 	/* Pack an invalid decimal. */
 	char *b = buf;
 	*b++ = 1;
 	*b++ = '\xab';
 	*b++ = '\xcd';
 	const char *bb = buf;
-	decimal_t dec;
 	is(decimal_unpack(&bb, 3, &dec), NULL, "unpack malformed decimal fails");
 	is(bb, buf, "decode malformed decimal preserves buffer position");
 
diff --git a/test/unit/decimal.result b/test/unit/decimal.result
index 1c72cdfab..2dd91af49 100644
--- a/test/unit/decimal.result
+++ b/test/unit/decimal.result
@@ -277,7 +277,7 @@ ok 275 - decimal_from_string(-1)
 ok 276 - decimal_log10(-1) - error on wrong operands.
 ok 277 - decimal_from_string(-10)
 ok 278 - decimal_sqrt(-10) - error on wrong operands.
-    1..146
+    1..151
     ok 1 - decimal_len(0)
     ok 2 - decimal_len(0) == len(decimal_pack(0)
     ok 3 - decimal_unpack(decimal_pack(0))
@@ -422,6 +422,11 @@ ok 278 - decimal_sqrt(-10) - error on wrong operands.
     ok 142 - decimal_unpack(decimal_pack(-99999999999999999999999999999999999999)) scale
     ok 143 - decimal_unpack(decimal_pack(-99999999999999999999999999999999999999)) precision
     ok 144 - str(decimal_unpack(decimal_pack(-99999999999999999999999999999999999999)) == -99999999999999999999999999999999999999
-    ok 145 - unpack malformed decimal fails
-    ok 146 - decode malformed decimal preserves buffer position
+    ok 145 - decimal_len() is small for positive exponent decimal
+    ok 146 - positive exponent decimal length
+    ok 147 - decimal_unpack() of a positive exponent decimal
+    ok 148 - decimal_unpack uses every byte packed by decimal_pack
+    ok 149 - positive exponent number is packed/unpacked correctly
+    ok 150 - unpack malformed decimal fails
+    ok 151 - decode malformed decimal preserves buffer position
 ok 279 - subtests
-- 
2.20.1 (Apple Git-117)

  parent reply	other threads:[~2019-08-08 11:55 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-08 11:55 [PATCH v2 0/8] Decimal indices Serge Petrenko
2019-08-08 11:55 ` [PATCH v2 1/8] lua: fix decimal comparison with nil Serge Petrenko
2019-08-12 21:16   ` Konstantin Osipov
2019-08-14 11:00   ` Vladimir Davydov
2019-08-14 22:17     ` Konstantin Osipov
2019-08-08 11:55 ` Serge Petrenko [this message]
2019-08-12 21:18   ` [PATCH v2 2/8] decimal: fix encoding numbers with positive exponent Konstantin Osipov
2019-08-13  9:00     ` [tarantool-patches] " Serge Petrenko
2019-08-14 22:21       ` Konstantin Osipov
2019-08-14 11:56   ` Vladimir Davydov
2019-08-08 11:55 ` [PATCH v2 3/8] lua/pickle: fix a typo Serge Petrenko
2019-08-12 21:18   ` Konstantin Osipov
2019-08-14 11:12   ` Vladimir Davydov
2019-08-14 11:15     ` Serge Petrenko
2019-08-08 11:55 ` [PATCH v2 4/8] lua: rework luaL_field types to support msgpack extensions Serge Petrenko
2019-08-12 21:23   ` Konstantin Osipov
2019-08-13 13:15     ` [tarantool-patches] " Serge Petrenko
2019-08-14 22:23       ` Konstantin Osipov
2019-08-15  8:27         ` Serge Petrenko
2019-08-16  8:06           ` Konstantin Osipov
2019-08-08 11:55 ` [PATCH v2 5/8] box: rework field_def and tuple_compare to work with mp_field_type instead of mp_type Serge Petrenko
2019-08-12 21:28   ` Konstantin Osipov
2019-08-08 11:55 ` [PATCH v2 6/8] decimal: allow to encode/decode decimals as MsgPack Serge Petrenko
2019-08-12 21:29   ` Konstantin Osipov
2019-08-12 21:34   ` Konstantin Osipov
2019-08-13 14:01     ` Serge Petrenko
2019-08-14 22:25       ` Konstantin Osipov
2019-08-08 11:55 ` [PATCH v2 7/8] decimal: add conversions to (u)int64_t Serge Petrenko
2019-08-12 21:39   ` Konstantin Osipov
2019-08-13 14:18     ` Serge Petrenko
2019-08-14 22:26       ` Konstantin Osipov
2019-08-14 22:29         ` Konstantin Osipov
2019-08-08 11:55 ` [PATCH v2 8/8] decimal: allow to index decimals Serge Petrenko
2019-08-08 13:42   ` Serge Petrenko
2019-08-12 21:41   ` Konstantin Osipov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=53d58fa5cad01273d3e60fe034c8e74f023d3c0e.1565263272.git.sergepetrenko@tarantool.org \
    --to=sergepetrenko@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH v2 2/8] decimal: fix encoding numbers with positive exponent.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox