Bug #84118 Escaping of \ and % in LIKE causes weird matches
Submitted: 8 Dec 2016 15:52 Modified: 8 Dec 2016 20:51
Reporter: Paul Clark Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DML Severity:S3 (Non-critical)
Version:5.6/5.7/8.0 OS:Ubuntu (16.04)
Assigned to: CPU Architecture:Any
Tags: escaping, like, wildcards

[8 Dec 2016 15:52] Paul Clark
Description:
Using \ in LIKE to escape \ itself and the wildcard % causes odd results when matching against fields containing \ and %.

'\\%' acts as if it was '\\\%'
'\\\%' acts as if it was '\\%'
'\\\\%' acts as if it was '\\%'
'\\\\\%' acts as if it was '\\\%'

The latter two look like double-unescaping but I can find no rhyme nor reason in the first two at all :-)

How to repeat:
-- in any old database

drop table if exists foo;
create table foo
(
  id varchar(255)
);

insert into foo values('bar%');
insert into foo values('bar%1');
insert into foo values('bart');
insert into foo values('bar\\t');
insert into foo values('bar\\%');

-- Wildcard % - fetches all
select id from foo where id like 'bar%';
-- OK

-- Straight escaped % - just bar%
select id from foo where id like 'bar\%';
-- OK

-- Escaped \ then wildcard % - should fetch bar\t and bar\%
select id from foo where id like 'bar\\%';
-- ERROR: Only fetches bar\%

-- Escaped \ then escaped % - should fetch bar\%
select id from foo where id like 'bar\\\%';
-- ERROR: Fetches both bar\t and bar\% as one before should have done

-- Two escaped \ then wildcard % - shouldn't fetch anything
select id from foo where id like 'bar\\\\%';
-- ERROR: Fetches both bar\t and bar\%

-- Two escaped \ then escaped % - shouldn't fetch anything
select id from foo where id like 'bar\\\\\%';
-- ERROR: Fetches bar\%
[8 Dec 2016 20:51] MySQL Verification Team
Thank you for the bug report. Verified as described.