Schlagwort-Archive: Octal

Slippery When Wet #8: Parsing IP addresses in C#

I proudly present to you the eighth in a infinite number of posts of “Slippery When Wet.” In these posts I show you a little bastard I stumbled on.

When I was trying to read some IP addresses from a CSV file I parsed them with the method IPAddress.Parse() from the System.Net namespace. Everything worked fine up to the eighth ip address. There the Parse method threw a System.FormatException: The format of the IP address was not valid. The IP address that caused the problem looked like ‚127.000.000.008‘. In my opinion this looked like a valid number, although it could be written a little bit shorter.

But Microsoft sees the world a little bit different. According to the answer by Mario Cossi on the problem description I found on social.msdn.microsoft.com this behaviour is completly valid, as all the numbers with leading zeros should be considered as octal numbers and therefore 008 is not valid, as 8 in not in the range of octal numbers. Mario writes that this is defined by standards, but does not refere to the standard where this is defined.

I did some more research but could not find the standard that describes this behaviour. What I did find were some older RFCs (like RFC 790) where IP addresses (or ranges) were written in the format with the leading zeros.

According to a wikipedia article and a therein referenced draft there is no defined standard for the textual representation of a IPv4 address.

In my opinion, the design decision of Microsoft is not the best. At least there should be a possibility to set the expected behaviour in case of a leading zero octet. Especially as the IP address 010.010.010.010 could be interpreted as 10.10.10.10 or 8.8.8.8 and you would not get an error as it would be in octal as well as in decimal a valid number.

As Microsoft did not give this possibility, according to your data you have to read, you should possibily use your own parsing method instead of the built-in one.