From 9c59cfe4d7fd6047212eb4e84aa2d657aea2134c Mon Sep 17 00:00:00 2001
From: "andrey@lmy004." <>
Date: Fri, 4 Aug 2006 12:50:49 +0200
Subject: [PATCH] Fix for bug#21416 SP: Recursion level higher than zero needed
 for non-recursive call

The following procedure was not possible if max_sp_recursion_depth is 0
create procedure show_proc() show create procedure show_proc;

Actually there is no recursive call but the limit is checked.

Solved by temporarily increasing the thread's limit just before the fetch from cache
and decreasing after that.
---
 mysql-test/r/sp.result |  7 +++++++
 mysql-test/t/sp.test   | 10 ++++++++++
 sql/sp.cc              | 21 +++++++++++++++------
 3 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/mysql-test/r/sp.result b/mysql-test/r/sp.result
index db72d19044..3ef1066ecd 100644
--- a/mysql-test/r/sp.result
+++ b/mysql-test/r/sp.result
@@ -5051,4 +5051,11 @@ concat('data was: /', var1, '/')
 data was: /1/
 drop table t3|
 drop procedure bug15217|
+drop procedure if exists bug21416|
+create procedure bug21416() show create procedure bug21416|
+call bug21416()|
+Procedure	sql_mode	Create Procedure
+bug21416		CREATE DEFINER=`root`@`localhost` PROCEDURE `bug21416`()
+show create procedure bug21416
+drop procedure bug21416|
 drop table t1,t2;
diff --git a/mysql-test/t/sp.test b/mysql-test/t/sp.test
index 99f3bbbbd1..fcf179d3e8 100644
--- a/mysql-test/t/sp.test
+++ b/mysql-test/t/sp.test
@@ -5950,6 +5950,16 @@ call bug15217()|
 drop table t3|
 drop procedure bug15217|
 
+#
+# BUG#21416: SP: Recursion level higher than zero needed for non-recursive call
+#
+--disable_warnings
+drop procedure if exists bug21416|
+--enable_warnings
+create procedure bug21416() show create procedure bug21416|
+call bug21416()|
+drop procedure bug21416|
+
 #
 # BUG#NNNN: New bug synopsis
 #
diff --git a/sql/sp.cc b/sql/sp.cc
index e794a46140..bea4eb1283 100644
--- a/sql/sp.cc
+++ b/sql/sp.cc
@@ -992,6 +992,12 @@ sp_find_routine(THD *thd, int type, sp_name *name, sp_cache **cp,
       }
       DBUG_RETURN(sp->m_first_free_instance);
     }
+    /*
+      Actually depth could be +1 than the actual value in case a SP calls
+      SHOW CREATE PROCEDURE. Hence, the linked list could hold up to one more
+      instance.
+    */
+
     level= sp->m_last_cached_sp->m_recursion_level + 1;
     if (level > depth)
     {
@@ -1161,19 +1167,22 @@ sp_update_procedure(THD *thd, sp_name *name, st_sp_chistics *chistics)
 int
 sp_show_create_procedure(THD *thd, sp_name *name)
 {
+  int ret= SP_KEY_NOT_FOUND;
   sp_head *sp;
   DBUG_ENTER("sp_show_create_procedure");
   DBUG_PRINT("enter", ("name: %.*s", name->m_name.length, name->m_name.str));
 
+  /*
+    Increase the recursion limit for this statement. SHOW CREATE PROCEDURE
+    does not do actual recursion.  
+  */
+  thd->variables.max_sp_recursion_depth++;
   if ((sp= sp_find_routine(thd, TYPE_ENUM_PROCEDURE, name,
                            &thd->sp_proc_cache, FALSE)))
-  {
-    int ret= sp->show_create_procedure(thd);
+    ret= sp->show_create_procedure(thd);
 
-    DBUG_RETURN(ret);
-  }
-
-  DBUG_RETURN(SP_KEY_NOT_FOUND);
+  thd->variables.max_sp_recursion_depth--;
+  DBUG_RETURN(ret);
 }
 
 
-- 
2.30.9