From cd6db92615ba116d3bf61eebed80dc64e8baf35d Mon Sep 17 00:00:00 2001
From: unknown <gshchepa/uchum@gshchepa.loc>
Date: Sun, 29 Apr 2007 00:50:33 +0500
Subject: [PATCH] Fixed bug #20710. This bug occurs when error message length
 exceeds allowed limit: my_error() function outputs "%s" sequences instead of
 long string arguments.

Formats like %-.64s are very common in errmsg.txt files, however my_error()
function simply ignores precision of those formats.


mysys/my_error.c:
  Fixed bug #20710.
  This bug occurs when error message length exceeds allowed limit: my_error()
  function output "%s" sequences instead of long string arguments.
  my_error() function has been fixed to accept formats like %-.64s.
mysql-test/t/alter_table.test:
  Added test case for bug #20710.
mysql-test/r/alter_table.result:
  Added test case for bug #20710.
---
 mysql-test/r/alter_table.result |  5 +++++
 mysql-test/t/alter_table.test   | 11 +++++++++++
 mysys/my_error.c                | 13 +++++++++++++
 3 files changed, 29 insertions(+)

diff --git a/mysql-test/r/alter_table.result b/mysql-test/r/alter_table.result
index da5cf688325..8957e1ee7a1 100644
--- a/mysql-test/r/alter_table.result
+++ b/mysql-test/r/alter_table.result
@@ -803,3 +803,8 @@ ERROR 42000: You have an error in your SQL syntax; check the manual that corresp
 alter table table_24562 order by no_such_col;
 ERROR 42S22: Unknown column 'no_such_col' in 'order clause'
 drop table table_24562;
+CREATE TABLE t1 (c1 CHAR(255));
+INSERT INTO t1 VALUES (REPEAT("x", 255)), (REPEAT("x", 255));
+ALTER TABLE t1 ADD UNIQUE (c1);
+ERROR 23000: Duplicate entry 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' for key 1
+DROP TABLE t1;
diff --git a/mysql-test/t/alter_table.test b/mysql-test/t/alter_table.test
index 52a569dfb57..874c42ac0b6 100644
--- a/mysql-test/t/alter_table.test
+++ b/mysql-test/t/alter_table.test
@@ -583,5 +583,16 @@ alter table table_24562 order by no_such_col;
 
 drop table table_24562;
 
+#
+# Bug #20710: adding unique index of column with duplicated
+# long values to reproduce error message with truncated key value.
+#
+
+CREATE TABLE t1 (c1 CHAR(255));
+INSERT INTO t1 VALUES (REPEAT("x", 255)), (REPEAT("x", 255));
+--error 1062
+ALTER TABLE t1 ADD UNIQUE (c1);
+DROP TABLE t1;
+
 # End of 4.1 tests
 
diff --git a/mysys/my_error.c b/mysys/my_error.c
index 8a377f63c7e..0f8ffb7c05f 100644
--- a/mysys/my_error.c
+++ b/mysys/my_error.c
@@ -82,6 +82,11 @@ int my_error(int nr,myf MyFlags, ...)
         If "%.*u" or "%.*d" are encountered, the precision number is read
         from the variable argument list but its value is ignored.
       */
+      if (*tpos == '-')
+      {
+        tpos++;
+        olen--;
+      }
       prec_supplied= 0;
       if (*tpos== '.')
       {
@@ -94,6 +99,14 @@ int my_error(int nr,myf MyFlags, ...)
           prec_chars= va_arg(ap, int); /* get length parameter */
           prec_supplied= 1;
         }
+        else
+        {
+          for (prec_chars= 0; my_isdigit(&my_charset_latin1, *tpos); tpos++, olen--)
+          {
+            prec_supplied= 1;
+            prec_chars= prec_chars * 10 + *tpos - '0';
+          }
+        }
       }
 
       if (!prec_supplied)
-- 
2.30.9