{"record":{"id":"06f68073f40a6101","repo":"microsoft/aspire","slug":"invalid-cidr-notation-cidr","errorCode":null,"errorMessage":"Invalid CIDR notation: '{cidr}'.","messagePattern":"Invalid CIDR notation: '(.+?)'\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Sql/SubnetAddressAllocator.cs","lineNumber":102,"sourceCode":"    private static bool OverlapsAny(uint start, uint end, List<(uint Start, uint End)> ranges)\n    {\n        foreach (var (rStart, rEnd) in ranges)\n        {\n            if (start <= rEnd && rStart <= end)\n            {\n                return true;\n            }\n        }\n\n        return false;\n    }\n\n    internal static (uint Start, uint End) ParseCidr(string cidr)\n    {\n        var parts = cidr.Split('/');\n        if (parts.Length != 2 || !int.TryParse(parts[1], out var prefix) || prefix < 0 || prefix > 32)\n        {\n            throw new FormatException($\"Invalid CIDR notation: '{cidr}'.\");\n        }\n\n        var ip = IPAddress.Parse(parts[0]);\n        if (ip.AddressFamily != AddressFamily.InterNetwork)\n        {\n            throw new FormatException($\"Only IPv4 CIDR notation is supported: '{cidr}'.\");\n        }\n\n        var bytes = ip.GetAddressBytes();\n        var address = (uint)((bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]);\n\n        // Compute the network mask\n        var mask = prefix == 0 ? 0u : uint.MaxValue << (32 - prefix);\n        var networkAddress = address & mask;\n        var broadcastAddress = networkAddress | ~mask;\n\n        return (networkAddress, broadcastAddress);\n    }","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Sql/SubnetAddressAllocator.cs#L84-L120","documentation":"ParseCidr validates CIDR strings before computing address ranges. It throws FormatException when the string does not split into exactly two parts around '/' or when the prefix length is not an integer between 0 and 32. This guards against malformed or non-IPv4-style CIDR input from configuration or user code.","triggerScenarios":"Passing a string like '10.0.0.0' (no prefix), '10.0.0.0/abc', '10.0.0.0/33', '10.0.0.0/-1', or any text with extra slashes into AllocateDeploymentScriptSubnet or range parsing that calls ParseCidr.","commonSituations":"Typos in vnet/subnet address prefixes, values sourced from environment variables or Bicep outputs with unexpected content, copying IPv6 or bare-IP strings instead of CIDR notation.","solutions":["Supply a valid IPv4 CIDR string of the form 'a.b.c.d/0-32', e.g. '10.0.0.0/16'","Trim whitespace and remove stray slashes from configuration values","Validate the prefix length is an integer in [0, 32] before calling the API"],"exampleFix":"// before\nallocator.AllocateDeploymentScriptSubnet(vnet, \"10.0.0.0\"); // FormatException\n// after\nallocator.AllocateDeploymentScriptSubnet(vnet, \"10.0.0.0/16\");","handlingStrategy":"validation","validationCode":"bool IsValidCidr(string cidr) {\n    var parts = cidr.Split('/');\n    return parts.Length == 2 && int.TryParse(parts[1], out var p) && p is >= 0 and <= 32\n        && System.Net.IPAddress.TryParse(parts[0], out _);\n}","typeGuard":null,"tryCatchPattern":"try { allocator.AllocateDeploymentScriptSubnet(vnet, cidr); }\ncatch (FormatException ex) { logger.LogError(ex, \"Invalid CIDR {Cidr}\", cidr); throw new ConfigurationException(...); }","preventionTips":["Store address prefixes as validated CIDR constants, not free-form strings","Trim and sanitize CIDR values from environment variables or Bicep outputs","Unit-test configuration parsing with malformed CIDR cases"],"tags":["azure","networking","cidr","format","validation"],"backgroundTag":"invalid-argument-format","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}