From 7319ef4ef15726590fe3083233956540196e1345 Mon Sep 17 00:00:00 2001
From: "ramil@mysql.com" <>
Date: Wed, 17 May 2006 17:00:30 +0500
Subject: [PATCH 01/11] Fix for #16327: invalid TIMESTAMP values retrieved

---
 mysql-test/r/func_time.result |  7 +++++++
 mysql-test/t/func_time.test   | 10 ++++++++++
 sql/field.cc                  |  2 +-
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/mysql-test/r/func_time.result b/mysql-test/r/func_time.result
index 02f3d2f7273..6f35285c945 100644
--- a/mysql-test/r/func_time.result
+++ b/mysql-test/r/func_time.result
@@ -630,3 +630,10 @@ select monthname(str_to_date(null, '%m')), monthname(str_to_date(null, '%m')),
 monthname(str_to_date(1, '%m')), monthname(str_to_date(0, '%m'));
 monthname(str_to_date(null, '%m'))	monthname(str_to_date(null, '%m'))	monthname(str_to_date(1, '%m'))	monthname(str_to_date(0, '%m'))
 NULL	NULL	January	NULL
+set time_zone='-6:00';
+create table t1(a timestamp);
+insert into t1 values (19691231190001);
+select * from t1;
+a
+1969-12-31 19:00:01
+drop table t1;
diff --git a/mysql-test/t/func_time.test b/mysql-test/t/func_time.test
index 01e4e47d318..e4bb9248bd9 100644
--- a/mysql-test/t/func_time.test
+++ b/mysql-test/t/func_time.test
@@ -322,4 +322,14 @@ select last_day('2005-01-00');
 select monthname(str_to_date(null, '%m')), monthname(str_to_date(null, '%m')),
        monthname(str_to_date(1, '%m')), monthname(str_to_date(0, '%m'));
 
+#
+# Bug #16327: problem with timestamp < 1970
+#
+
+set time_zone='-6:00';
+create table t1(a timestamp);
+insert into t1 values (19691231190001);
+select * from t1;
+drop table t1;
+
 # End of 4.1 tests
diff --git a/sql/field.cc b/sql/field.cc
index a64eaad7308..e018f4122cf 100644
--- a/sql/field.cc
+++ b/sql/field.cc
@@ -3809,7 +3809,7 @@ String *Field_timestamp::val_str(String *val_buffer, String *val_ptr)
   thd->time_zone_used= 1;
 
   temp= time_tmp.year % 100;
