=== modified file 'mysql-test/r/signal_demo3.result'
--- a/mysql-test/r/signal_demo3.result	2008-06-13 17:06:59 +0000
+++ b/mysql-test/r/signal_demo3.result	2008-06-18 01:09:10 +0000
@@ -1,3 +1,11 @@
+SET @start_global_value = @@global.max_error_count;
+SELECT @start_global_value;
+@start_global_value
+64
+SET @start_session_value = @@session.max_error_count;
+SELECT @start_session_value;
+@start_session_value
+64
 drop database if exists demo;
 create database demo;
 use demo;
@@ -79,4 +87,57 @@
 Error	1677	Oops in proc_3
 Error	1677	Oops in proc_2
 Error	1677	Oops in proc_1
+SET @@session.max_error_count = 5;
+SELECT @@session.max_error_count;
+@@session.max_error_count
+5
+call proc_1();
+ERROR 55555: Oops in proc_1
+show warnings;
+Level	Code	Message
+Error	1677	Oops in proc_5
+Error	1677	Oops in proc_4
+Error	1677	Oops in proc_3
+Error	1677	Oops in proc_2
+Error	1677	Oops in proc_1
+SET @@session.max_error_count = 7;
+SELECT @@session.max_error_count;
+@@session.max_error_count
+7
+call proc_1();
+ERROR 55555: Oops in proc_1
+show warnings;
+Level	Code	Message
+Error	1677	Oops in proc_7
+Error	1677	Oops in proc_6
+Error	1677	Oops in proc_5
+Error	1677	Oops in proc_4
+Error	1677	Oops in proc_3
+Error	1677	Oops in proc_2
+Error	1677	Oops in proc_1
+SET @@session.max_error_count = 9;
+SELECT @@session.max_error_count;
+@@session.max_error_count
+9
+call proc_1();
+ERROR 55555: Oops in proc_1
+show warnings;
+Level	Code	Message
+Error	1677	Oops in proc_9
+Error	1677	Oops in proc_8
+Error	1677	Oops in proc_7
+Error	1677	Oops in proc_6
+Error	1677	Oops in proc_5
+Error	1677	Oops in proc_4
+Error	1677	Oops in proc_3
+Error	1677	Oops in proc_2
+Error	1677	Oops in proc_1
 drop database demo;
+SET @@global.max_error_count = @start_global_value;
+SELECT @@global.max_error_count;
+@@global.max_error_count
+64
+SET @@session.max_error_count = @start_session_value;
+SELECT @@session.max_error_count;
+@@session.max_error_count
+64

=== modified file 'mysql-test/t/signal_demo3.test'
--- a/mysql-test/t/signal_demo3.test	2008-06-13 17:06:59 +0000
+++ b/mysql-test/t/signal_demo3.test	2008-06-18 01:09:10 +0000
@@ -17,6 +17,13 @@
 # Demonstrate how RESIGNAL can be used to print a stack trace
 #
 
+# Save defaults
+
+SET @start_global_value = @@global.max_error_count;
+SELECT @start_global_value;
+SET @start_session_value = @@session.max_error_count;
+SELECT @start_session_value;
+
 --disable_warnings
 drop database if exists demo;
 --enable_warnings
@@ -120,5 +127,33 @@
 
 show warnings;
 
+SET @@session.max_error_count = 5;
+SELECT @@session.max_error_count;
+
+-- error ER_SIGNAL_EXCEPTION
+call proc_1();
+show warnings;
+
+SET @@session.max_error_count = 7;
+SELECT @@session.max_error_count;
+
+-- error ER_SIGNAL_EXCEPTION
+call proc_1();
+show warnings;
+
+SET @@session.max_error_count = 9;
+SELECT @@session.max_error_count;
+
+-- error ER_SIGNAL_EXCEPTION
+call proc_1();
+show warnings;
+
 drop database demo;
 
