DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Posted Tue, Apr 4 2006 3:40 by bill
UPDATE: This bug was fixed in .NET 2.0 SP1
940900  (http://support.microsoft.com/kb/940900/ ) FIX: You receive the NullReferenceException exception when you call the String.IsNullOrEmpty function in an application that is built on the .NET Framework 2.0
I saw a little talk in the blogshpere about using String.IsNullOrEmpty in .NET 2.0, so I thought, just for fun I would run it through a series of tests.  Unfortunately in doing so I found a very serious bug with it.  The following C# code shows the problem:
classProgram
{
 
static void Main(string[] args)
{
   Console.WriteLine("starting");
   test(null);
   Console.WriteLine("finished");
   Console.ReadLine();
}
static void test(string x)
{
   for (int j = 0; j < 10; j++)
   {
      if (String.IsNullOrEmpty(x))
     {
        //TODO:
      }
   }
}
}
 
 
Have you spotted the problem ?  The astute of you are probably saying "yeh it's in that lame language C#", and although you'd be right, that's not the actual gotchya here.
 
The problem is when you compile this application as a release build with optimizations on and run it from outside of the IDE.  Note from inside the IDE all seems fine.  but when run form outside this will lead to a null exception being thrown at runtime.  Try it for yourselves !
 
Now in all fairness, this is actually a JIT optimization problem that impacts both VB and C#.  Vb.NET does provide a pretty good alternative syntax that does not have this issue, via the:
   If x = Nothing syntax. 
Of course in both VB and C# you could use the:
 If x Is Nothing OrElse x.Length= 0 Then
approach, but that seems to loose a bit in the readability.
 
So String.IsNullOrEmpty can cause unexpected runtime errors that you won't detect when running from inside the IDE.  I recommend using extreme CAUTION when using this method.
 
 
Filed under: ,

Comments

# WARNING: String.IsNullOrEmpty

Tuesday, April 04, 2006 9:36 AM by Richard Clark

Actuellement, tjs pour VB Code Snippet Editor, Bill essaye d'optimiser la compilation en C#, et il est...

# Watch out for those Nulls.

Tuesday, April 04, 2006 3:45 PM by Public Class GeoffAppleby

Bill McCarthy found a very weird bug today. It seems that if you craft some code just right and do a...

# .Net 2.0: String.IsNullOrEmpty causes runtime exception

Tuesday, April 04, 2006 4:50 PM by Anatoly Lubarsky

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Wednesday, April 05, 2006 12:40 AM by Ayende Rahien

Would you mind sharing exactly why this is happening.

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Wednesday, April 05, 2006 12:49 AM by bill

The JIT team hasn't officially said yet. My guess is that the JIT is removing the check for null on the parameter being passed to String.IsNullOrEmpty as this is often superfulous

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Wednesday, April 05, 2006 12:56 AM by Ayende Rahien

This doesn't make sense.
The IsNullOrEmpty is a static method that is built correctly.
The exception is not caused on the IsNullOrEmpty, but on the test() method.
On the for() line, as a matter of fact, and I can't understand why.

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Wednesday, April 05, 2006 1:24 AM by Kalpesh

Well, this might sound stupid.

But, can you assign null to a string explicitly?

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Wednesday, April 05, 2006 1:34 AM by bill

Kalpesh, yes fo course you can assign a null to a string as it is a reference type.

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Wednesday, April 05, 2006 1:36 AM by bill

Ayende,

I beleive that the removal of the code that checks for null is the likely suspect. It's that kind of optimisation that the JIT looks for when you allow it to optimise the code. Rember the JIT can inline code from other assemblies if it thinks it should.

I have bugged the issue on MSDN feedback:
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=a184aa36-55d5-45d3-a308-9c2240a175fe

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Wednesday, April 05, 2006 11:13 AM by Darren Kopp

very interesting. and to think that when you asked "have you spotted the problem?" all i could see was an if statement and a method call within a for loop :)

c# rules!

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Wednesday, April 05, 2006 12:41 PM by Jonathan de Halleux

I am the tester for the JIT. We are looking at the problem since it is most likely a bug on our side.

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Wednesday, April 05, 2006 6:34 PM by bill

Thanks Jonathan. I look forward to finding out why this happens.

# re: Null, Nothing, Empty

Thursday, April 06, 2006 11:55 AM by XTab's Blog

# Be careful with String.IsNullOrEmpty

Tuesday, September 19, 2006 4:07 AM by Jean-Paul Smit

While I ran FxCop over someone else's code it pointed me to a String method I didn't know about: IsNullOrEmpty

# Thomas goes .NET - String.IsNullOrEmpty() schmei??t Exceptions

PingBack from http://blog.thomasbandt.de/PermaLink,guid,d6de00ed-ab10-485a-bda4-615e53eb3981.aspx

# String.IsNullOrEmpty can lead to runtime Null exceptions!

Sunday, June 17, 2007 9:23 AM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# String.IsNulOrEmpty Flaw

Monday, June 18, 2007 11:12 AM by Dityo Nurasto Journal

String.IsNulOrEmpty Flaw

# Okay this is nice, trust me | Suddenelfilio 's Blog

Wednesday, June 20, 2007 2:45 AM by Okay this is nice, trust me | Suddenelfilio 's Blog

Pingback from  Okay this is nice, trust me | Suddenelfilio 's Blog

# dangerous behaviour of String.IsNullOrEmpty at www2.sitina.net

Pingback from  dangerous behaviour of String.IsNullOrEmpty at  www2.sitina.net

# Bug en String.IsNullOrEmpty en .NET 2.0

Sunday, July 29, 2007 6:58 PM by vtortola

Leo en un blog de MSMVPs.com que se ha detectado un bug en el método estático String.IsNullOrEmpty causado

# El bug de String.IsNullOrEmpty en .NET 2.0

Monday, July 30, 2007 2:11 PM by vtortola

Leo en un blog de MSMVPs.com una noticia de hace más de un año que contaba que se detecto un bug en el

# Controlling the usage of libraries

Monday, October 20, 2008 12:48 PM by Patrick Smacchia [MVP C#]

Recently, Scott Hanselman has proposed a survey to know which part of the .NET real-world developers

# Controlling the usage of libraries

Monday, October 20, 2008 1:26 PM by Community Blogs

Recently, Scott Hanselman has proposed a survey to know which part of the .NET Framework real-world developers

# String.IsNullOrEmpty - performance considerations, bugs

Thursday, November 20, 2008 10:07 AM by Technical Musings

I once read that .Net's String.IsNullOrEmpty performs better, and is safer, than just checking to see if a particular string has an empty value. I.e. replace if (myString == "") or if (myString.Equals(string.Empty)) or if (myString.Length == 0) with if