{"record":{"id":"a4f0a74dcf3cb49c","repo":"stride3d/stride","slug":"not-enough-size-for-destination-buffer","errorCode":null,"errorMessage":"Not enough size for destination buffer","messagePattern":"Not enough size for destination buffer","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Foundation/Graphics/DDSHelper.cs","lineNumber":625,"sourceCode":"                        break;\n                    case PixelFormat.R16_Float:\n                        ddpf.Size = Unsafe.SizeOf<DDS.DDSPixelFormat>();\n                        ddpf.Flags = DDS.PixelFormatFlags.FourCC;\n                        ddpf.FourCC = 111; // D3DFMT_R16F\n                        break;\n                }\n            }\n\n            required = sizeof (int) + Unsafe.SizeOf<DDS.Header>();\n\n            if (ddpf.Size == 0)\n                required += Unsafe.SizeOf<DDS.HeaderDXT10>();\n\n            if (pDestination == IntPtr.Zero)\n                return;\n\n            if (maxsize < required)\n                throw new ArgumentException(\"Not enough size for destination buffer\", \"maxsize\");\n\n            *(uint*)(pDestination) = DDS.MagicHeader;\n\n            var header = (DDS.Header*)((byte*)(pDestination) + sizeof (int));\n            *header = default;\n            header->Size = Unsafe.SizeOf<DDS.Header>();\n            header->Flags = DDS.HeaderFlags.Texture;\n            header->SurfaceFlags = DDS.SurfaceFlags.Texture;\n\n            if (description.MipLevels > 0)\n            {\n                header->Flags |= DDS.HeaderFlags.Mipmap;\n                header->MipMapCount = description.MipLevels;\n\n                if (header->MipMapCount > 1)\n                    header->SurfaceFlags |= DDS.SurfaceFlags.Mipmap;\n            }\n","sourceCodeStart":607,"sourceCodeEnd":643,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Foundation/Graphics/DDSHelper.cs#L607-L643","documentation":"Thrown by EncodeDDSHeader when the caller-provided destination buffer (maxsize) is smaller than the required size: the 4-byte magic header plus the DDS header plus, when needed, the DX10 extension header. It is an ArgumentException on the 'maxsize' parameter, thrown before any bytes are written.","triggerScenarios":"Calling EncodeDDSHeader (or higher-level Save-to-DDS APIs) with a preallocated buffer whose size is less than sizeof(uint) + sizeof(DDS.Header) (+ sizeof(DDS.HeaderDXT10) for DX10-extended files). Passing IntPtr.Zero skips writing and returns the required size instead — the throw only occurs for a non-null but too-small buffer.","commonSituations":"Reusing a buffer sized for a legacy DDS and then encoding a DX10-extended one (needs ~20 more bytes); hardcoding 128 as the header size and forgetting the 4-byte magic or the extension header; computing capacity from image data size only.","solutions":["Call EncodeDDSHeader once with pDestination = IntPtr.Zero to get the required size, allocate that, then call again with the real buffer.","Allocate maxsize >= sizeof(uint) + sizeof(DDS.Header) + (needsDx10 ? sizeof(DDS.HeaderDXT10) : 0).","Increase the buffer size and retry; catch ArgumentException on 'maxsize' to report the capacity shortfall."],"exampleFix":"// before\nvar buf = new byte[128];\nEncodeDDSHeader(desc, flags, buf);\n\n// after\nint required = EncodeDDSHeader(desc, flags, IntPtr.Zero, 0);\nvar buf = new byte[required];\nEncodeDDSHeader(desc, flags, buf);","handlingStrategy":"try-catch","validationCode":"int required = EncodeDDSHeader(desc, flags, IntPtr.Zero, 0); // probe pass\nif (buffer.Length < required)\n    throw new ArgumentException($\"Buffer {buffer.Length} < required {required}\", nameof(buffer));","typeGuard":"bool CanEncode(ReadOnlySpan<byte> buf, bool dx10) =>\n    buf.Length >= sizeof(uint) + System.Runtime.CompilerServices.Unsafe.SizeOf<DDS.Header>()\n    + (dx10 ? System.Runtime.CompilerServices.Unsafe.SizeOf<DDS.HeaderDXT10>() : 0);","tryCatchPattern":"try { EncodeDDSHeader(desc, flags, buffer); }\ncatch (ArgumentException e) when (e.ParamName == \"maxsize\")\n{\n    buffer = new byte[EncodeDDSHeader(desc, flags, IntPtr.Zero, 0)];\n    EncodeDDSHeader(desc, flags, buffer);\n}","preventionTips":["Always probe with pDestination = IntPtr.Zero to learn the required size","Account for the 4-byte magic plus DX10 extension header when sizing buffers","Never hardcode 128 as the DDS header size"],"tags":["dds","buffer-size","argument"],"backgroundTag":"argument-out-of-range","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}