diff --git a/mysql-test/r/func_str.result b/mysql-test/r/func_str.result index 2622e070587..6fbb9c811a7 100644 --- a/mysql-test/r/func_str.result +++ b/mysql-test/r/func_str.result @@ -2469,5 +2469,21 @@ id 2 3 4 +DROP TABLE t1; +SELECT SUBSTR('foo',1,0) FROM DUAL; +SUBSTR('foo',1,0) + +SELECT SUBSTR('foo',1,CAST(0 AS SIGNED)) FROM DUAL; +SUBSTR('foo',1,CAST(0 AS SIGNED)) + +SELECT SUBSTR('foo',1,CAST(0 AS UNSIGNED)) FROM DUAL; +SUBSTR('foo',1,CAST(0 AS UNSIGNED)) + +CREATE TABLE t1 (a varchar(10), len int unsigned); +INSERT INTO t1 VALUES ('bar', 2), ('foo', 0); +SELECT SUBSTR(a,1,len) FROM t1; +SUBSTR(a,1,len) +ba + DROP TABLE t1; End of 5.0 tests diff --git a/mysql-test/t/func_str.test b/mysql-test/t/func_str.test index 9148b757b29..945f5a050f4 100644 --- a/mysql-test/t/func_str.test +++ b/mysql-test/t/func_str.test @@ -1216,4 +1216,19 @@ SELECT id FROM t1 WHERE LOCATE(a,p) <=> NULL; DROP TABLE t1; +# +# Bug #27130: SUBSTR with UNSIGNED 0 as the last argument +# + +SELECT SUBSTR('foo',1,0) FROM DUAL; +SELECT SUBSTR('foo',1,CAST(0 AS SIGNED)) FROM DUAL; +SELECT SUBSTR('foo',1,CAST(0 AS UNSIGNED)) FROM DUAL; + +CREATE TABLE t1 (a varchar(10), len int unsigned); +INSERT INTO t1 VALUES ('bar', 2), ('foo', 0); + +SELECT SUBSTR(a,1,len) FROM t1; + +DROP TABLE t1; + --echo End of 5.0 tests diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index 3dc352338a4..a376504512f 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -1143,8 +1143,9 @@ String *Item_func_substr::val_str(String *str) (arg_count == 3 && args[2]->null_value)))) return 0; /* purecov: inspected */ - /* Negative length, will return empty string. */ - if ((arg_count == 3) && (length <= 0) && !args[2]->unsigned_flag) + /* Negative or zero length, will return empty string. */ + if ((arg_count == 3) && (length <= 0) && + (length == 0 || !args[2]->unsigned_flag)) return &my_empty_string; /* Assumes that the maximum length of a String is < INT_MAX32. */