microsoft/garnet · error · FormatException

Invalid character '{c}' in AofAddress string.

Error message

Invalid character '{c}' in AofAddress string.

What it means

Thrown by AofAddress.FromString(input) when parsing a comma-separated list of decimal integers and encountering a character that is not a digit (0-9), a comma separator, or a minus sign. This is a standard System.FormatException indicating the input string does not conform to the expected address-list grammar.

Source

Thrown at libs/server/AOF/AofAddress.cs:181

            for (var i = 0; i < span.Length; i++)
            {
                var c = span[i];
                if (c == ',')
                {
                    aofAddress[idx++] = value;
                    value = 0;
                }
                else if (c >= '0' && c <= '9')
                {
                    value = value * 10 + (c - '0');
                }
                else if (c == '-')
                {
                    negative = true;
                }
                else
                {
                    throw new FormatException($"Invalid character '{c}' in AofAddress string.");
                }
            }

            // Handle last value
            aofAddress[idx] = value * (negative ? -1 : 1);
            return aofAddress;
        }

        /// <summary>
        /// Serialize contents using provided BinaryWriter
        /// </summary>
        /// <param name="writer"></param>
        public void Serialize(BinaryWriter writer)
        {
            writer.Write(length);
            for (var i = 0; i < Length; i++)
                writer.Write(addresses[i]);
        }

View on GitHub (pinned to 951b0fc683)

Solutions

  1. Ensure the input string contains only digits, commas, and optional minus signs (e.g. "100,200,-1").
  2. Pre-validate with a regex such as ^-?\d+(,-?\d+)*$ before calling FromString.
  3. If the input comes from a config file, document the expected format explicitly.

Example fix

// before
var addr = AofAddress.FromString("100; 200; 300");

// after
var addr = AofAddress.FromString("100,200,300");
Defensive patterns

Strategy: validation

Validate before calling

using System.Text.RegularExpressions;
if (!Regex.IsMatch(input, @"^-?\d+(,-?\d+)*$"))
    throw new FormatException($"Input '{input}' is not a valid comma-separated integer list.");

Type guard

static bool IsValidAofAddressString(string input) =>
    !string.IsNullOrEmpty(input) &&
    Regex.IsMatch(input, @"^-?\d+(,-?\d+)*$");

Try / catch

try { var addr = AofAddress.FromString(input); }
catch (FormatException ex) { logger.LogError("Invalid AofAddress input: {Msg}", ex.Message); throw; }

Prevention

When it happens

Trigger: Calling AofAddress.FromString with input like "100;200" (semicolon), "100.5" (decimal point), "0x100" (hex), "100 200" (space), or any string containing letters, whitespace, or punctuation other than ',' and '-'. The parser only accepts integer literals and '-' for negative values.

Common situations: Deserializing a user-supplied or config-supplied AOF address string with the wrong delimiter; copy-pasting an address representation from a different format; locale-specific number formatting leaking in.

Understand the failure class

Related errors


AI-assisted analysis of microsoft/garnet@951b0fc683 (2026-08-13). Data as JSON: /api/errors/478b20884053d508. Report an issue: GitHub.