diff --git a/mysql-test/r/cast.result b/mysql-test/r/cast.result index fa8e810cf2b..7bf710f3491 100644 --- a/mysql-test/r/cast.result +++ b/mysql-test/r/cast.result @@ -367,3 +367,6 @@ DROP TABLE t1; select cast(NULL as decimal(6)) as t1; t1 NULL +select cast(1.0e+300 as signed int); +cast(1.0e+300 as signed int) +9223372036854775807 diff --git a/mysql-test/t/cast.test b/mysql-test/t/cast.test index 6220b4cbae7..028cb08620e 100644 --- a/mysql-test/t/cast.test +++ b/mysql-test/t/cast.test @@ -160,6 +160,11 @@ select cast(concat('184467440','73709551615') as signed); select cast(repeat('1',20) as unsigned); select cast(repeat('1',20) as signed); +# +# Bug #13344: cast of large decimal to signed int not handled correctly +# +select cast(1.0e+300 as signed int); + # End of 4.1 tests diff --git a/sql/item.h b/sql/item.h index 320591d4d99..7a7e080db02 100644 --- a/sql/item.h +++ b/sql/item.h @@ -1331,6 +1331,14 @@ public: longlong val_int() { DBUG_ASSERT(fixed == 1); + if (value <= (double) LONGLONG_MIN) + { + return LONGLONG_MIN; + } + else if (value >= (double) (ulonglong) LONGLONG_MAX) + { + return LONGLONG_MAX; + } return (longlong) (value+(value > 0 ? 0.5 : -0.5)); } String *val_str(String*);