{"record":{"id":"711b93927ed9a150","repo":"XINCGer/Unity3DTraining","slug":"offset-must-be-within-the-buffer","errorCode":null,"errorMessage":"Offset must be within the buffer","messagePattern":"Offset must be within the buffer","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/CodedInputStream.cs","lineNumber":136,"sourceCode":"        // Note that the checks are performed such that we don't end up checking obviously-valid things\n        // like non-null references for arrays we've just created.\n\n        /// <summary>\n        /// Creates a new CodedInputStream reading data from the given byte array.\n        /// </summary>\n        public CodedInputStream(byte[] buffer) : this(null, ProtoPreconditions.CheckNotNull(buffer, \"buffer\"), 0, buffer.Length)\n        {\n        }\n\n        /// <summary>\n        /// Creates a new <see cref=\"CodedInputStream\"/> that reads from the given byte array slice.\n        /// </summary>\n        public CodedInputStream(byte[] buffer, int offset, int length)\n            : this(null, ProtoPreconditions.CheckNotNull(buffer, \"buffer\"), offset, offset + length)\n        {\n            if (offset < 0 || offset > buffer.Length)\n            {\n                throw new ArgumentOutOfRangeException(\"offset\", \"Offset must be within the buffer\");\n            }\n            if (length < 0 || offset + length > buffer.Length)\n            {\n                throw new ArgumentOutOfRangeException(\"length\", \"Length must be non-negative and within the buffer\");\n            }\n        }\n\n        /// <summary>\n        /// Creates a new <see cref=\"CodedInputStream\"/> reading data from the given stream, which will be disposed\n        /// when the returned object is disposed.\n        /// </summary>\n        /// <param name=\"input\">The stream to read from.</param>\n        public CodedInputStream(Stream input) : this(input, false)\n        {\n        }\n\n        /// <summary>\n        /// Creates a new <see cref=\"CodedInputStream\"/> reading data from the given stream.","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/CodedInputStream.cs#L118-L154","documentation":"CodedInputStream's byte[]/offset/length constructor validates that the offset lies within the supplied buffer. If offset is negative or greater than buffer.Length, ArgumentOutOfRangeException('Offset must be within the buffer') is thrown before any parsing happens. This is defensive argument validation in Google.Protobuf; the stream is never created.","triggerScenarios":"new CodedInputStream(buffer, offset, length) where offset < 0 or offset > buffer.Length. E.g. passing an offset captured from a previous read past the buffer end, or -1 from a failed lookup used directly as the offset.","commonSituations":"Fragmenting a received socket payload into messages and computing the next message offset incorrectly; reusing a stale offset after the buffer was replaced with a shorter array; passing byte[] objects received from unmanaged/interop code with mismatched lengths.","solutions":["Clamp/validate the offset before constructing: ensure 0 <= offset <= buffer.Length.","Verify the offset computation (e.g. bytes consumed so far) against the actual buffer length actually received.","If the offset can legitimately be at/after the end, guard with 'if (offset >= buffer.Length) skip/return' instead of constructing a reader.","Use a checked bounds helper so offsets are always derived from buffer.Length rather than hard-coded assumptions."],"exampleFix":"// before\nvar stream = new CodedInputStream(payload, offset, payload.Length - offset);\n// after\nif (offset < 0 || offset > payload.Length)\n    throw new ArgumentException($\"Bad offset {offset} for buffer of {payload.Length} bytes\");\nvar stream = new CodedInputStream(payload, offset, payload.Length - offset);","handlingStrategy":"validation","validationCode":"if (offset < 0 || offset > buffer.Length) throw new ArgumentException($\"offset {offset} out of bounds for buffer length {buffer.Length}\", nameof(offset));","typeGuard":"bool IsValidOffset(byte[] buffer, int offset) => buffer != null && offset >= 0 && offset <= buffer.Length;","tryCatchPattern":"try { var s = new CodedInputStream(buffer, offset, length); ... } catch (ArgumentOutOfRangeException ex) { logger.LogError(ex, \"Invalid buffer window offset={Offset} len={Length}\", offset, length); return null; }","preventionTips":["Always derive offset from bytes-consumed counters scoped to the current buffer object.","Null-check and length-check buffers received from the network before framing.","Write a unit test for the framing function covering empty buffers and offset == buffer.Length."],"tags":["protobuf","argument-out-of-range","csharp"],"backgroundTag":"argument-out-of-range","analyzedSha":"016f98412ea5e11756b286736f479b66ab6216d5","analyzedAt":"2026-09-12T01:08:58.711Z","contentChangedAt":"2026-09-12T01:08:58.711Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}