Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: Sergey Nikiforov <void@tarantool.org>,
	tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH v2 2/2] base64: Improve decoder performance
Date: Thu, 17 Dec 2020 00:22:08 +0100	[thread overview]
Message-ID: <33050dea-c4b8-44cf-02fb-67928d5f8a93@tarantool.org> (raw)
In-Reply-To: <f41878133643cd7ad19985071f775d95d553f08a.1608041781.git.void@tarantool.org>

Hi! Thanks for the patch!

On 15.12.2020 15:22, Sergey Nikiforov wrote:
> Unnecessary checks were removed from internal loops.
> Benchmark shows that performance is now ~1.15 times higher
> (release build, Intel Core I7-9700K, only one thread).
> ---
> 
> Branch: https://github.com/tarantool/tarantool/tree/void234/gh-3069-fix-base64-memory-overrun-v2
> 
>  third_party/base64.c | 36 +++++++++++++++++++++++++-----------
>  1 file changed, 25 insertions(+), 11 deletions(-)
> 
> diff --git a/third_party/base64.c b/third_party/base64.c
> index 3350a98ff..f4fbbf477 100644
> --- a/third_party/base64.c
> +++ b/third_party/base64.c
> @@ -257,7 +257,7 @@ base64_decode_block(const char *in_base64, int in_len,
>  		{
>  	case step_a:
>  			do {
> -				if (in_pos == in_end || out_pos >= out_end)
> +				if (in_pos >= in_end)

This test now crashes:

====================
--- a/test/unit/base64.c
+++ b/test/unit/base64.c
@@ -7,7 +7,7 @@ static void
 base64_test(const char *str, int options, const char *no_symbols,
 	    int no_symbols_len)
 {
-	plan(3 + no_symbols_len);
+	plan(4 + no_symbols_len);
 
 	int len = strlen(str);
 	int base64_buflen = base64_bufsize(len + 1, options);
@@ -34,6 +34,11 @@ base64_test(const char *str, int options, const char *no_symbols,
 	free(base64_buf);
 	free(strbuf);
 
+	const char *in = "sIIpHw==";
+	int in_len = strlen(in);
+	rc = base64_decode(in, in_len, NULL, 0);
+	is(rc, 0, "no space in out buffer");
+
 	check_plan();
 }
====================

It didn't crash while the checks were in place. I would suggest to
add this test to the previous commit. Because technically it is
also 'buffer overrun'. And it will crash 3069 bug even without ASAN.

For this patch you may want to add a zero length in the beginning.
Also we can keep the number of 'if's unchanged like this:

====================
--- a/third_party/base64.c
+++ b/third_party/base64.c
@@ -256,6 +256,12 @@ base64_decode_block(const char *in_base64, int in_len,
 		while (1)
 		{
 	case step_a:
+			if (out_pos >= out_end)
+			{
+				state->step = step_a;
+				state->result = curr_byte;
+				return out_pos - out_bin;
+			}
 			do {
 				if (in_pos >= in_end)
 				{
@@ -316,12 +322,7 @@ base64_decode_block(const char *in_base64, int in_len,
 			} while (fragment < 0);
 			curr_byte |= (fragment & 0x03f);
 			*out_pos = curr_byte;
-			if (++out_pos >= out_end)
-			{
-				state->step = step_a;
-				state->result = curr_byte;
-				return out_pos - out_bin;
-			}
+			++out_pos;
 		}
 	}
 	/* control should not reach here */
====================

Will that work?

  reply	other threads:[~2020-12-16 23:22 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-15 14:22 [Tarantool-patches] [PATCH v2 0/2] base64: Fix decoder, improve its performance Sergey Nikiforov
2020-12-15 14:22 ` [Tarantool-patches] [PATCH v2 1/2] base64: Fix decoder output buffer overrun (reads) Sergey Nikiforov
2020-12-15 14:22 ` [Tarantool-patches] [PATCH v2 2/2] base64: Improve decoder performance Sergey Nikiforov
2020-12-16 23:22   ` Vladislav Shpilevoy [this message]
2020-12-17 12:43     ` Sergey Nikiforov
2020-12-20 16:27       ` Vladislav Shpilevoy
2020-12-22 10:30         ` Sergey Nikiforov
2020-12-22 15:05           ` Vladislav Shpilevoy
2020-12-22 16:08             ` Sergey Nikiforov
2020-12-22 16:39               ` Vladislav Shpilevoy
2020-12-25 10:39                 ` Sergey Nikiforov
2020-12-26 13:25                   ` Vladislav Shpilevoy

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=33050dea-c4b8-44cf-02fb-67928d5f8a93@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=void@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v2 2/2] base64: Improve decoder performance' \
    /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