stride3d/stride · error · ArgumentException

Invalid length for FourCC

Error message

Invalid length for FourCC("{0}". Must be be 4 characters long 

What it means

Thrown by the FourCC(string) constructor when the input string is not exactly 4 characters. A FourCC is by definition a 4-byte code, so any other length is invalid. Note the message text itself has typos (missing closing paren, doubled 'be').

Solutions

  1. Ensure the string is exactly 4 characters before constructing (pad with spaces or '\0' as the format requires)
  2. Check the intended code: e.g. DDS uses "DDS ", use the canonical constant rather than typing it
  3. Validate input from config/files with a length check and trim or reject invalid tags

Example fix

// before
var fourCC = new FourCC(formatTag); // "DDS"
// after
if (formatTag.Length != 4) formatTag = formatTag.PadRight(4, ' ');
var fourCC = new FourCC(formatTag); // "DDS "
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(fourCC) || fourCC.Length != 4)
    throw new ArgumentException($"FourCC must be exactly 4 chars, got '{fourCC}'", nameof(fourCC));

Type guard

bool IsValidFourCC(string s) => !string.IsNullOrEmpty(s) && s.Length == 4;

Try / catch

FourCC fourCC;
try
{
    fourCC = new FourCC(tag);
}
catch (ArgumentException ex)
{
    log.Error($"Invalid FourCC tag '{tag}': must be exactly 4 characters", ex);
    throw new FormatException($"Bad format tag '{tag}'");
}

Prevention

When it happens

Trigger: new FourCC("DDS") (3 chars), new FourCC("") (empty), new FourCC("DXGI ") or any string whose .Length != 4, e.g. format names read from config or parsed from headers.

Common situations: Parsing format tags from files or user config where trailing whitespace or a truncated tag slips in; hardcoding a 3-letter mnemonic like "DDS" or "PNG" instead of the real 4-char code ("DDS ", "PNG\0"); string slicing off by one.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/243535b74ddf75f9. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Foundation/Graphics/FourCC.cs:47

namespace Stride
{
    /// <summary>
    /// A FourCC descriptor.
    /// </summary>
    [DataSerializer(typeof(FourCC.Serializer))]
    [StructLayout(LayoutKind.Sequential, Size = 4)]
    internal struct FourCC : IEquatable<FourCC>
    {
        private readonly uint value;

        /// <summary>
        /// Initializes a new instance of the <see cref="FourCC" /> struct.
        /// </summary>
        /// <param name="fourCC">The fourCC value as a string .</param>
        public FourCC(string fourCC)
        {
            if (fourCC.Length != 4)
                throw new ArgumentException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Invalid length for FourCC(\"{0}\". Must be be 4 characters long ", fourCC), "fourCC");
            this.value = ((uint)fourCC[3]) << 24 | ((uint)fourCC[2]) << 16 | ((uint)fourCC[1]) << 8 | ((uint)fourCC[0]);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FourCC" /> struct.
        /// </summary>
        /// <param name="byte1">The byte1.</param>
        /// <param name="byte2">The byte2.</param>
        /// <param name="byte3">The byte3.</param>
        /// <param name="byte4">The byte4.</param>
        public FourCC(char byte1, char byte2, char byte3, char byte4)
        {
            this.value = ((uint)byte4) << 24 | ((uint)byte3) << 16 | ((uint)byte2) << 8 | ((uint)byte1);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FourCC" /> struct.
        /// </summary>

View on GitHub (pinned to 96fad776d2)