commit 85c9b1e50d088402cb608e4d3342cfd80956ccab
Author: Sachin <sachin.setiya@mariadb.com>
Date:   Tue Oct 16 05:07:44 2018 +0530

    MDEV-10362 mysqlbinlog verbose output cannot be decoded
    
    base64_encode does not correctly parse comments inside of base64 string.
    This patch fixes that.

diff --git a/include/base64.h b/include/base64.h
index 9a843b5088e..5779dc1ace2 100644
--- a/include/base64.h
+++ b/include/base64.h
@@ -54,6 +54,8 @@ int base64_decode(const char *src, size_t src_len,
 
 /* Allow multuple chunks 'AAA= AA== AA==', binlog uses this */
 #define MY_BASE64_DECODE_ALLOW_MULTIPLE_CHUNKS 1
+/* Allow Comment starting of line with #*/
+#define MY_BASE64_DECODE_ALLOW_COMMENTS 2
 
 
 #ifdef __cplusplus
diff --git a/mysql-test/suite/binlog/r/binlog_mysqlbinlog_comment_base64.result b/mysql-test/suite/binlog/r/binlog_mysqlbinlog_comment_base64.result
new file mode 100644
index 00000000000..7adf6cf3c7f
--- /dev/null
+++ b/mysql-test/suite/binlog/r/binlog_mysqlbinlog_comment_base64.result
@@ -0,0 +1,20 @@
+create table t1(a int , b int);
+#202 entry so that binlog query is split
+select count(*) from t1;
+count(*)
+202
+FLUSH logs;
+DROP TABLE t1;
+select count(*) from t1;
+count(*)
+202
+DROP TABLE t1;
+#No change in BASE64_FROM behaviour
+SELECT FROM_BASE64('TWFy
+#Comment 344
+#
+aWE=') AS 'Output';
+Output
+NULL
+Warnings:
+Warning	1958	Bad base64 data as position 5
diff --git a/mysql-test/suite/binlog/t/binlog_mysqlbinlog_comment_base64.test b/mysql-test/suite/binlog/t/binlog_mysqlbinlog_comment_base64.test
new file mode 100644
index 00000000000..49db05903bd
--- /dev/null
+++ b/mysql-test/suite/binlog/t/binlog_mysqlbinlog_comment_base64.test
@@ -0,0 +1,32 @@
+--source include/have_binlog_format_row.inc
+
+create table t1(a int , b int);
+
+--echo #202 entry so that binlog query is split
+--let str=insert into t1 values(1,1),
+--let counter=200
+while ($counter)
+{
+  --let str=$str(1,1),
+  --dec $counter
+}
+--let str=$str(1,1)
+--disable_query_log
+--eval $str
+--enable_query_log
+select count(*) from t1;
+FLUSH logs;
+--let $MYSQLD_DATADIR= `SELECT @@datadir`
+--exec $MYSQL_BINLOG -vvv $MYSQLD_DATADIR/master-bin.000001 > $MYSQTEST_VARDIR/tmp/a.binlog
+
+DROP TABLE t1;
+
+--exec $MYSQL < $MYSQTEST_VARDIR/tmp/a.binlog
+select count(*) from t1;
+DROP TABLE t1;
+
+--echo #No change in BASE64_FROM behaviour
+SELECT FROM_BASE64('TWFy
+#Comment 344
+#
+aWE=') AS 'Output';
diff --git a/mysys/base64.c b/mysys/base64.c
index 265b2f22aad..6b9a69afda7 100644
--- a/mysys/base64.c
+++ b/mysys/base64.c
@@ -201,7 +201,22 @@ my_base64_decoder_skip_spaces(MY_BASE64_DECODER *decoder)
     decoder->error= 1; /* Unexpected end-of-input found */
   return TRUE;
 }
-
+/**
+ * Skip all the comments inside of base64 encoding
+ *
+ * @param decode base64 decoding stream
+ *
+ * @return void
+ * */
+static inline void
+my_base64_skip_comments(MY_BASE64_DECODER *decoder)
+{
+  my_base64_decoder_skip_spaces(decoder);
+  const char* src= decoder->src;
+  while(*src == '#')
+    while (*src++ != '\n'){}
+  decoder->src= src;
+}
 
 /**
  * Convert the next character in a base64 encoded stream
@@ -323,11 +338,13 @@ base64_decode(const char *src_base, size_t len,
   decoder.end= src_base + len;
   decoder.error= 0;
   decoder.mark= 0;
-
+  uint i=1;
   for ( ; ; )
   {
     decoder.c= 0;
     decoder.state= 0;
+    if (flags & MY_BASE64_DECODE_ALLOW_COMMENTS)
+      my_base64_skip_comments(&decoder);
 
     if (my_base64_decoder_getch(&decoder) ||
         my_base64_decoder_getch(&decoder) ||
@@ -346,6 +363,7 @@ base64_decode(const char *src_base, size_t len,
         break;
       decoder.mark= 0;
     }
+    i++;
   }
 
   /* Return error if there are more non-space characters */
diff --git a/sql/sql_binlog.cc b/sql/sql_binlog.cc
index f0465cdf5bf..07dac31184f 100644
--- a/sql/sql_binlog.cc
+++ b/sql/sql_binlog.cc
@@ -125,7 +125,8 @@ void mysql_client_binlog_statement(THD* thd)
   {
     char const *endptr= 0;
     int bytes_decoded= base64_decode(strptr, coded_len, buf, &endptr,
-                                     MY_BASE64_DECODE_ALLOW_MULTIPLE_CHUNKS);
+                                     MY_BASE64_DECODE_ALLOW_MULTIPLE_CHUNKS
+                                       | MY_BASE64_DECODE_ALLOW_COMMENTS);
 
 #ifndef HAVE_valgrind
       /*
