Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible bug in VB.NET 'Like' operator?

Why is it that the following evaluates as True?

Dim result = "b" Like "*a*b"

Thanks.

EDIT:
To generalize this a bit, the following returns True:

"String1" Like "*AnyText1*AnyText2*AnyText???******????*String1"

VBA works correctly, returning False.
PowerShell works correctly, returning False:

PS C:\Users\XXX> "b" -Like "*a*b"
False

EDIT2:
The link to the bug report:
https://connect.microsoft.com/VisualStudio/feedback/details/748415/a-bug-in-net-like-operator

like image 972
mcu Avatar asked Jun 10 '12 19:06

mcu


1 Answers

I decided, for fun, to open up ilspy to debug this :-)

in this method;

    private static void MatchAsterisk(string Source, int SourceLength, int SourceIndex, LigatureInfo[] SourceLigatureInfo, string Pattern, int PatternLength, int PatternIndex, LigatureInfo[] PattternLigatureInfo, ref bool Mismatch, ref bool PatternError, CompareInfo Comparer, CompareOptions Options)

for this condition

                if (SourceLength <= 0)
                {
                    return;
                }

by changing it to

                if (SourceLength < 0)
                {
                    return;
                }

it seem to be make it work as intended

I only did a few small test, nothing big

the issue is; it is only looking at the LAST asterisk and when it was valid, was stopping right there

my small change make sure to check the previous one, or anything in fact before it

with my fix

 Dim result = "b" Like "*a*b"
 now return false

 "String1" Like "*AnyText1*AnyText2*AnyText???******????*String1"
 now return false

but does return true when it need to return true

like image 152
Fredou Avatar answered Sep 22 '22 18:09

Fredou