Skip to content

Commit f22093a

Browse files
committed
MDEV-22764 Crash with a stored aggregate function returning INET6
Item_sum_sp did not override val_native(). So the reported script crashed in the default implementation in Item::val_native() on DBUG_ASSERT(). Implementing a correct Item_sum_sp::val_native().
1 parent dec3f8c commit f22093a

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

plugin/type_inet/mysql-test/type_inet/type_inet6.result

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,3 +1986,38 @@ ERROR HY000: Illegal parameter data type inet6 for operation 'extract(day)'
19861986
DROP TABLE t1;
19871987
SELECT EXTRACT(DAY FROM CAST('::' AS INET6));
19881988
ERROR HY000: Illegal parameter data type inet6 for operation 'extract(day)'
1989+
#
1990+
# MDEV-22764 Crash with a stored aggregate function returning INET6
1991+
#
1992+
CREATE OR REPLACE AGGREGATE FUNCTION aggregate_min_inet6(x INET6) RETURNS INET6
1993+
BEGIN
1994+
DECLARE res INET6 DEFAULT NULL;
1995+
DECLARE CONTINUE HANDLER FOR NOT FOUND
1996+
RETURN res;
1997+
LOOP
1998+
FETCH GROUP NEXT ROW;
1999+
IF (res IS NULL) OR (res > x) THEN
2000+
SET res= x;
2001+
END IF;
2002+
END LOOP;
2003+
END;
2004+
$$
2005+
CREATE OR REPLACE TABLE t1 (name CHAR(30), val INET6);
2006+
INSERT INTO t1 VALUES ('a', '::05');
2007+
INSERT INTO t1 VALUES ('a', '::03');
2008+
INSERT INTO t1 VALUES ('b', '::01');
2009+
INSERT INTO t1 VALUES ('b', '::02');
2010+
INSERT INTO t1 VALUES ('b', '::05');
2011+
SELECT name, aggregate_min_inet6(val) pc FROM t1 GROUP BY name;
2012+
name pc
2013+
a ::3
2014+
b ::1
2015+
CREATE OR REPLACE TABLE t2 (name CHAR(30), val INET6);
2016+
INSERT INTO t2 SELECT name, aggregate_min_inet6(val) pc FROM t1 GROUP BY name;
2017+
SELECT * FROM t2;
2018+
name val
2019+
a ::3
2020+
b ::1
2021+
DROP TABLE t2;
2022+
DROP TABLE t1;
2023+
DROP FUNCTION aggregate_min_inet6;

plugin/type_inet/mysql-test/type_inet/type_inet6.test

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,3 +1457,40 @@ SELECT EXTRACT(DAY FROM a) FROM t1;
14571457
DROP TABLE t1;
14581458
--error ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION
14591459
SELECT EXTRACT(DAY FROM CAST('::' AS INET6));
1460+
1461+
1462+
--echo #
1463+
--echo # MDEV-22764 Crash with a stored aggregate function returning INET6
1464+
--echo #
1465+
1466+
DELIMITER $$;
1467+
CREATE OR REPLACE AGGREGATE FUNCTION aggregate_min_inet6(x INET6) RETURNS INET6
1468+
BEGIN
1469+
DECLARE res INET6 DEFAULT NULL;
1470+
DECLARE CONTINUE HANDLER FOR NOT FOUND
1471+
RETURN res;
1472+
LOOP
1473+
FETCH GROUP NEXT ROW;
1474+
IF (res IS NULL) OR (res > x) THEN
1475+
SET res= x;
1476+
END IF;
1477+
END LOOP;
1478+
END;
1479+
$$
1480+
DELIMITER ;$$
1481+
1482+
CREATE OR REPLACE TABLE t1 (name CHAR(30), val INET6);
1483+
INSERT INTO t1 VALUES ('a', '::05');
1484+
INSERT INTO t1 VALUES ('a', '::03');
1485+
INSERT INTO t1 VALUES ('b', '::01');
1486+
INSERT INTO t1 VALUES ('b', '::02');
1487+
INSERT INTO t1 VALUES ('b', '::05');
1488+
SELECT name, aggregate_min_inet6(val) pc FROM t1 GROUP BY name;
1489+
1490+
CREATE OR REPLACE TABLE t2 (name CHAR(30), val INET6);
1491+
INSERT INTO t2 SELECT name, aggregate_min_inet6(val) pc FROM t1 GROUP BY name;
1492+
SELECT * FROM t2;
1493+
DROP TABLE t2;
1494+
1495+
DROP TABLE t1;
1496+
DROP FUNCTION aggregate_min_inet6;

sql/item_sum.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,11 @@ class Item_sum_sp :public Item_sum,
14071407
return sp_result_field->val_decimal(dec_buf);
14081408
}
14091409

1410+
bool val_native(THD *thd, Native *to)
1411+
{
1412+
return null_value= execute() || sp_result_field->val_native(to);
1413+
}
1414+
14101415
String *val_str(String *str)
14111416
{
14121417
String buf;

0 commit comments

Comments
 (0)