{"record":{"id":"783c0ec552962ea1","repo":"stride3d/stride","slug":"the-alignment-must-be-a-positive-power-of-2","errorCode":null,"errorMessage":"The alignment must be a positive power of 2.","messagePattern":"The alignment must be a positive power of 2\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/MemoryUtilities.cs","lineNumber":366,"sourceCode":"\n\n    /// <summary>\n    ///   Swaps two values.\n    /// </summary>\n    /// <typeparam name=\"T\">The type of the values to swap.</typeparam>\n    /// <param name=\"left\">The left value.</param>\n    /// <param name=\"right\">The right value.</param>\n    public static void Swap<T>(ref T left, ref T right)\n    {\n        (right, left) = (left, right);\n    }\n\n    #region Throw helpers\n\n    [DoesNotReturn]\n    private static bool ThrowAlignmentNotPowerOfTwo(int align, [CallerArgumentExpression(nameof(align))] string? paramName = null)\n    {\n        throw new ArgumentException(\"The alignment must be a positive power of 2.\", paramName);\n    }\n\n    #endregion\n}\n","sourceCodeStart":348,"sourceCodeEnd":371,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/MemoryUtilities.cs#L348-L371","documentation":"MemoryUtilities.ThrowAlignmentNotPowerOfTwo is a [DoesNotReturn] helper that throws ArgumentException(\"The alignment must be a positive power of 2.\") with the failing expression as paramName. It is invoked by Allocate and IsAligned when the align parameter is not a positive power of two (must satisfy align > 0 and (align & (align - 1)) == 0). Native/unaligned memory allocation and alignment checks require valid power-of-two alignment.","triggerScenarios":"Calling MemoryUtilities.Allocate(size, align) or IsAligned(pointer, align) with align <= 0, or a non-power-of-two value like 3, 6, 24, or 100. Alignment values not representable as 2^n are rejected regardless of size.","commonSituations":"Passing sizeof(SomeStruct) or Marshal.SizeOf results as alignment (e.g. 12 or 24); hard-coding an alignment like 16 bytes but multiplying it by a SIMD width; configuration values from users that are not validated as powers of two.","solutions":["Round the alignment up to the next power of two before calling, or use a fixed valid constant (1, 2, 4, 8, 16, 32, 64...)","Validate input with (align > 0 && (align & (align - 1)) == 0) before invoking Allocate/IsAligned","Replace struct-size-derived alignment with Math.BitOperations.RoundUpToPowerOf2 or a hardcoded natural alignment","Check config/CLI sources supplying the alignment and clamp/validate them at load time"],"exampleFix":"// before\nvar ptr = MemoryUtilities.Allocate(size, sizeof(MyStruct)); // e.g. 12 -> throws\n// after\nint align = (int)BitOperations.RoundUpToPowerOf2((uint)sizeof(MyStruct)); // 16\nvar ptr = MemoryUtilities.Allocate(size, align);","handlingStrategy":"validation","validationCode":"static bool IsPowerOfTwo(int align) => align > 0 && (align & (align - 1)) == 0;\nif (!IsPowerOfTwo(align)) throw new ArgumentOutOfRangeException(nameof(align));","typeGuard":"static bool IsValidAlignment(int align) =>\n    int.IsPow2(align) && align > 0;","tryCatchPattern":"try\n{\n    var ptr = MemoryUtilities.Allocate(size, align);\n}\ncatch (ArgumentException ex) when (ex.ParamName == nameof(align))\n{\n    // align was not a positive power of two: round up and retry\n}","preventionTips":["Use fixed constants (1, 2, 4, 8, 16, 32, 64) for alignments","Round up computed alignments with BitOperations.RoundUpToPowerOf2","Validate user/config-supplied alignment values at load time","Do not pass sizeof(T)/Marshal.SizeOf results directly as alignment"],"tags":["csharp","memory","alignment","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}