From dcf6b68d08acfbfdc3183b0a13f041af51573eb1 Mon Sep 17 00:00:00 2001
From: Georgi Kodinov <Georgi.Kodinov@Oracle.com>
Date: Fri, 25 Mar 2011 12:57:27 +0200
Subject: [PATCH] Bug #11766769: 59959: SMALL VALUES OF --MAX-ALLOWED-PACKET
 ARE NOT BEING HONORED

max_allowed_packet works in conjunction with net_buffer_length.
max_allowed_packet is an upper bound of net_buffer_length.
So it doesn't make sense to set the upper limit lower than the value.
Added a warning (using ER_UNKNOWN_ERRROR and a specific message)
when this is done (in the log at startup and when setting either
max_allowed_packet or the net_buffer_length variables)
Added a test case.
Fixed several tests that broke the above rule.
---
 mysql-test/r/packet.result                    |  1 +
 mysql-test/r/variables.result                 | 27 +++++++++++++
 mysql-test/suite/rpl/r/rpl_packet.result      |  2 +
 .../suite/rpl/t/rpl_loaddata_map-master.opt   |  2 +-
 .../suite/rpl/t/rpl_loaddata_map-slave.opt    |  2 +-
 mysql-test/t/variables.test                   | 26 +++++++++++++
 sql/mysqld.cc                                 |  8 ++++
 sql/set_var.cc                                | 38 ++++++++++++++++++-
 8 files changed, 102 insertions(+), 4 deletions(-)

diff --git a/mysql-test/r/packet.result b/mysql-test/r/packet.result
index ecbb47d4ee0..d673ab42691 100644
--- a/mysql-test/r/packet.result
+++ b/mysql-test/r/packet.result
@@ -3,6 +3,7 @@ set @net_buffer_length=@@global.net_buffer_length;
 set global max_allowed_packet=100;
 Warnings:
 Warning	1292	Truncated incorrect max_allowed_packet value: '100'
+Warning	1105	The value of 'max_allowed_packet' should be no less than the value of 'net_buffer_length'
 set global net_buffer_length=100;
 Warnings:
 Warning	1292	Truncated incorrect net_buffer_length value: '100'
diff --git a/mysql-test/r/variables.result b/mysql-test/r/variables.result
index f4e2a8c08fc..af3b76b09f3 100644
--- a/mysql-test/r/variables.result
+++ b/mysql-test/r/variables.result
@@ -280,6 +280,7 @@ NET_BUFFER_LENGTH	1024
 set global net_buffer_length=2000000000;
 Warnings:
 Warning	1292	Truncated incorrect net_buffer_length value: '2000000000'
+Warning	1105	The value of 'max_allowed_packet' should be no less than the value of 'net_buffer_length'
 show global variables like 'net_buffer_length';
 Variable_name	Value
 net_buffer_length	1048576
@@ -502,6 +503,7 @@ set low_priority_updates=1;
 set global max_allowed_packet=100;
 Warnings:
 Warning	1292	Truncated incorrect max_allowed_packet value: '100'
+Warning	1105	The value of 'max_allowed_packet' should be no less than the value of 'net_buffer_length'
 set global max_binlog_cache_size=100;
 Warnings:
 Warning	1292	Truncated incorrect max_binlog_cache_size value: '100'
@@ -1059,6 +1061,8 @@ set global max_write_lock_count      =default;
 set global myisam_data_pointer_size  =@my_myisam_data_pointer_size;
 set global myisam_max_sort_file_size =@my_myisam_max_sort_file_size;
 set global net_buffer_length         =@my_net_buffer_length;
+Warnings:
+Warning	1105	The value of 'max_allowed_packet' should be no less than the value of 'net_buffer_length'
 set global net_write_timeout         =@my_net_write_timeout;
 set global net_read_timeout          =@my_net_read_timeout;
 set global query_cache_limit         =@my_query_cache_limit;
@@ -1547,4 +1551,27 @@ SET @@global.max_binlog_cache_size=DEFAULT;
 SET @@global.max_join_size=DEFAULT;
 SET @@global.key_buffer_size=@kbs;
 SET @@global.key_cache_block_size=@kcbs;