-  if (temp < YY_PART_YEAR)
+  if (temp < YY_PART_YEAR - 1)
   {
     *to++= '2';
     *to++= '0';

From e22532fd80f75e7eeb1612e4af07b5ba0bafc22f Mon Sep 17 00:00:00 2001
From: "kent@mysql.com/c-4b4072d5.010-2112-6f72651.cust.bredbandsbolaget.se" <>
Date: Mon, 24 Jul 2006 10:55:26 +0200
Subject: [PATCH 02/11] mwasmnlm, mwccnlm, mwldnlm:   Filter out strange
 control characters, messes up logs

---
 netware/BUILD/mwasmnlm | 5 ++++-
 netware/BUILD/mwccnlm  | 5 ++++-
 netware/BUILD/mwldnlm  | 5 ++++-
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/netware/BUILD/mwasmnlm b/netware/BUILD/mwasmnlm
index 381f84ec0c8..11fc2bc3842 100755
--- a/netware/BUILD/mwasmnlm
+++ b/netware/BUILD/mwasmnlm
@@ -5,4 +5,7 @@ set -e
 
 args=" $*"
 
-wine --debugmsg -all -- mwasmnlm $args
+# NOTE: Option 'pipefail' is not standard sh
+set -o pipefail
+wine --debugmsg -all -- mwasmnlm $args | \
+perl -pe 's/\r//g; s/^\e.*\e(\[J|>)?//; s/[[^:print:]]//g'
diff --git a/netware/BUILD/mwccnlm b/netware/BUILD/mwccnlm
index cb2d62fe8cf..e6840e781f8 100755
--- a/netware/BUILD/mwccnlm
+++ b/netware/BUILD/mwccnlm
@@ -7,4 +7,7 @@ set -e
 # convert it to "-I../include"
 args=" "`echo $* | sed -e 's/-I.\/../-I../g'`
 
-wine --debugmsg -all -- mwccnlm $args
+# NOTE: Option 'pipefail' is not standard sh
+set -o pipefail
+wine --debugmsg -all -- mwccnlm $args | \
+perl -pe 's/\r//g; s/^\e.*\e(\[J|>)?//; s/[[^:print:]]//g'
diff --git a/netware/BUILD/mwldnlm b/netware/BUILD/mwldnlm
index 28566fc5cb1..cc8c9e63c6e 100755
--- a/netware/BUILD/mwldnlm
+++ b/netware/BUILD/mwldnlm
@@ -5,4 +5,7 @@ set -e
 
 args=" $*"
 
-wine --debugmsg -all -- mwldnlm $args
+# NOTE: Option 'pipefail' is not standard sh
+set -o pipefail
+wine --debugmsg -all -- mwldnlm $args | \
+perl -pe 's/\r//g; s/^\e.*\e(\[J|>)?//; s/[[^:print:]]//g'

From d8151b6368fb410aad5496afd7761edd82fb4388 Mon Sep 17 00:00:00 2001
From: "msvensson@neptunus.(none)" <>
Date: Mon, 24 Jul 2006 18:12:57 +0200
Subject: [PATCH 03/11] Bug#21218 Test "mysqlbinlog" fails to execute another
 program on Windows  - Send confusing output to /dev/null

---
 mysql-test/t/mysqlbinlog.test | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/mysql-test/t/mysqlbinlog.test b/mysql-test/t/mysqlbinlog.test
index 0e4d16efb64..9df9edb5487 100644
--- a/mysql-test/t/mysqlbinlog.test
+++ b/mysql-test/t/mysqlbinlog.test
@@ -43,21 +43,21 @@ select "--- Local --" as "";
 #
 
 --replace_result $MYSQL_TEST_DIR MYSQL_TEST_DIR
---exec $MYSQL_BINLOG --short-form --local-load=$MYSQL_TEST_DIR/var/tmp/ $MYSQL_TEST_DIR/var/log/master-bin.000001 
+--exec $MYSQL_BINLOG --short-form --local-load=$MYSQL_TEST_DIR/var/tmp/ $MYSQL_TEST_DIR/var/log/master-bin.000001
 
 # this should not fail but shouldn't produce any working statements
 --disable_query_log
 select "--- Broken LOAD DATA --" as "";
 --enable_query_log
 --replace_result $MYSQL_TEST_DIR MYSQL_TEST_DIR
---exec $MYSQL_BINLOG --short-form --local-load=$MYSQL_TEST_DIR/var/tmp/ $MYSQL_TEST_DIR/var/log/master-bin.000002
+--exec $MYSQL_BINLOG --short-form --local-load=$MYSQL_TEST_DIR/var/tmp/ $MYSQL_TEST_DIR/var/log/master-bin.000002 2> /dev/null
 
 # this should show almost nothing
 --disable_query_log
 select "--- --database --" as "";
 --enable_query_log
 --replace_result $MYSQL_TEST_DIR MYSQL_TEST_DIR
---exec $MYSQL_BINLOG --short-form --local-load=$MYSQL_TEST_DIR/var/tmp/ --database=nottest $MYSQL_TEST_DIR/var/log/master-bin.000001
+--exec $MYSQL_BINLOG --short-form --local-load=$MYSQL_TEST_DIR/var/tmp/ --database=nottest $MYSQL_TEST_DIR/var/log/master-bin.000001 2> /dev/null
 
 # this test for position option
 --disable_query_log
@@ -82,14 +82,14 @@ select "--- Remote --" as "";
 select "--- Broken LOAD DATA --" as "";
 --enable_query_log
 --replace_result $MYSQL_TEST_DIR MYSQL_TEST_DIR
---exec $MYSQL_BINLOG --short-form --local-load=$MYSQL_TEST_DIR/var/tmp/ --read-from-remote-server --user=root --host=127.0.0.1 --port=$MASTER_MYPORT master-bin.000002
+--exec $MYSQL_BINLOG --short-form --local-load=$MYSQL_TEST_DIR/var/tmp/ --read-from-remote-server --user=root --host=127.0.0.1 --port=$MASTER_MYPORT master-bin.000002 2> /dev/null
 
 # And this too ! (altough it is documented)
 --disable_query_log
 select "--- --database --" as "";
 --enable_query_log
 --replace_result $MYSQL_TEST_DIR MYSQL_TEST_DIR
---exec $MYSQL_BINLOG --short-form --local-load=$MYSQL_TEST_DIR/var/tmp/ --read-from-remote-server --user=root --host=127.0.0.1 --port=$MASTER_MYPORT --database=nottest master-bin.000001
+--exec $MYSQL_BINLOG --short-form --local-load=$MYSQL_TEST_DIR/var/tmp/ --read-from-remote-server --user=root --host=127.0.0.1 --port=$MASTER_MYPORT --database=nottest master-bin.000001 2> /dev/null
 
 # Strangely but this works
 --disable_query_log
@@ -130,3 +130,4 @@ select * from t5  /* must be (1),(1) */;
 drop table t1, t2, t03, t04, t3, t4, t5; 
 
 # End of 4.1 tests
+

From 4e845cccc484f690556925daa6dd81cd83e5b3a8 Mon Sep 17 00:00:00 2001
From: "kroki/tomash@moonlight.intranet" <>
Date: Wed, 26 Jul 2006 16:23:07 +0400
Subject: [PATCH 04/11] BUG#21206: memory corruption when too many cursors are
 opened at once

Too many cursors (more than 1024) could lead to memory corruption.
This affects both, stored routines and C API cursors, and the
threshold is per-server, not per-connection.  Similarly, the
corruption could happen when the server was under heavy load
(executing more than 1024 simultaneous complex queries), and this is
the reason why this bug is fixed in 4.1, which doesn't support
cursors.

The corruption was caused by a bug in the temporary tables code, when
an attempt to create a table could lead to a write beyond allocated
space.  Note, that only internal tables were affected (the tables
created internally by the server to resolve the query), not tables
created with CREATE TEMPORARY TABLE.  Another pre-condition for the
bug is TRUE value of --temp-pool startup option, which, however, is a
default.

The cause of a bug was that random memory was overwritten in
bitmap_set_next() due to out-of-bound memory access.
---
 mysys/my_bitmap.c |  2 +-
 sql/sql_select.cc | 12 ++++++++----
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/mysys/my_bitmap.c b/mysys/my_bitmap.c
index f0d3339535d..2af4edbf1a5 100644
--- a/mysys/my_bitmap.c
+++ b/mysys/my_bitmap.c
@@ -110,7 +110,7 @@ uint bitmap_set_next(MY_BITMAP *map)
 {
   uchar *bitmap=map->bitmap;
   uint bit_found = MY_BIT_NONE;
-  uint bitmap_size=map->bitmap_size*8;
+  uint bitmap_size=map->bitmap_size;
   uint i;
 
   DBUG_ASSERT(map->bitmap);
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index fd8a5149edd..9e3883e87e0 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -5240,12 +5240,14 @@ create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
 		       param->group_length : 0,
 		       NullS))
   {
-    bitmap_clear_bit(&temp_pool, temp_pool_slot);
+    if (temp_pool_slot != MY_BIT_NONE)
+      bitmap_clear_bit(&temp_pool, temp_pool_slot);
     DBUG_RETURN(NULL);				/* purecov: inspected */
   }
   if (!(param->copy_field=copy=new Copy_field[field_count]))
   {
-    bitmap_clear_bit(&temp_pool, temp_pool_slot);
+    if (temp_pool_slot != MY_BIT_NONE)
+      bitmap_clear_bit(&temp_pool, temp_pool_slot);
     my_free((gptr) table,MYF(0));		/* purecov: inspected */
     DBUG_RETURN(NULL);				/* purecov: inspected */
   }
