Description:
When the INSERT(str, pos, len, newstr) function is used with the RLIKE operator, it behaves incorrectly if the str argument is supplied by the non-deterministic function CURRENT_USER() and the pos argument is an invalid position (e.g., a negative number). The expected behavior is for INSERT() to return the original string. However, in this scenario, it incorrectly returns the value of the fourth argument (newstr), '0'. This causes RLIKE to match against an incorrect pattern, leading to a wrong result. The bug is not triggered when an equivalent string literal (' root@ %') is used instead of the CURRENT_USER() function.
How to repeat:
CREATE TABLE t18 (c1 FLOAT, c2 LONGTEXT);
INSERT t18 () VALUES (1,'uM');
INSERT t18 () VALUES (-1,'30');
SELECT CURRENT_USER();
-- 'root@%' or similar
SELECT INSERT((CURRENT_USER()),c1, 9, '0') from t18;
-- Observed:
-- '0'
-- '0' <- Incorrect
SELECT ((t18.c2) RLIKE INSERT((CURRENT_USER()), t18.c1, 9, '0')) FROM t18;
-- Observed:
-- 0
-- 1 <- Incorrect
SELECT ((t18.c2) RLIKE INSERT(('root@%'), t18.c1, 9, '0')) FROM t18;
-- Observed:
-- 0
-- 0 <- Correct
Description: When the INSERT(str, pos, len, newstr) function is used with the RLIKE operator, it behaves incorrectly if the str argument is supplied by the non-deterministic function CURRENT_USER() and the pos argument is an invalid position (e.g., a negative number). The expected behavior is for INSERT() to return the original string. However, in this scenario, it incorrectly returns the value of the fourth argument (newstr), '0'. This causes RLIKE to match against an incorrect pattern, leading to a wrong result. The bug is not triggered when an equivalent string literal (' root@ %') is used instead of the CURRENT_USER() function. How to repeat: CREATE TABLE t18 (c1 FLOAT, c2 LONGTEXT); INSERT t18 () VALUES (1,'uM'); INSERT t18 () VALUES (-1,'30'); SELECT CURRENT_USER(); -- 'root@%' or similar SELECT INSERT((CURRENT_USER()),c1, 9, '0') from t18; -- Observed: -- '0' -- '0' <- Incorrect SELECT ((t18.c2) RLIKE INSERT((CURRENT_USER()), t18.c1, 9, '0')) FROM t18; -- Observed: -- 0 -- 1 <- Incorrect SELECT ((t18.c2) RLIKE INSERT(('root@%'), t18.c1, 9, '0')) FROM t18; -- Observed: -- 0 -- 0 <- Correct