{"record":{"id":"11849514f3d10e65","repo":"EllanJiang/GameFramework","slug":"ensure-size-is-invalid","errorCode":null,"errorMessage":"Ensure size is invalid.","messagePattern":"Ensure size is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Base/DataProvider/DataProvider.cs","lineNumber":128,"sourceCode":"            add\n            {\n                m_ReadDataDependencyAssetEventHandler += value;\n            }\n            remove\n            {\n                m_ReadDataDependencyAssetEventHandler -= value;\n            }\n        }\n\n        /// <summary>\n        /// 确保二进制流缓存分配足够大小的内存并缓存。\n        /// </summary>\n        /// <param name=\"ensureSize\">要确保二进制流缓存分配内存的大小。</param>\n        public static void EnsureCachedBytesSize(int ensureSize)\n        {\n            if (ensureSize < 0)\n            {\n                throw new GameFrameworkException(\"Ensure size is invalid.\");\n            }\n\n            if (s_CachedBytes == null || s_CachedBytes.Length < ensureSize)\n            {\n                FreeCachedBytes();\n                int size = (ensureSize - 1 + BlockSize) / BlockSize * BlockSize;\n                s_CachedBytes = new byte[size];\n            }\n        }\n\n        /// <summary>\n        /// 释放缓存的二进制流。\n        /// </summary>\n        public static void FreeCachedBytes()\n        {\n            s_CachedBytes = null;\n        }\n","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Base/DataProvider/DataProvider.cs#L110-L146","documentation":"DataProvider.EnsureCachedBytesSize throws this GameFrameworkException when asked to ensure a negative buffer size. The static byte cache used to hold binary data cannot be sized below zero, so the argument is rejected immediately. It is an argument validation guard before any memory allocation.","triggerScenarios":"Calling DataProvider.EnsureCachedBytesSize with a negative int, directly or indirectly via ReadData when m_ResourceManager.GetBinaryLength(dataAssetName) returns a negative length (e.g. a corrupted resource index).","commonSituations":"Corrupt or stale resource metadata making GetBinaryLength return -1; a typo passing a sentinel value like -1 as ensureSize; custom code pre-sizing the cache before a load with a computed length that underflowed.","solutions":["Check the value passed to EnsureCachedBytesSize; never pass a negative int.","If triggered via ReadData, verify the resource's binary length metadata on disk is intact and re-build/export the resource package.","Upgrade/re-sync GameFramework and resource version files so GetBinaryLength cannot report a bogus negative length."],"exampleFix":"// before\nDataProvider.EnsureCachedBytesSize(dataLength); // dataLength == -1\n// after\nif (dataLength > 0) { DataProvider.EnsureCachedBytesSize(dataLength); }","handlingStrategy":"validation","validationCode":"if (ensureSize < 0) throw new ArgumentOutOfRangeException(nameof(ensureSize));\nDataProvider.EnsureCachedBytesSize(ensureSize);","typeGuard":"bool IsValidSize(int size) => size >= 0;","tryCatchPattern":null,"preventionTips":["Never pass sentinel negatives (e.g. -1) as sizes.","Sanity-check GetBinaryLength results before using them as sizes."],"tags":["argument-validation","negative-value","dataprovider"],"backgroundTag":"invalid-argument-value","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}