none
IPAddress.TryParse() inconsistent behavior with 0 padded values on certain IP Addresses

    Question

  • I believe I have found a bug or at least inconsistency with IPAddress.TryParse and Parse methods.

    I am using System.Net.IPAddress.TryParse() to test if a string is a valid IP.

    If I pass in an ip string with an octet that contains an 8 or 9 padded with 1 or 2 zeros it returns false.

    See Code Example Below. I tagged the failure cases with //FAILS all other cases obviously pass.

    IPAddress ipAddress;
          bool test = false;
    
          //basic tests
          test = IPAddress.TryParse("000.000.000.001", out ipAddress);
          test = IPAddress.TryParse("000.000.000.002", out ipAddress);
          test = IPAddress.TryParse("000.000.000.003", out ipAddress);
          test = IPAddress.TryParse("000.000.000.004", out ipAddress);
          test = IPAddress.TryParse("000.000.000.005", out ipAddress);
          test = IPAddress.TryParse("000.000.000.006", out ipAddress);
          test = IPAddress.TryParse("000.000.000.007", out ipAddress);
          test = IPAddress.TryParse("000.000.000.008", out ipAddress);//FAILS
          test = IPAddress.TryParse("000.000.000.009", out ipAddress);//FAILS
          test = IPAddress.TryParse("000.000.000.010", out ipAddress);
          test = IPAddress.TryParse("000.000.000.011", out ipAddress);
    
          //other padded tests
          test = IPAddress.TryParse("000.000.000.08", out ipAddress);//FAILS
          test = IPAddress.TryParse("000.000.000.8", out ipAddress);
    
          test = IPAddress.TryParse("000.000.008.006", out ipAddress);//FAILS
          test = IPAddress.TryParse("000.000.008.000", out ipAddress);//FAILS
    
          test = IPAddress.TryParse("000.000.8.006", out ipAddress);
          test = IPAddress.TryParse("000.000.8.000", out ipAddress);
    
          test = IPAddress.TryParse("000.009.000.006", out ipAddress);//FAILS
          test = IPAddress.TryParse("000.009.000.000", out ipAddress);//FAILS
    
          test = IPAddress.TryParse("000.000.007.006", out ipAddress);
          test = IPAddress.TryParse("000.000.007.000", out ipAddress);
    
          //Verify that IPAddress.Parse() also fails the same way... It does     
          try
          {
            IPAddress testip = IPAddress.Parse("000.000.000.008");//FAILS - throws exception, as expected since TryParse() does not work
          }
          catch (Exception ex)
          {
            Console.WriteLine("Failed!  "+ex.StackTrace);
          }
    

    • Windows XP SP3
    • Visual Studio 2008
    • .Net Framework 3.5

    I am working around this by trimming leading zeros but thanks for any input and/or thoughts!!!

    -Brian

    Tuesday, May 24, 2011 6:55 PM

Answers

  • The reason is that IPAddress.[Try]Parse adhere to the standard for inet_addr which allows you to use numbers in decimal, octal and hexadecimal form. A leading zero indicates an octal number, while a leading 0x indicates an hexadecimal one. So, the following strings:

    "16.16.16.16"
    "020.020.020.020"
    "0x10.0x10.0x10.0x10"

    will all be parsed to the same address (16.16.16.16).

    The failure you are getting is due to the fact that the leading zero indicates an octal string, and neither '8' nor '9' are valid symbols. Unfortunately you could encounter a worse failure by not removing the leading zeros: as show in the example above, "020" does NOT parse to 20, but to 16.

    As this behavior is dictated by a standard, I doubt it can be considered a bug. All you can do is sanitize the input yourself; a regex would probably work nicely in this case.

    Sorry for the bad news
    --mc

    • Marked as answer by BrianSwEng Wednesday, May 25, 2011 6:00 PM
    Wednesday, May 25, 2011 5:55 PM

All replies

  • The reason is that IPAddress.[Try]Parse adhere to the standard for inet_addr which allows you to use numbers in decimal, octal and hexadecimal form. A leading zero indicates an octal number, while a leading 0x indicates an hexadecimal one. So, the following strings:

    "16.16.16.16"
    "020.020.020.020"
    "0x10.0x10.0x10.0x10"

    will all be parsed to the same address (16.16.16.16).

    The failure you are getting is due to the fact that the leading zero indicates an octal string, and neither '8' nor '9' are valid symbols. Unfortunately you could encounter a worse failure by not removing the leading zeros: as show in the example above, "020" does NOT parse to 20, but to 16.

    As this behavior is dictated by a standard, I doubt it can be considered a bug. All you can do is sanitize the input yourself; a regex would probably work nicely in this case.

    Sorry for the bad news
    --mc

    • Marked as answer by BrianSwEng Wednesday, May 25, 2011 6:00 PM
    Wednesday, May 25, 2011 5:55 PM
  • Thanks for the reply Mario,

    That makes sense, I did not see that described anywhere in the documentation unless I missed it. Glad to know that it is just my usage!

     

    Insight is much appreciated!!!

    -Brian

    • Proposed as answer by Vickie Liu Thursday, May 26, 2011 3:26 AM
    • Unproposed as answer by Vickie Liu Thursday, May 26, 2011 3:27 AM
    Wednesday, May 25, 2011 6:03 PM
  • Yes, that's unfortunate. I added a comment to the relevant MSDN pages, let's hope it's enough to save some headaches in the future.

    --mc

    Wednesday, May 25, 2011 7:16 PM