+#
+# Bug #11766769 : 59959: SMALL VALUES OF --MAX-ALLOWED-PACKET 
+#   ARE NOT BEING HONORED
+#
+CREATE TABLE t1 (a MEDIUMTEXT);
+SET GLOBAL max_allowed_packet=2048;
+Warnings:
+Warning	1105	The value of 'max_allowed_packet' should be no less than the value of 'net_buffer_length'
+SET GLOBAL net_buffer_length=4096;
+Warnings:
+Warning	1105	The value of 'max_allowed_packet' should be no less than the value of 'net_buffer_length'
+SHOW SESSION VARIABLES LIKE 'max_allowed_packet';
+Variable_name	Value
+max_allowed_packet	2048
+SHOW SESSION VARIABLES LIKE 'net_buffer_length';
+Variable_name	Value
+net_buffer_length	4096
+ERROR 08S01: Got a packet bigger than 'max_allowed_packet' bytes
+SELECT LENGTH(a) FROM t1;
+LENGTH(a)
+SET GLOBAL max_allowed_packet=default;
+SET GLOBAL net_buffer_length=default;
+DROP TABLE t1;
 End of 5.1 tests
diff --git a/mysql-test/suite/rpl/r/rpl_packet.result b/mysql-test/suite/rpl/r/rpl_packet.result
index 9239a718504..7a7f8141ac8 100644
--- a/mysql-test/suite/rpl/r/rpl_packet.result
+++ b/mysql-test/suite/rpl/r/rpl_packet.result
@@ -49,6 +49,8 @@ SET @max_allowed_packet_2= @@session.max_allowed_packet;
 ==== clean up ====
 DROP TABLE t1;
 SET @@global.max_allowed_packet= 1024;
+Warnings:
+Warning	1105	The value of 'max_allowed_packet' should be no less than the value of 'net_buffer_length'
 SET @@global.net_buffer_length= 1024;
 DROP TABLE t1;
 RESET SLAVE;
diff --git a/mysql-test/suite/rpl/t/rpl_loaddata_map-master.opt b/mysql-test/suite/rpl/t/rpl_loaddata_map-master.opt
index 831680eb5ef..5fdeb855110 100644
--- a/mysql-test/suite/rpl/t/rpl_loaddata_map-master.opt
+++ b/mysql-test/suite/rpl/t/rpl_loaddata_map-master.opt
@@ -1 +1 @@
---read_buffer_size=12K --max_allowed_packet=8K
+--read_buffer_size=12K --max_allowed_packet=8K --net-buffer-length=8K
diff --git a/mysql-test/suite/rpl/t/rpl_loaddata_map-slave.opt b/mysql-test/suite/rpl/t/rpl_loaddata_map-slave.opt
index 95f55bcf7d8..7d404fae240 100644
--- a/mysql-test/suite/rpl/t/rpl_loaddata_map-slave.opt
+++ b/mysql-test/suite/rpl/t/rpl_loaddata_map-slave.opt
@@ -1 +1 @@
---max_allowed_packet=8K
+--max_allowed_packet=8K --net-buffer-length=8K
diff --git a/mysql-test/t/variables.test b/mysql-test/t/variables.test
index c61e2aa3708..383bdfc79a9 100644
--- a/mysql-test/t/variables.test
+++ b/mysql-test/t/variables.test
@@ -1304,4 +1304,30 @@ SET @@global.max_join_size=DEFAULT;
 SET @@global.key_buffer_size=@kbs;
 SET @@global.key_cache_block_size=@kcbs;
 
+
+--echo #
+--echo # Bug #11766769 : 59959: SMALL VALUES OF --MAX-ALLOWED-PACKET 
+--echo #   ARE NOT BEING HONORED
+--echo #
+
+CREATE TABLE t1 (a MEDIUMTEXT);
+
+SET GLOBAL max_allowed_packet=2048;
+SET GLOBAL net_buffer_length=4096;
+CONNECT (con1,localhost,root,,test);
+SHOW SESSION VARIABLES LIKE 'max_allowed_packet';
+SHOW SESSION VARIABLES LIKE 'net_buffer_length';
+--disable_query_log
+--error ER_NET_PACKET_TOO_LARGE
+INSERT INTO t1 VALUES ('123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890');
+--enable_query_log
+SELECT LENGTH(a) FROM t1;
+
+CONNECTION default;
+DISCONNECT con1;
+SET GLOBAL max_allowed_packet=default;
+SET GLOBAL net_buffer_length=default;
+DROP TABLE t1;
+
+
 --echo End of 5.1 tests
