Skip to content

Commit 44a27a2

Browse files
committed
MDEV-28416 Incorrect AUTO_INCREMENT may be issued when close to UINT64_MAX
ha_innobase::get_auto_increment(): In the overflow check, account for 64-bit unsigned integer wrap-around. Based on mysql/mysql-server@25ecfe7
1 parent f21a875 commit 44a27a2

File tree

5 files changed

+101
-9
lines changed

5 files changed

+101
-9
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# MDEV-28416 Incorrect AUTO_INCREMENT may be issued
3+
#
4+
SET @aii=@@auto_increment_increment;
5+
SET auto_increment_increment=300;
6+
CREATE TABLE t1 (a SERIAL) ENGINE=innodb
7+
PARTITION BY RANGE (a) (
8+
PARTITION p0 VALUES LESS THAN (6),
9+
PARTITION p1 VALUES LESS THAN MAXVALUE
10+
);
11+
INSERT INTO t1 VALUES (18446744073709551613);
12+
SHOW CREATE TABLE t1;
13+
Table Create Table
14+
t1 CREATE TABLE `t1` (
15+
`a` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
16+
UNIQUE KEY `a` (`a`)
17+
) ENGINE=InnoDB AUTO_INCREMENT=18446744073709551614 DEFAULT CHARSET=latin1
18+
PARTITION BY RANGE (`a`)
19+
(PARTITION `p0` VALUES LESS THAN (6) ENGINE = InnoDB,
20+
PARTITION `p1` VALUES LESS THAN MAXVALUE ENGINE = InnoDB)
21+
INSERT INTO t1 VALUES (NULL);
22+
ERROR 22003: Out of range value for column 'a' at row 1
23+
SHOW CREATE TABLE t1;
24+
Table Create Table
25+
t1 CREATE TABLE `t1` (
26+
`a` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
27+
UNIQUE KEY `a` (`a`)
28+
) ENGINE=InnoDB AUTO_INCREMENT=298 DEFAULT CHARSET=latin1
29+
PARTITION BY RANGE (`a`)
30+
(PARTITION `p0` VALUES LESS THAN (6) ENGINE = InnoDB,
31+
PARTITION `p1` VALUES LESS THAN MAXVALUE ENGINE = InnoDB)
32+
DROP TABLE t1;
33+
SET auto_increment_increment=@aii;
34+
# End of 10.2 tests

mysql-test/suite/innodb/r/innodb-autoinc.result

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
drop table if exists t1;
21
CREATE TABLE t1 (c1 BIGINT PRIMARY KEY AUTO_INCREMENT, c2 VARCHAR(10)) ENGINE=InnoDB;
32
INSERT INTO t1 VALUES (9223372036854775807, null);
43
INSERT INTO t1 (c2) VALUES ('innodb');
@@ -1619,3 +1618,27 @@ id name
16191618
-1 dog
16201619
2 cat
16211620
DROP PROCEDURE autoinc_mdev15353_one;
1621+
#
1622+
# MDEV-28416 Incorrect AUTO_INCREMENT may be issued
1623+
#
1624+
SET @aii=@@auto_increment_increment;
1625+
SET auto_increment_increment=300;
1626+
CREATE TABLE t1 (a SERIAL) ENGINE=innodb;
1627+
INSERT INTO t1 VALUES (18446744073709551613);
1628+
SHOW CREATE TABLE t1;
1629+
Table Create Table
1630+
t1 CREATE TABLE `t1` (
1631+
`a` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
1632+
UNIQUE KEY `a` (`a`)
1633+
) ENGINE=InnoDB AUTO_INCREMENT=18446744073709551614 DEFAULT CHARSET=latin1
1634+
INSERT INTO t1 VALUES (NULL);
1635+
ERROR 22003: Out of range value for column 'a' at row 1
1636+
SHOW CREATE TABLE t1;
1637+
Table Create Table
1638+
t1 CREATE TABLE `t1` (
1639+
`a` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
1640+
UNIQUE KEY `a` (`a`)
1641+
) ENGINE=InnoDB AUTO_INCREMENT=18446744073709551615 DEFAULT CHARSET=latin1
1642+
DROP TABLE t1;
1643+
SET auto_increment_increment=@aii;
1644+
# End of 10.2 tests
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--source include/have_partition.inc
2+
--source include/have_innodb.inc
3+
4+
--echo #
5+
--echo # MDEV-28416 Incorrect AUTO_INCREMENT may be issued
6+
--echo #
7+
8+
SET @aii=@@auto_increment_increment;
9+
SET auto_increment_increment=300;
10+
11+
CREATE TABLE t1 (a SERIAL) ENGINE=innodb
12+
PARTITION BY RANGE (a) (
13+
PARTITION p0 VALUES LESS THAN (6),
14+
PARTITION p1 VALUES LESS THAN MAXVALUE
15+
);
16+
INSERT INTO t1 VALUES (18446744073709551613);
17+
SHOW CREATE TABLE t1;
18+
--error HA_ERR_AUTOINC_ERANGE
19+
INSERT INTO t1 VALUES (NULL);
20+
SHOW CREATE TABLE t1;
21+
DROP TABLE t1;
22+
SET auto_increment_increment=@aii;
23+
24+
--echo # End of 10.2 tests

mysql-test/suite/innodb/t/innodb-autoinc.test

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
--source include/have_innodb.inc
2-
# embedded server ignores 'delayed', so skip this
3-
-- source include/not_embedded.inc
4-
5-
--disable_warnings
6-
drop table if exists t1;
7-
--enable_warnings
82

93
#
104
# Bug #34335
@@ -770,3 +764,20 @@ DROP TABLE t1;
770764

771765
SET @engine='INNODB';
772766
--source include/autoinc_mdev15353.inc
767+
768+
--echo #
769+
--echo # MDEV-28416 Incorrect AUTO_INCREMENT may be issued
770+
--echo #
771+
772+
SET @aii=@@auto_increment_increment;
773+
SET auto_increment_increment=300;
774+
CREATE TABLE t1 (a SERIAL) ENGINE=innodb;
775+
INSERT INTO t1 VALUES (18446744073709551613);
776+
SHOW CREATE TABLE t1;
777+
--error HA_ERR_AUTOINC_ERANGE
778+
INSERT INTO t1 VALUES (NULL);
779+
SHOW CREATE TABLE t1;
780+
DROP TABLE t1;
781+
SET auto_increment_increment=@aii;
782+
783+
--echo # End of 10.2 tests

storage/innobase/handler/ha_innodb.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16970,8 +16970,8 @@ ha_innobase::get_auto_increment(
1697016970

1697116971
(3) It is restricted only for insert operations. */
1697216972

16973-
if (increment > 1 && thd_sql_command(m_user_thd) != SQLCOM_ALTER_TABLE
16974-
&& autoinc < col_max_value) {
16973+
if (increment > 1 && increment <= ~autoinc && autoinc < col_max_value
16974+
&& thd_sql_command(m_user_thd) != SQLCOM_ALTER_TABLE) {
1697516975

1697616976
ulonglong prev_auto_inc = autoinc;
1697716977

0 commit comments

Comments
 (0)