microsoft/aspire · error · FormatException

Invalid CIDR notation

Error message

Invalid CIDR notation: '{cidr}'.

What it means

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.

Solutions

  1. Supply a valid IPv4 CIDR string of the form 'a.b.c.d/0-32', e.g. '10.0.0.0/16'
  2. Trim whitespace and remove stray slashes from configuration values
  3. Validate the prefix length is an integer in [0, 32] before calling the API

Example fix

// before
allocator.AllocateDeploymentScriptSubnet(vnet, "10.0.0.0"); // FormatException
// after
allocator.AllocateDeploymentScriptSubnet(vnet, "10.0.0.0/16");
Defensive patterns

Strategy: validation

Validate before calling

bool IsValidCidr(string cidr) {
    var parts = cidr.Split('/');
    return parts.Length == 2 && int.TryParse(parts[1], out var p) && p is >= 0 and <= 32
        && System.Net.IPAddress.TryParse(parts[0], out _);
}

Try / catch

try { allocator.AllocateDeploymentScriptSubnet(vnet, cidr); }
catch (FormatException ex) { logger.LogError(ex, "Invalid CIDR {Cidr}", cidr); throw new ConfigurationException(...); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/06f68073f40a6101. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Azure.Sql/SubnetAddressAllocator.cs:102

    private static bool OverlapsAny(uint start, uint end, List<(uint Start, uint End)> ranges)
    {
        foreach (var (rStart, rEnd) in ranges)
        {
            if (start <= rEnd && rStart <= end)
            {
                return true;
            }
        }

        return false;
    }

    internal static (uint Start, uint End) ParseCidr(string cidr)
    {
        var parts = cidr.Split('/');
        if (parts.Length != 2 || !int.TryParse(parts[1], out var prefix) || prefix < 0 || prefix > 32)
        {
            throw new FormatException($"Invalid CIDR notation: '{cidr}'.");
        }

        var ip = IPAddress.Parse(parts[0]);
        if (ip.AddressFamily != AddressFamily.InterNetwork)
        {
            throw new FormatException($"Only IPv4 CIDR notation is supported: '{cidr}'.");
        }

        var bytes = ip.GetAddressBytes();
        var address = (uint)((bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]);

        // Compute the network mask
        var mask = prefix == 0 ? 0u : uint.MaxValue << (32 - prefix);
        var networkAddress = address & mask;
        var broadcastAddress = networkAddress | ~mask;

        return (networkAddress, broadcastAddress);
    }

View on GitHub (pinned to 25830f84bd)