{"record":{"id":"83a31b17c95fe55c","repo":"microsoft/garnet","slug":"setting-bitfield-failed-size-of-bitmap-smaller-th","errorCode":null,"errorMessage":"Setting bitfield failed: Size of bitmap smaller than offset provided.","messagePattern":"Setting bitfield failed: Size of bitmap smaller than offset provided\\.","errorType":"exception","errorClass":"GarnetException","httpStatus":null,"severity":"error","filePath":"libs/server/Resp/Bitmap/BitmapManagerBitfield.cs","lineNumber":371,"sourceCode":"        /// <param name=\"encoding\"></param>\n        /// <param name=\"signed\"></param>\n        /// <param name=\"newValue\"></param>\n        /// <param name=\"overflowType\"></param>\n        /// <returns></returns>\n        /// <exception cref=\"GarnetException\"></exception>\n        private static (long, bool) SetBitfield(byte* bitmap, long bitmapLength, long offset, byte encoding, bool signed, long newValue, byte overflowType)\n        {\n            if (!TryValidateBitfieldOffset(offset, encoding, multiplyOffset: false, out offset, out var endOffset))\n                throw new GarnetException(\"ERR value is not an integer or out of range.\");\n\n            var byteIndexStart = Index(offset);\n            var byteIndexEnd = Index(endOffset) + 1;\n            var buf = stackalloc byte[8];\n            *(ulong*)buf = 0;\n\n            // Simple case value is beyond current length\n            if (byteIndexStart >= bitmapLength)\n                throw new GarnetException(\"Setting bitfield failed: Size of bitmap smaller than offset provided.\");\n\n            #region getValue\n            // Shift with typeInfo in mind            \n            var vend = bitmap + bitmapLength;\n            var curr = bitmap + byteIndexStart;\n            var cend = bitmap + byteIndexEnd < vend ? (bitmap + byteIndexEnd) : vend;\n            var oldValue = GetValue(ref buf, ref curr, cend, vend, bitmapLength, offset, encoding, signed);\n\n            #endregion\n\n            #region checkOverflow\n            if (overflowType == (byte)BitFieldOverflow.FAIL && CheckBitfieldOverflow(oldValue, 0, out _, encoding, overflowType, signed))\n                return (0, true);\n            #endregion\n\n            #region setValue\n            // Prune leading bits\n            var _left = (int)(offset - (byteIndexStart << 3));","sourceCodeStart":353,"sourceCodeEnd":389,"githubUrl":"https://github.com/microsoft/garnet/blob/951b0fc6838721f89d102c2bbe1b914e8d39d700/libs/server/Resp/Bitmap/BitmapManagerBitfield.cs#L353-L389","documentation":"Thrown by SetBitfield after offset validation passed but byteIndexStart >= bitmapLength - the bitmap buffer handed to the setter is shorter than the offset requires. For SET the bitmap is expected to be grown to cover the offset first; reaching this throw means the buffer was not grown (or the offset exceeds the allocated size). Distinct from the range-error throws: the offset itself is valid, the buffer is just too small.","triggerScenarios":"SetBitfield is called with a byte-index start at/over the provided bitmap length - the caller did not pre-grow the bitmap to cover the SET offset.","commonSituations":"An internal SET path that fails to grow the bitmap before invoking the setter; memory/allocation limits preventing growth; a very large offset on a small/empty bitmap where growth was skipped.","solutions":["Ensure the bitmap is grown to Index(offset) + requiredBytes before calling SET (normally done by the command layer).","If hitting memory limits, raise the configured bitmap/object size limit or reduce the offset.","If reproducible on a current build with a normal SET, report it - the growth step may be missing."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before SET, ensure the bitmap is large enough for the target offset\nvar neededBytes = (int)(offset >> 3) + EncodingByteLength(encoding);\nif (currentBitmapLength < neededBytes)\n    currentBitmap = GrowBitmap(currentBitmap, neededBytes);","typeGuard":null,"tryCatchPattern":"catch (GarnetException ex) when (ex.Message.StartsWith(\"Setting bitfield failed: Size of bitmap\")) {\n    logger.LogError(\"Bitmap not grown for SET offset; check allocation path\");\n    throw;\n}","preventionTips":["Grow the bitmap to cover the SET offset before invoking the setter.","Cap SET offsets at a value the allocation path can satisfy."],"tags":["bitmap","bitfield","allocation","internal"],"backgroundTag":null,"analyzedSha":"951b0fc6838721f89d102c2bbe1b914e8d39d700","analyzedAt":"2026-08-13T19:01:32.939Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}