+# Restore defaults
+
+SET @@global.max_error_count = @start_global_value;
+SELECT @@global.max_error_count;
+SET @@session.max_error_count = @start_session_value;
+SELECT @@session.max_error_count;
+

=== modified file 'mysql-test/t/signal_diag.test'
--- a/mysql-test/t/signal_diag.test	2008-06-11 22:53:47 +0000
+++ b/mysql-test/t/signal_diag.test	2008-06-18 01:09:10 +0000
@@ -67,7 +67,7 @@
 # Error : 1 condition
 --error ER_BAD_TABLE_ERROR
 drop table `no_such_table_and_this_error_better_stick`;
-# REPORTME: no_warnings_for_error is set in mysql_rm_table_part2(),
+# Bug#37431: no_warnings_for_error is set in mysql_rm_table_part2(),
 # which silence the error
 show warnings;
 GET CURRENT DIAGNOSTICS @foo = NUMBER;

=== modified file 'sql/sql_class.cc'
--- a/sql/sql_class.cc	2008-06-11 22:53:47 +0000
+++ b/sql/sql_class.cc	2008-06-18 01:09:10 +0000
@@ -423,6 +423,13 @@
   {
     uint convert_error= 0;
     uint32 copy_len;
+
+    /*
+      Enforce that m_message is a valid UTF8 string,
+      and is properly terminated.
+      Note: any invalid character in utf8_message will be
+      converted to a '?' character.
+    */
     copy_len= copy_and_convert(m_message,
                                sizeof(m_message) - 1,
                                & my_charset_utf8_bin,
@@ -521,8 +528,6 @@
                                    const char *message_arg,
                                    const char *sqlstate)
 {
-  uint convert_error= 0;
-
   /*
     Only allowed to report error if has not yet reported a success
     The only exception is when we flush the message to the client,

=== modified file 'sql/sql_class.h'
--- a/sql/sql_class.h	2008-06-11 22:53:47 +0000
+++ b/sql/sql_class.h	2008-06-18 01:09:10 +0000
@@ -1214,6 +1214,9 @@
   static SQL_condition* deep_copy(THD *thd, MEM_ROOT *mem_root,
                                   const SQL_condition *cond);
 
+  void deep_copy(MEM_ROOT *mem_root, const SQL_condition *cond);
+
+
   void set(THD *thd, uint code, const char *str,
            MYSQL_ERROR::enum_warning_level level, myf MyFlags);
 
@@ -1230,9 +1233,6 @@
   const char* get_sqlstate() const
   { return m_returned_sqlstate; }
 
-private:
-  void deep_copy(MEM_ROOT *mem_root, const SQL_condition *cond);
-
 public:
   UTF8String64 m_class_origin;
   UTF8String64 m_subclass_origin;

=== modified file 'sql/sql_signal.cc'
--- a/sql/sql_signal.cc	2008-06-11 22:53:47 +0000
+++ b/sql/sql_signal.cc	2008-06-18 01:09:10 +0000
@@ -200,8 +200,6 @@
 void
 SQL_condition::deep_copy(MEM_ROOT *mem_root, const SQL_condition *cond)
 {
-  const char* copy;
-
   memcpy(m_returned_sqlstate, cond->m_returned_sqlstate,
          sizeof(m_returned_sqlstate));
 
@@ -719,9 +717,20 @@
   }
 
   /* RESIGNAL with signal_value */
+
+  /* Make room for 2 conditions */
+  while ((thd->main_da.m_stmt_area.warn_list.elements > 0) &&
+         ((thd->main_da.m_stmt_area.warn_list.elements + 2)
+                       > thd->variables.max_error_count))
+  {
+    thd->main_da.m_stmt_area.warn_list.pop();
+    thd->main_da.m_stmt_area.m_more= TRUE;
+  }
+
   thd->raise_condition_no_handler(signaled);
 
   SQL_condition new_cond;
+  new_cond.deep_copy(thd->mem_root, signaled);
   result= raise_condition(thd, & new_cond);
   DBUG_RETURN(result);
 }



