Skip to content

Commit

Permalink
MDEV-4017 - GET_LOCK() with negative timeouts has strange behavior
Browse files Browse the repository at this point in the history
GET_LOCK() silently accepted negative values and NULL for timeout.
Fixed GET_LOCK() to issue a warning and return NULL in such cases.
  • Loading branch information
Sergey Vojtovich committed Jul 29, 2015
1 parent e40bc65 commit 392df76
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ rollback;
create table t0 (n int);
insert t0 select * from t1;
set autocommit=1;
insert into t0 select GET_LOCK("lock1",null);
insert into t0 select GET_LOCK("lock1",0);
set autocommit=0;
create table t2 (n int) engine=innodb;
insert into t2 values (3);
Expand Down
13 changes: 13 additions & 0 deletions mysql-test/r/func_misc.result
Original file line number Diff line number Diff line change
Expand Up @@ -361,5 +361,18 @@ set optimizer_switch=@optimizer_switch_save;
drop view v_merge, vm;
drop table t1,tv;
#
# MDEV-4017 - GET_LOCK() with negative timeouts has strange behavior
#
SELECT GET_LOCK('ul1', NULL);
GET_LOCK('ul1', NULL)
NULL
Warnings:
Warning 1411 Incorrect timeout value: 'NULL' for function get_lock
SELECT GET_LOCK('ul1', -1);
GET_LOCK('ul1', -1)
NULL
Warnings:
Warning 1411 Incorrect timeout value: '-1' for function get_lock
#
# End of 5.5 tests
#
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Warning 1196 Some non-transactional changed tables couldn't be rolled back
create table t0 (n int);
insert t0 select * from t1;
set autocommit=1;
insert into t0 select GET_LOCK("lock1",null);
insert into t0 select GET_LOCK("lock1",0);
set autocommit=0;
create table t2 (n int) engine=innodb;
insert into t2 values (3);
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/suite/binlog/r/binlog_stm_mix_innodb_myisam.result
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ Warning 1196 Some non-transactional changed tables couldn't be rolled back
create table t0 (n int);
insert t0 select * from t1;
set autocommit=1;
insert into t0 select GET_LOCK("lock1",null);
insert into t0 select GET_LOCK("lock1",0);
Warnings:
Note 1592 Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. Statement is unsafe because it uses a system function that may return a different value on the slave.
set autocommit=0;
Expand Down Expand Up @@ -288,7 +288,7 @@ master-bin.000001 # Query # # BEGIN
master-bin.000001 # Query # # use `test`; insert t0 select * from t1
master-bin.000001 # Query # # COMMIT
master-bin.000001 # Query # # BEGIN
master-bin.000001 # Query # # use `test`; insert into t0 select GET_LOCK("lock1",null)
master-bin.000001 # Query # # use `test`; insert into t0 select GET_LOCK("lock1",0)
master-bin.000001 # Query # # COMMIT
master-bin.000001 # Query # # use `test`; create table t2 (n int) engine=innodb
master-bin.000001 # Query # # use `test`; DROP /*!40005 TEMPORARY */ TABLE IF EXISTS `t1`,`ti`
Expand Down
5 changes: 5 additions & 0 deletions mysql-test/t/func_misc.test
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ set optimizer_switch=@optimizer_switch_save;
drop view v_merge, vm;
drop table t1,tv;

--echo #
--echo # MDEV-4017 - GET_LOCK() with negative timeouts has strange behavior
--echo #
SELECT GET_LOCK('ul1', NULL);
SELECT GET_LOCK('ul1', -1);

--echo #
--echo # End of 5.5 tests
Expand Down
18 changes: 18 additions & 0 deletions sql/item_func.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4184,7 +4184,25 @@ longlong Item_func_get_lock::val_int()
it's not guaranteed to be same as on master.
*/
if (thd->slave_thread)
{
null_value= 0;
DBUG_RETURN(1);
}

if (args[1]->null_value ||
(!args[1]->unsigned_flag && ((longlong) timeout < 0)))
{
char buf[22];
if (args[1]->null_value)
strmov(buf, "NULL");
else
llstr(((longlong) timeout), buf);
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
"timeout", buf, "get_lock");
null_value= 1;
DBUG_RETURN(0);
}

mysql_mutex_lock(&LOCK_user_locks);

Expand Down

0 comments on commit 392df76

Please sign in to comment.