Possible bug in VB.NET 'Like' operator?

2019-04-05 17:10发布

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

1条回答
Emotional °昔
2楼-- · 2019-04-05 17:30

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

查看更多
登录 后发表回答