The Daily WTF
Follow
CodeSOD: How to Validate an IP Address
Andy is concerned about future-proofing a C# code snippet that validates IP addresses. The code splits the input IP string by dots and checks if there are four parts, throwing an exception if not. Andy believes IPv6 will never be widely used, but the code has a larger issue: it only checks for four parts, which could lead to incorrect validation of IP addresses with more or fewer bits than IPv4. The code could mistakenly validate an IP like 555.192.168.0.1 as correct. C# already has a built-in function for parsing IP addresses that does it correctly. Andy's code could be useful for validating IP addresses with 40 bits, potentially creating a bridge between IPv4 and IPv6. However, the code's approach is problematic due to its simplicity. IPv6 has 128 bits of addresses, offering much more capacity than IPv4's 32 bits. Andy's concerns highlight the importance of considering different IP address formats and using built-in functions for validation.