@@ -5668,7 +5670,8 @@ create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
   */
   *table->blob_field= 0;
   free_tmp_table(thd,table);                    /* purecov: inspected */
-  bitmap_clear_bit(&temp_pool, temp_pool_slot);
+  if (temp_pool_slot != MY_BIT_NONE)
+    bitmap_clear_bit(&temp_pool, temp_pool_slot);
   DBUG_RETURN(NULL);				/* purecov: inspected */
 }
 
@@ -5831,7 +5834,8 @@ free_tmp_table(THD *thd, TABLE *entry)
   my_free((gptr) entry->record[0],MYF(0));
   free_io_cache(entry);
 
-  bitmap_clear_bit(&temp_pool, entry->temp_pool_slot);
+  if (entry->temp_pool_slot != MY_BIT_NONE)
+    bitmap_clear_bit(&temp_pool, entry->temp_pool_slot);
 
   my_free((gptr) entry,MYF(0));
   thd->proc_info=save_proc_info;

From 345945165aba55d383e980b996476989cdc2b56d Mon Sep 17 00:00:00 2001
From: "kent@mysql.com/c-4b4072d5.010-2112-6f72651.cust.bredbandsbolaget.se" <>
Date: Fri, 28 Jul 2006 21:26:46 +0200
Subject: [PATCH 05/11] sql_select.cc:   Renamed variable, to avoid name clash
 with macro "rem_size"   on AIX 5.3 and "/usr/include/sys/xmem.h" (bug#17648)
 asn.cpp, asn.hpp:   Avoid name clash with NAME_MAX

---
 extra/yassl/taocrypt/include/asn.hpp | 8 ++++----
 extra/yassl/taocrypt/src/asn.cpp     | 2 +-
 sql/sql_select.cc                    | 8 ++++----
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/extra/yassl/taocrypt/include/asn.hpp b/extra/yassl/taocrypt/include/asn.hpp
index 90bc46a59fd..8bea2ae780b 100644
--- a/extra/yassl/taocrypt/include/asn.hpp
+++ b/extra/yassl/taocrypt/include/asn.hpp
@@ -103,7 +103,7 @@ enum Constants
     MAX_ALGO_SIZE =  9,
     MAX_DIGEST_SZ = 25,    // SHA + enum(Bit or Octet) + length(4)
     DSA_SIG_SZ    = 40,
-    NAME_MAX      = 512    // max total of all included names
+    ASN_NAME_MAX  = 512    // max total of all included names
 };
 
 
