{"record":{"id":"a6cd234e185df493","repo":"XINCGer/Unity3DTraining","slug":"length-must-be-non-negative-and-within-the-buffer","errorCode":null,"errorMessage":"Length must be non-negative and within the buffer","messagePattern":"Length must be non-negative and within the buffer","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/CodedInputStream.cs","lineNumber":140,"sourceCode":"        /// 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.\n        /// </summary>\n        /// <param name=\"input\">The stream to read from.</param>\n        /// <param name=\"leaveOpen\"><c>true</c> to leave <paramref name=\"input\"/> open when the returned\n        /// <c cref=\"CodedInputStream\"/> is disposed; <c>false</c> to dispose of the given stream when the","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/CodedInputStream.cs#L122-L158","documentation":"The same constructor validates that the requested length is non-negative and that offset+length does not run past the end of the buffer. If length < 0 or offset + length > buffer.Length, ArgumentOutOfRangeException('Length must be non-negative and within the buffer') is thrown. This prevents the parser from ever reading beyond the supplied data.","triggerScenarios":"new CodedInputStream(buffer, offset, length) with a negative length (e.g. computing length = end - start where end < start), or length so large that offset + length exceeds buffer.Length (e.g. assuming a length prefix that exceeds the actual payload).","commonSituations":"Framing messages from a stream where the declared message length is trusted but the packet was truncated; off-by-one in start/end index arithmetic; reading a length header in the wrong endianness producing a huge length.","solutions":["Compute length as buffer.Length - offset and validate it is >= 0 before constructing the reader.","Validate any length-prefix from the wire against the actual bytes available before honoring it.","Log the buffer length, offset, and computed length at the failure site to find the bad arithmetic.","If truncation is expected, handle it at the framing layer (buffer more data) instead of passing an over-large length."],"exampleFix":"// before\nvar stream = new CodedInputStream(buf, pos, declaredLen);\n// after\nint available = buf.Length - pos;\nint len = Math.Min(declaredLen, available);\nif (len < 0) throw new InvalidDataException(\"Truncated message\");\nvar stream = new CodedInputStream(buf, pos, len);","handlingStrategy":"validation","validationCode":"int avail = buffer.Length - offset;\nif (length < 0 || length > avail) throw new ArgumentException($\"length {length} invalid; only {avail} bytes available at offset {offset}\");","typeGuard":"bool IsValidWindow(byte[] buffer, int offset, int length) => offset >= 0 && length >= 0 && offset + length <= buffer.Length;","tryCatchPattern":"try { var s = new CodedInputStream(buffer, offset, length); ... } catch (ArgumentOutOfRangeException ex) { logger.LogError(ex, \"Bad buffer window\"); bufferMoreDataFromSocket(); }","preventionTips":["Never trust wire-declared length prefixes; clamp to bytes actually received.","Compute lengths as end - start only after asserting end >= start.","Keep message framing in one helper so length arithmetic is tested once."],"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"}