Possible bug in NullReference analysis

I encountered some code today that appears to be exposing a bug in the NullReference analysis in ReSharper 2.5.1. I have attached a screenshot of the code with the possible NullReference warnings. You will notice, that if the urlString parameter is null, then the length variable will be 0, and thus the for loop block will never be entered.

I concede that this is a rather complicated case, but I thought it was worth posting. This code was actually ripped out of a private method in the System.Web.dll assembly by using the dotNet Reflector.

Regards,

~Andy Alm



Attachment(s):
BogusNullReferenceWarning.png
3 comments
Comment actions Permalink

Hello, Andrew,

Thank you for your feedback. The code you posted really exposes not a bug in NullReference analysis, but the fact that it could be made more agressive/precise. Indeed, if Resharper knew that the effect of assignment to length is not only the fact that urlString could be null, but also that length is made zero in this case, it would probably be able to infer the inpossibility of NRE in your code. However, doing so would require too heavy analysis with too much resources consumpted, so we probably won't do it (until Intel makes a really good progress:) Note that you circumvent the error by just surrounding the loop with 'if (urlString != null)' check that willl make your code clearer IMHO anyway.

Regards,
Eugene.

P.S. I'm a new kid in Resharper town, so there is a good chance I'm not 100% correct in my judgements.

0
Comment actions Permalink

Eugene,

I suggest if( urlString == null ) return nv;

This will definitely make the code clearer.

--
Sergey V. Coox
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


0
Comment actions Permalink

Hello Eugene,

Thanks for your feedback. The code is definitely very subtle, and if it
weren't for the fact that ReSharper is frequently surprising me with all
the subtle things it does pick up on, I would not have bothered posting it.
But I definitely understand the need to balance complexity and performance.
I also agree that this code could be made much clearer (like I said, it
was ripped directly from dotNet Reflector). This is certainly an easy thing
to work around. Thanks for taking a look and responding.

~Andy

Hello, Andrew,

Thank you for your feedback. The code you posted really exposes not a
bug in NullReference analysis, but the fact that it could be made more
agressive/precise. Indeed, if Resharper knew that the effect of
assignment to length is not only the fact that urlString could be
null, but also that length is made zero in this case, it would
probably be able to infer the inpossibility of NRE in your code.
However, doing so would require too heavy analysis with too much
resources consumpted, so we probably won't do it (until Intel makes a
really good progress:) Note that you circumvent the error by just
surrounding the loop with 'if (urlString != null)' check that willl
make your code clearer IMHO anyway.

Regards,
Eugene.
P.S. I'm a new kid in Resharper town, so there is a good chance I'm
not 100% correct in my judgements.



0

Please sign in to leave a comment.