@@ -216,7 +216,7 @@ enum { SHA_SIZE = 20 };
 // A Signing Authority
 class Signer {
     PublicKey key_;
-    char      name_[NAME_MAX];
+    char      name_[ASN_NAME_MAX];
     byte      hash_[SHA_SIZE];
 public:
     Signer(const byte* k, word32 kSz, const char* n, const byte* h);
@@ -270,8 +270,8 @@ private:
     byte      subjectHash_[SHA_SIZE];   // hash of all Names
     byte      issuerHash_[SHA_SIZE];    // hash of all Names
     byte*     signature_;
-    char      issuer_[NAME_MAX];        // Names
-    char      subject_[NAME_MAX];       // Names
+    char      issuer_[ASN_NAME_MAX];    // Names
+    char      subject_[ASN_NAME_MAX];   // Names
     char      beforeDate_[MAX_DATE_SZ]; // valid before date
     char      afterDate_[MAX_DATE_SZ];  // valid after date
     bool      verify_;                  // Default to yes, but could be off
diff --git a/extra/yassl/taocrypt/src/asn.cpp b/extra/yassl/taocrypt/src/asn.cpp
index beb5490bb66..45fb1e60a0c 100644
--- a/extra/yassl/taocrypt/src/asn.cpp
+++ b/extra/yassl/taocrypt/src/asn.cpp
@@ -665,7 +665,7 @@ void CertDecoder::GetName(NameType nt)
 
     SHA    sha;
     word32 length = GetSequence();  // length of all distinguished names
-    assert (length < NAME_MAX);
+    assert (length < ASN_NAME_MAX);
     length += source_.get_index();
 
     char*  ptr = (nt == ISSUER) ? issuer_ : subject_;
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index a7158960ed0..2ba55ea2792 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -4131,14 +4131,14 @@ greedy_search(JOIN      *join,
   double    read_time=    0.0;
   uint      idx= join->const_tables; // index into 'join->best_ref'
   uint      best_idx;
-  uint      rem_size;    // cardinality of remaining_tables
+  uint      size_remain;    // cardinality of remaining_tables
   POSITION  best_pos;
   JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
 
   DBUG_ENTER("greedy_search");
 
   /* number of tables that remain to be optimized */
-  rem_size= my_count_bits(remaining_tables);
+  size_remain= my_count_bits(remaining_tables);
 
   do {
     /* Find the extension of the current QEP with the lowest cost */
@@ -4146,7 +4146,7 @@ greedy_search(JOIN      *join,
     best_extension_by_limited_search(join, remaining_tables, idx, record_count,
                                      read_time, search_depth, prune_level);
 
-    if (rem_size <= search_depth)
+    if (size_remain <= search_depth)
     {
       /*
         'join->best_positions' contains a complete optimal extension of the
@@ -4182,7 +4182,7 @@ greedy_search(JOIN      *join,
     read_time+=    join->positions[idx].read_time;
 
     remaining_tables&= ~(best_table->table->map);
-    --rem_size;
+    --size_remain;
     ++idx;
 
     DBUG_EXECUTE("opt", print_plan(join, join->tables,

From ef0c9d758883a818e76d5f79e10a509ff89d6d81 Mon Sep 17 00:00:00 2001
From: "kent@mysql.com/c-4b4072d5.010-2112-6f72651.cust.bredbandsbolaget.se" <>
Date: Fri, 28 Jul 2006 22:57:57 +0200
Subject: [PATCH 06/11] mysql.spec.sh:   Man page for "mysqld" command move to
 section 8 (bug#21220)

---
 support-files/mysql.spec.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/support-files/mysql.spec.sh b/support-files/mysql.spec.sh
index 5796e776b83..2958e857049 100644
--- a/support-files/mysql.spec.sh
+++ b/support-files/mysql.spec.sh
@@ -444,7 +444,7 @@ fi
 %doc %attr(644, root, man) %{_mandir}/man1/myisamchk.1*
 %doc %attr(644, root, man) %{_mandir}/man1/myisamlog.1*
 %doc %attr(644, root, man) %{_mandir}/man1/myisampack.1*
-%doc %attr(644, root, man) %{_mandir}/man1/mysqld.1*
+%doc %attr(644, root, man) %{_mandir}/man8/mysqld.8*
 %doc %attr(644, root, man) %{_mandir}/man1/mysqld_multi.1*
 %doc %attr(644, root, man) %{_mandir}/man1/mysqld_safe.1*
 %doc %attr(644, root, man) %{_mandir}/man1/mysql_fix_privilege_tables.1*

From c188a3bf0ae337b12696a0725af767ec16191f1e Mon Sep 17 00:00:00 2001
From: "kent@mysql.com/c-4b4072d5.010-2112-6f72651.cust.bredbandsbolaget.se" <>
Date: Fri, 28 Jul 2006 23:06:49 +0200
Subject: [PATCH 07/11] make_binary_distribution.sh:   Man page for "mysqld"
 command move to section 8 (bug#21220)

---
 scripts/make_binary_distribution.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/scripts/make_binary_distribution.sh b/scripts/make_binary_distribution.sh
index 396c4f83bac..ae24752d290 100644
--- a/scripts/make_binary_distribution.sh
+++ b/scripts/make_binary_distribution.sh
@@ -70,7 +70,7 @@ mkdir $BASE $BASE/bin $BASE/docs \
 
 if [ $BASE_SYSTEM != "netware" ] ; then
  mkdir $BASE/share/mysql $BASE/tests $BASE/sql-bench $BASE/man \
-  $BASE/man/man1 $BASE/data $BASE/data/mysql $BASE/data/test
+  $BASE/man/man1 $BASE/man/man8 $BASE/data $BASE/data/mysql $BASE/data/test
 
  chmod o-rwx $BASE/data $BASE/data/*
 fi
@@ -198,6 +198,7 @@ if [ $BASE_SYSTEM != "netware" ] ; then
   fi
   if [ -d man ] ; then
     $CP man/*.1 $BASE/man/man1
+    $CP man/*.8 $BASE/man/man8
   fi
 fi
 
@@ -255,7 +256,6 @@ else
 fi
 
 # Make safe_mysqld a symlink to mysqld_safe for backwards portability
-# To be removed in MySQL 4.1
 (cd $BASE/bin ; ln -s mysqld_safe safe_mysqld )
 
 # Clean up if we did this from a bk tree

From 2faea9832477756135da5f9600fbb24032f5a98b Mon Sep 17 00:00:00 2001
From: "kent@mysql.com/c-4b4072d5.010-2112-6f72651.cust.bredbandsbolaget.se" <>
Date: Fri, 28 Jul 2006 23:12:40 +0200
Subject: [PATCH 08/11] Makefile.am, configure.in:   Man page for mysqld
 command move to section 8 (bug#21220)

---
 configure.in    | 4 ++++
 man/Makefile.am | 3 ++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/configure.in b/configure.in
index 376cf48c476..57993fa29a1 100644
--- a/configure.in
+++ b/configure.in
@@ -2218,12 +2218,16 @@ then
   man_dirs="man"
   man1_files=`ls -1 $srcdir/man/*.1 | sed -e 's;^.*man/;;'`
   man1_files=`echo $man1_files`
+  man8_files=`ls -8 $srcdir/man/*.8 | sed -e 's;^.*man/;;'`
+  man8_files=`echo $man8_files`
 else
   man_dirs=""
   man1_files=""
+  man8_files=""
 fi
 AC_SUBST(man_dirs)
 AC_SUBST(man1_files)
+AC_SUBST(man8_files)
 
 # Shall we build the bench code?
 AC_ARG_WITH(bench,
diff --git a/man/Makefile.am b/man/Makefile.am
index 9702c4b2ace..5753259fd3d 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -18,7 +18,8 @@
 ## Process this file with automake to create Makefile.in
 
 man1_MANS =   @man1_files@
-EXTRA_DIST =  $(man1_MANS)
+man8_MANS =   @man8_files@
+EXTRA_DIST =  $(man1_MANS) $(man8_MANS)
 
 # Don't update the files from bitkeeper
 %::SCCS/s.%

From bd47f4f3be851c2eba9927a680d13c20b2470134 Mon Sep 17 00:00:00 2001
From: "kent@mysql.com/c-4b4072d5.010-2112-6f72651.cust.bredbandsbolaget.se" <>
Date: Sat, 29 Jul 2006 04:41:50 +0200
Subject: [PATCH 09/11] udf_example.c, udf.test, Makefile.am:   Converted
 "udf_example.cc" to C, avoids C++ runtime lib dependency (bug#21336)

---
 mysql-test/t/udf.test                 |   2 +-
 sql/Makefile.am                       |   4 +-
 sql/{udf_example.cc => udf_example.c} | 157 +++++++++++++++-----------
 3 files changed, 93 insertions(+), 70 deletions(-)
 rename sql/{udf_example.cc => udf_example.c} (87%)

diff --git a/mysql-test/t/udf.test b/mysql-test/t/udf.test
index f3be08c8537..e0c2493c616 100644
--- a/mysql-test/t/udf.test
+++ b/mysql-test/t/udf.test
@@ -1,6 +1,6 @@
 --source include/have_udf.inc
 #
-# To run this tests the "sql/udf_example.cc" need to be compiled into
+# To run this tests the "sql/udf_example.c" need to be compiled into
 # udf_example.so and LD_LIBRARY_PATH should be setup to point out where
 # the library are.
 #
diff --git a/sql/Makefile.am b/sql/Makefile.am
index 416f0faf1a6..8428d6401b5 100644
--- a/sql/Makefile.am
+++ b/sql/Makefile.am
@@ -116,7 +116,7 @@ DEFS =			-DMYSQL_SERVER \
 			@DEFS@
 
 BUILT_SOURCES =		sql_yacc.cc sql_yacc.h lex_hash.h
-EXTRA_DIST =		udf_example.cc $(BUILT_SOURCES)
+EXTRA_DIST =		$(BUILT_SOURCES)
 DISTCLEANFILES =        lex_hash.h
 AM_YFLAGS =		-d
 
@@ -155,7 +155,7 @@ lex_hash.h:	gen_lex_hash$(EXEEXT)
 
 # For testing of udf_example.so
 noinst_LTLIBRARIES= udf_example.la
-udf_example_la_SOURCES= udf_example.cc
+udf_example_la_SOURCES= udf_example.c
 udf_example_la_LDFLAGS= -module -rpath $(pkglibdir)
 
 
diff --git a/sql/udf_example.cc b/sql/udf_example.c
similarity index 87%
rename from sql/udf_example.cc
rename to sql/udf_example.c
index 6ad066eacc2..62995085599 100644
--- a/sql/udf_example.cc
+++ b/sql/udf_example.c
@@ -127,7 +127,7 @@ typedef long long longlong;
 #else
 #include <my_global.h>
 #include <my_sys.h>
-#include <m_string.h>		// To get strmov()
+#include <m_string.h>		/* To get strmov() */
 #endif
 #include <mysql.h>
 #include <ctype.h>
@@ -138,7 +138,6 @@ static pthread_mutex_t LOCK_hostname;
 
 /* These must be right or mysqld will not find the symbol! */
 
-extern "C" {
 my_bool metaphon_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
 void metaphon_deinit(UDF_INIT *initid);
 char *metaphon(UDF_INIT *initid, UDF_ARGS *args, char *result,
@@ -159,7 +158,6 @@ void avgcost_reset( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error
 void avgcost_clear( UDF_INIT* initid, char* is_null, char *error );
 void avgcost_add( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error );
 double avgcost( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error );
-}
 
 
 /*************************************************************************
@@ -221,7 +219,7 @@ my_bool metaphon_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
 ****************************************************************************/
 
 
-void metaphon_deinit(UDF_INIT *initid)
+void metaphon_deinit(UDF_INIT *initid __attribute__((unused)))
 {
 }
 
@@ -267,23 +265,25 @@ static char codes[26] =  {
 #define NOGHTOF(x)  (codes[(x) - 'A'] & 16)	/* BDH */
 
 
-char *metaphon(UDF_INIT *initid, UDF_ARGS *args, char *result,
-	       unsigned long *length, char *is_null, char *error)
+char *metaphon(UDF_INIT *initid __attribute__((unused)),
+               UDF_ARGS *args, char *result, unsigned long *length,
+               char *is_null, char *error __attribute__((unused)))
 {
   const char *word=args->args[0];
-  if (!word)					// Null argument
+  const char *w_end;
+  char *org_result;
+  char *n, *n_start, *n_end; /* pointers to string */
+  char *metaph_end;	     /* pointers to end of metaph */
+  char ntrans[32];	     /* word with uppercase letters */
+  int  KSflag;		     /* state flag for X to KS */
+
+  if (!word)					/* Null argument */
   {
     *is_null=1;
     return 0;
   }
-  const char *w_end=word+args->lengths[0];
-  char *org_result=result;
-
-  char *n, *n_start, *n_end; /* pointers to string */
-  char *metaph, *metaph_end; /* pointers to metaph */
-  char ntrans[32];	     /* word with uppercase letters */
-  char newm[8];		     /* new metaph for comparison */
-  int  KSflag;		     /* state flag for X to KS */
+  w_end=word+args->lengths[0];
+  org_result=result;
 
   /*--------------------------------------------------------
    *  Copy word to internal buffer, dropping non-alphabetic
@@ -519,6 +519,8 @@ char *metaphon(UDF_INIT *initid, UDF_ARGS *args, char *result,
 
 my_bool myfunc_double_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
 {
+  uint i;
+
   if (!args->arg_count)
   {
     strcpy(message,"myfunc_double must have at least one argument");
@@ -528,27 +530,28 @@ my_bool myfunc_double_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
   ** As this function wants to have everything as strings, force all arguments
   ** to strings.
   */
-  for (uint i=0 ; i < args->arg_count; i++)
+  for (i=0 ; i < args->arg_count; i++)
     args->arg_type[i]=STRING_RESULT;
-  initid->maybe_null=1;		// The result may be null
-  initid->decimals=2;		// We want 2 decimals in the result
-  initid->max_length=6;		// 3 digits + . + 2 decimals
+  initid->maybe_null=1;		/* The result may be null */
+  initid->decimals=2;		/* We want 2 decimals in the result */
+  initid->max_length=6;		/* 3 digits + . + 2 decimals */
   return 0;
 }
 
 
-double myfunc_double(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
-		     char *error)
+double myfunc_double(UDF_INIT *initid __attribute__((unused)), UDF_ARGS *args,
+                     char *is_null, char *error __attribute__((unused)))
 {
   unsigned long val = 0;
   unsigned long v = 0;
+  uint i, j;
 
-  for (uint i = 0; i < args->arg_count; i++)
+  for (i = 0; i < args->arg_count; i++)
   {
     if (args->args[i] == NULL)
       continue;
     val += args->lengths[i];
-    for (uint j=args->lengths[i] ; j-- > 0 ;)
+    for (j=args->lengths[i] ; j-- > 0 ;)
       v += args->args[i][j];
   }
   if (val)
@@ -575,22 +578,25 @@ double myfunc_double(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
 
 /* This function returns the sum of all arguments */
 
-longlong myfunc_int(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
-		     char *error)
+longlong myfunc_int(UDF_INIT *initid __attribute__((unused)), UDF_ARGS *args,
+                    char *is_null __attribute__((unused)),
+                    char *error __attribute__((unused)))
 {
   longlong val = 0;
-  for (uint i = 0; i < args->arg_count; i++)
+  uint i;
+
+  for (i = 0; i < args->arg_count; i++)
   {
     if (args->args[i] == NULL)
       continue;
     switch (args->arg_type[i]) {
-    case STRING_RESULT:			// Add string lengths
+    case STRING_RESULT:			/* Add string lengths */
       val += args->lengths[i];
       break;
-    case INT_RESULT:			// Add numbers
+    case INT_RESULT:			/* Add numbers */
       val += *((longlong*) args->args[i]);
       break;
-    case REAL_RESULT:			// Add numers as longlong
+    case REAL_RESULT:			/* Add numers as longlong */
       val += (longlong) *((double*) args->args[i]);
       break;
     default:
@@ -604,7 +610,9 @@ longlong myfunc_int(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
   At least one of _init/_deinit is needed unless the server is started
   with --allow_suspicious_udfs.
 */
-my_bool myfunc_int_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
+my_bool myfunc_int_init(UDF_INIT *initid __attribute__((unused)),
+                        UDF_ARGS *args __attribute__((unused)),
+                        char *message __attribute__((unused)))
 {
   return 0;
 }
@@ -622,7 +630,7 @@ my_bool sequence_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
     return 1;
   }
   if (args->arg_count)
-    args->arg_type[0]= INT_RESULT;		// Force argument to int
+    args->arg_type[0]= INT_RESULT;		/* Force argument to int */
 
   if (!(initid->ptr=(char*) malloc(sizeof(longlong))))
   {
@@ -646,8 +654,9 @@ void sequence_deinit(UDF_INIT *initid)
     free(initid->ptr);
 }
 
-longlong sequence(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
-		   char *error)
+longlong sequence(UDF_INIT *initid __attribute__((unused)), UDF_ARGS *args,
+                  char *is_null __attribute__((unused)),
+                  char *error __attribute__((unused)))
 {
   ulonglong val=0;
   if (args->arg_count)
@@ -670,7 +679,6 @@ longlong sequence(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
 #include <arpa/inet.h>
 #include <netdb.h>
 
-extern "C" {
 my_bool lookup_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
 void lookup_deinit(UDF_INIT *initid);
 char *lookup(UDF_INIT *initid, UDF_ARGS *args, char *result,
@@ -679,7 +687,6 @@ my_bool reverse_lookup_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
 void reverse_lookup_deinit(UDF_INIT *initid);
 char *reverse_lookup(UDF_INIT *initid, UDF_ARGS *args, char *result,
 		     unsigned long *length, char *null_value, char *error);
-}
 
 
 /****************************************************************************
@@ -705,20 +712,26 @@ my_bool lookup_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
   return 0;
 }
 
-void lookup_deinit(UDF_INIT *initid)
+void lookup_deinit(UDF_INIT *initid __attribute__((unused)))
 {
 #if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_SOLARIS_STYLE_GETHOST)
   (void) pthread_mutex_destroy(&LOCK_hostname);
 #endif
 }
 
-char *lookup(UDF_INIT *initid, UDF_ARGS *args, char *result,
-	     unsigned long *res_length, char *null_value, char *error)
+char *lookup(UDF_INIT *initid __attribute__((unused)), UDF_ARGS *args,
+             char *result, unsigned long *res_length, char *null_value,
+             char *error __attribute__((unused)))
 {
   uint length;
+  char name_buff[256];
+  struct hostent *hostent;
+#if defined(HAVE_GETHOSTBYADDR_R) && defined(HAVE_SOLARIS_STYLE_GETHOST)
   int tmp_errno;
-  char name_buff[256],hostname_buff[2048];
-  struct hostent tmp_hostent,*hostent;
+  char hostname_buff[2048];
+  struct hostent tmp_hostent;
+#endif
+  struct in_addr in;
 
   if (!args->args[0] || !(length=args->lengths[0]))
   {
@@ -746,7 +759,6 @@ char *lookup(UDF_INIT *initid, UDF_ARGS *args, char *result,
   }
   VOID(pthread_mutex_unlock(&LOCK_hostname));
 #endif
-  struct in_addr in;
   memcpy_fixed((char*) &in,(char*) *hostent->h_addr_list, sizeof(in.s_addr));
   *res_length= (ulong) (strmov(result, inet_ntoa(in)) - result);
   return result;
@@ -780,18 +792,23 @@ my_bool reverse_lookup_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
   return 0;
 }
 
-void reverse_lookup_deinit(UDF_INIT *initid) 
+void reverse_lookup_deinit(UDF_INIT *initid __attribute__((unused)))
 {
 #if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_SOLARIS_STYLE_GETHOST)
   (void) pthread_mutex_destroy(&LOCK_hostname);
 #endif
 }
 
-char *reverse_lookup(UDF_INIT *initid, UDF_ARGS *args, char *result,
-		     unsigned long *res_length, char *null_value, char *error)
+char *reverse_lookup(UDF_INIT *initid __attribute__((unused)), UDF_ARGS *args,
+                     char *result, unsigned long *res_length,
+                     char *null_value, char *error __attribute__((unused)))
 {
+#if defined(HAVE_GETHOSTBYADDR_R) && defined(HAVE_SOLARIS_STYLE_GETHOST)
   char name_buff[256];
   struct hostent tmp_hostent;
+#endif
+  struct hostent *hp;
+  unsigned long taddr;
   uint length;
 
   if (args->arg_count == 4)
@@ -808,8 +825,8 @@ char *reverse_lookup(UDF_INIT *initid, UDF_ARGS *args, char *result,
 	    (int) *((longlong*) args->args[3]));
   }
   else
-  {						// string argument
-    if (!args->args[0])				// Return NULL for NULL values
+  {					/* string argument */
+    if (!args->args[0])			/* Return NULL for NULL values */
     {
       *null_value=1;
       return 0;
@@ -821,13 +838,12 @@ char *reverse_lookup(UDF_INIT *initid, UDF_ARGS *args, char *result,
     result[length]=0;
   }
 
-  unsigned long taddr = inet_addr(result);
+  taddr = inet_addr(result);
   if (taddr == (unsigned long) -1L)
   {
     *null_value=1;
     return 0;
   }
-  struct hostent *hp;
 #if defined(HAVE_GETHOSTBYADDR_R) && defined(HAVE_SOLARIS_STYLE_GETHOST)
   int tmp_errno;
   if (!(hp=gethostbyaddr_r((char*) &taddr,sizeof(taddr), AF_INET,
@@ -902,11 +918,15 @@ avgcost_init( UDF_INIT* initid, UDF_ARGS* args, char* message )
   /*args->arg_type[0]	= REAL_RESULT;
     args->arg_type[1]	= REAL_RESULT;*/
 
-  initid->maybe_null	= 0;		// The result may be null
-  initid->decimals	= 4;		// We want 4 decimals in the result
-  initid->max_length	= 20;		// 6 digits + . + 10 decimals
+  initid->maybe_null	= 0;		/* The result may be null */
+  initid->decimals	= 4;		/* We want 4 decimals in the result */
+  initid->max_length	= 20;		/* 6 digits + . + 10 decimals */
 
-  data = new struct avgcost_data;
+  if (!(data = (struct avgcost_data*) malloc(sizeof(struct avgcost_data))))
+  {
+    strmov(message,"Couldn't allocate memory");
+    return 1;
+  }
   data->totalquantity	= 0;
   data->totalprice	= 0.0;
 
@@ -918,7 +938,7 @@ avgcost_init( UDF_INIT* initid, UDF_ARGS* args, char* message )
 void
 avgcost_deinit( UDF_INIT* initid )
 {
-  delete initid->ptr;
+  free(initid->ptr);
 }
 
 
@@ -933,7 +953,8 @@ avgcost_reset(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* message)
 /* This is needed to get things to work in MySQL 4.1.1 and above */
 
 void
-avgcost_clear(UDF_INIT* initid, char* is_null, char* message)
+avgcost_clear(UDF_INIT* initid, char* is_null __attribute__((unused)),
+              char* message __attribute__((unused)))
 {
   struct avgcost_data* data = (struct avgcost_data*)initid->ptr;
   data->totalprice=	0.0;
@@ -943,7 +964,9 @@ avgcost_clear(UDF_INIT* initid, char* is_null, char* message)
 
 
 void
-avgcost_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* message)
+avgcost_add(UDF_INIT* initid, UDF_ARGS* args,
+            char* is_null __attribute__((unused)),
+            char* message __attribute__((unused)))
 {
   if (args->args[0] && args->args[1])
   {
@@ -963,7 +986,7 @@ avgcost_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* message)
       if (   ((quantity < 0) && (newquantity < 0))
 	     || ((quantity > 0) && (newquantity > 0)) )
       {
-	data->totalprice	= price * double(newquantity);
+	data->totalprice	= price * (double)newquantity;
       }
       /*
       **	sub q if totalq > 0
@@ -971,15 +994,15 @@ avgcost_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* message)
       */
       else
       {
-	price		  = data->totalprice / double(data->totalquantity);
-	data->totalprice  = price * double(newquantity);
+	price		  = data->totalprice / (double)data->totalquantity;
+	data->totalprice  = price * (double)newquantity;
       }
       data->totalquantity = newquantity;
     }
     else
     {
       data->totalquantity	+= quantity;
-      data->totalprice		+= price * double(quantity);
+      data->totalprice		+= price * (double)quantity;
     }
 
     if (data->totalquantity == 0)
@@ -989,7 +1012,8 @@ avgcost_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* message)
 
 
 double
-avgcost( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* error )
+avgcost( UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
+         char* is_null, char* error __attribute__((unused)))
 {
   struct avgcost_data* data = (struct avgcost_data*)initid->ptr;
   if (!data->count || !data->totalquantity)
@@ -999,16 +1023,14 @@ avgcost( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* error )
   }
 
   *is_null = 0;
-  return data->totalprice/double(data->totalquantity);
+  return data->totalprice/(double)data->totalquantity;
 }
 
-extern "C" {
 my_bool myfunc_argument_name_init(UDF_INIT *initid, UDF_ARGS *args,
 				  char *message);
 char *myfunc_argument_name(UDF_INIT *initid, UDF_ARGS *args, char *result,
 			   unsigned long *length, char *null_value,
 			   char *error);
-}
 
 my_bool myfunc_argument_name_init(UDF_INIT *initid, UDF_ARGS *args,
 				  char *message)
@@ -1024,16 +1046,17 @@ my_bool myfunc_argument_name_init(UDF_INIT *initid, UDF_ARGS *args,
   return 0;
 }
 
-char *myfunc_argument_name(UDF_INIT *initid, UDF_ARGS *args, char *result,
-			   unsigned long *length, char *null_value,
-			   char *error)
+char *myfunc_argument_name(UDF_INIT *initid __attribute__((unused)),
+                           UDF_ARGS *args, char *result,
+                           unsigned long *length, char *null_value,
+                           char *error __attribute__((unused)))
 {
   if (!args->attributes[0])
   {
     null_value= 0;
     return 0;
   }
-  (*length)--; // space for ending \0 (for debugging purposes)
+  (*length)--; /* space for ending \0 (for debugging purposes) */
   if (*length > args->attribute_lengths[0])
     *length= args->attribute_lengths[0];
   memcpy(result, args->attributes[0], *length);

From cc98d26a2318bd5c3e9169a39eb90cba8db46307 Mon Sep 17 00:00:00 2001
From: "kroki/tomash@moonlight.intranet" <>
Date: Sat, 29 Jul 2006 13:50:08 +0400
Subject: [PATCH 10/11] Fix after manual merge.

---
 mysys/my_bitmap.c         |  2 +-
 tests/mysql_client_test.c | 44 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/mysys/my_bitmap.c b/mysys/my_bitmap.c
index 3a83372b637..25ff2651e90 100644
--- a/mysys/my_bitmap.c
+++ b/mysys/my_bitmap.c
@@ -445,7 +445,7 @@ uint bitmap_get_first(const MY_BITMAP *map)
 {
   uchar *bitmap=map->bitmap;
   uint bit_found = MY_BIT_NONE;
-  uint bitmap_size=map->bitmap_size*8;
+  uint bitmap_size=map->bitmap_size;
   uint i;
 
   DBUG_ASSERT(map->bitmap);
diff --git a/tests/mysql_client_test.c b/tests/mysql_client_test.c
index 76a220e2454..5ee63cb8738 100644
--- a/tests/mysql_client_test.c
+++ b/tests/mysql_client_test.c
@@ -15028,6 +15028,49 @@ static void test_bug20152()
 }
 
 
+/*
+  Bug#21206: memory corruption when too many cursors are opened at once
+
+  Memory corruption happens when more than 1024 cursors are open
+  simultaneously.
+*/
+static void test_bug21206()
+{
+  const size_t cursor_count= 1025;
+
+  const char *create_table[]=
+  {
+    "DROP TABLE IF EXISTS t1",
+    "CREATE TABLE t1 (i INT)",
+    "INSERT INTO t1 VALUES (1), (2), (3)"
+  };
+  const char *query= "SELECT * FROM t1";
+
+  Stmt_fetch *fetch_array=
+    (Stmt_fetch*) calloc(cursor_count, sizeof(Stmt_fetch));
+
+  Stmt_fetch *fetch;
+
+  DBUG_ENTER("test_bug21206");
+  myheader("test_bug21206");
+
+  fill_tables(create_table, sizeof(create_table) / sizeof(*create_table));
+
+  for (fetch= fetch_array; fetch < fetch_array + cursor_count; ++fetch)
+  {
+    /* Init will exit(1) in case of error */
+    stmt_fetch_init(fetch, fetch - fetch_array, query);
+  }
+
+  for (fetch= fetch_array; fetch < fetch_array + cursor_count; ++fetch)
+    stmt_fetch_close(fetch);
+
+  free(fetch_array);
+
+  DBUG_VOID_RETURN;
+}
+
+
 /*
   Read and parse arguments and MySQL options from my.cnf
 */
@@ -15300,6 +15343,7 @@ static struct my_tests_st my_tests[]= {
   { "test_bug14169", test_bug14169 },
   { "test_bug17667", test_bug17667 },
   { "test_bug19671", test_bug19671},
+  { "test_bug21206", test_bug21206},
   { 0, 0 }
 };
 

From f2ec5d7128f3b154d22f0acc2199c6a10c0e3173 Mon Sep 17 00:00:00 2001
From: "kent@mysql.com/c-4b4072d5.010-2112-6f72651.cust.bredbandsbolaget.se" <>
Date: Sat, 29 Jul 2006 17:24:48 +0200
Subject: [PATCH 11/11] configure.in:   Corrected typo

---
 configure.in | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/configure.in b/configure.in
index 57993fa29a1..0be3bcf0406 100644
--- a/configure.in
+++ b/configure.in
@@ -2218,7 +2218,7 @@ then
   man_dirs="man"
   man1_files=`ls -1 $srcdir/man/*.1 | sed -e 's;^.*man/;;'`
   man1_files=`echo $man1_files`
-  man8_files=`ls -8 $srcdir/man/*.8 | sed -e 's;^.*man/;;'`
+  man8_files=`ls -1 $srcdir/man/*.8 | sed -e 's;^.*man/;;'`
   man8_files=`echo $man8_files`
 else
   man_dirs=""