diff --git a/sql/mysqld.cc b/sql/mysqld.cc
index 46376a08ec9..54850f36d10 100644
--- a/sql/mysqld.cc
+++ b/sql/mysqld.cc
@@ -8795,6 +8795,14 @@ static int get_options(int *argc,char **argv)
        opt_log_slow_slave_statements) &&
       !opt_slow_log)
     sql_print_warning("options --log-slow-admin-statements, --log-queries-not-using-indexes and --log-slow-slave-statements have no effect if --log_slow_queries is not set");
+  if (global_system_variables.net_buffer_length > 
+      global_system_variables.max_allowed_packet)
+  {
+    sql_print_warning("net_buffer_length (%lu) is set to be larger "
+                      "than max_allowed_packet (%lu). Please rectify.",
+                      global_system_variables.net_buffer_length, 
+                      global_system_variables.max_allowed_packet);
+  }
 
 #if defined(HAVE_BROKEN_REALPATH)
   my_use_symdir=0;
diff --git a/sql/set_var.cc b/sql/set_var.cc
index 333fb90c795..76957e32536 100644
--- a/sql/set_var.cc
+++ b/sql/set_var.cc
@@ -147,6 +147,8 @@ static void sys_default_general_log_path(THD *thd, enum_var_type type);
 static bool sys_update_slow_log_path(THD *thd, set_var * var);
 static void sys_default_slow_log_path(THD *thd, enum_var_type type);
 static uchar *get_myisam_mmap_size(THD *thd);
+static int check_max_allowed_packet(THD *thd,  set_var *var);
+static int check_net_buffer_length(THD *thd,  set_var *var);
 
 /*
   Variable definition list
@@ -360,7 +362,8 @@ static sys_var_const    sys_lower_case_table_names(&vars,
                                                    (uchar*)
                                                    &lower_case_table_names);
 static sys_var_thd_ulong_session_readonly sys_max_allowed_packet(&vars, "max_allowed_packet",
-					       &SV::max_allowed_packet);
+					       &SV::max_allowed_packet,
+                                               check_max_allowed_packet);
 static sys_var_ulonglong_ptr sys_max_binlog_cache_size(&vars, "max_binlog_cache_size",
                                                        &max_binlog_cache_size);
 static sys_var_long_ptr	sys_max_binlog_size(&vars, "max_binlog_size",
@@ -450,7 +453,8 @@ static sys_var_const            sys_named_pipe(&vars, "named_pipe",
 /* purecov: end */
 #endif
 static sys_var_thd_ulong_session_readonly sys_net_buffer_length(&vars, "net_buffer_length",
-					      &SV::net_buffer_length);
+					      &SV::net_buffer_length,
+                                              check_net_buffer_length);
 static sys_var_thd_ulong	sys_net_read_timeout(&vars, "net_read_timeout",
 					     &SV::net_read_timeout,
 					     0, fix_net_read_timeout);
@@ -4312,6 +4316,36 @@ uchar *sys_var_event_scheduler::value_ptr(THD *thd, enum_var_type type,
 }
 #endif
 
+
+int 
+check_max_allowed_packet(THD *thd,  set_var *var)
+{
+  longlong val= var->value->val_int();
+  if (val < (longlong) global_system_variables.net_buffer_length)
+  {
+    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 
+                        ER_UNKNOWN_ERROR, 
+                        "The value of 'max_allowed_packet' should be no less than "
+                        "the value of 'net_buffer_length'");
+  }
+  return 0;
+}
+
+
+int 
+check_net_buffer_length(THD *thd,  set_var *var)
+{
+  longlong val= var->value->val_int();
+  if (val > (longlong) global_system_variables.max_allowed_packet)
+  {
+    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 
+                        ER_UNKNOWN_ERROR, 
+                        "The value of 'max_allowed_packet' should be no less than "
+                        "the value of 'net_buffer_length'");
+  }
+  return 0;
+}
+
 /****************************************************************************
   Used templates
 ****************************************************************************/