{"record":{"id":"5dee6b92eb6b81bb","repo":"XINCGer/Unity3DTraining","slug":"size-limit-must-be-positive","errorCode":null,"errorMessage":"Size limit must be positive","messagePattern":"Size limit must be positive","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/CodedInputStream.cs","lineNumber":193,"sourceCode":"            this.bufferSize = bufferSize;\n            this.sizeLimit = DefaultSizeLimit;\n            this.recursionLimit = DefaultRecursionLimit;\n        }\n\n        /// <summary>\n        /// Creates a new CodedInputStream reading data from the given\n        /// stream and buffer, using the specified limits.\n        /// </summary>\n        /// <remarks>\n        /// This chains to the version with the default limits instead of vice versa to avoid\n        /// having to check that the default values are valid every time.\n        /// </remarks>\n        internal CodedInputStream(Stream input, byte[] buffer, int bufferPos, int bufferSize, int sizeLimit, int recursionLimit)\n            : this(input, buffer, bufferPos, bufferSize)\n        {\n            if (sizeLimit <= 0)\n            {\n                throw new ArgumentOutOfRangeException(\"sizeLimit\", \"Size limit must be positive\");\n            }\n            if (recursionLimit <= 0)\n            {\n                throw new ArgumentOutOfRangeException(\"recursionLimit!\", \"Recursion limit must be positive\");\n            }\n            this.sizeLimit = sizeLimit;\n            this.recursionLimit = recursionLimit;\n        }\n        #endregion\n\n        /// <summary>\n        /// Creates a <see cref=\"CodedInputStream\"/> with the specified size and recursion limits, reading\n        /// from an input stream.\n        /// </summary>\n        /// <remarks>\n        /// This method exists separately from the constructor to reduce the number of constructor overloads.\n        /// It is likely to be used considerably less frequently than the constructors, as the default limits\n        /// are suitable for most use cases.","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/CodedInputStream.cs#L175-L211","documentation":"The internal constructor that takes an explicit sizeLimit validates that sizeLimit > 0. sizeLimit bounds the total number of bytes the logical stream may contain; a non-positive value would make progress checks meaningless. This constructor is internal, so it is hit via library-internal paths or reflection, not typical public API calls.","triggerScenarios":"Constructing CodedInputStream via the internal (Stream, byte[], int, int, int sizeLimit, int recursionLimit) overload with sizeLimit <= 0 — e.g. passing a length of 0 for an empty payload or an uninitialized/default int.","commonSituations":"Custom framing code using reflection or InternalsVisibleTo to build segmented readers; passing a computed total message size that came out 0 or negative due to bad arithmetic upstream.","solutions":["Ensure sizeLimit is at least 1 — if the payload may be empty, special-case the empty input instead of constructing a reader with limit 0.","Fix the upstream computation that produced the non-positive total size.","Avoid the internal constructor; use public overloads (byte[] or Stream based) which derive limits correctly.","If using reflection, validate parameters against the constructor's invariants first."],"exampleFix":"// before\nvar stream = MakeReader(input, buf, pos, size, totalSize, 64); // totalSize could be 0\n// after\nif (totalSize <= 0) throw new InvalidDataException(\"Empty or invalid message size\");\nvar stream = MakeReader(input, buf, pos, size, totalSize, 64);","handlingStrategy":"validation","validationCode":"if (sizeLimit <= 0) throw new InvalidDataException($\"sizeLimit must be positive, got {sizeLimit}\");","typeGuard":"bool IsValidSizeLimit(int sizeLimit) => sizeLimit > 0;","tryCatchPattern":"try { CreateReader(input, buf, pos, size, sizeLimit, 64); } catch (ArgumentOutOfRangeException ex) { logger.LogError(ex, \"Invalid size limit {V}\", sizeLimit); }","preventionTips":["Special-case empty payloads before building a reader.","Initialize all limit fields in config objects with sane defaults (e.g. 64MB).","Avoid internal constructors; use public overloads."],"tags":["protobuf","argument-out-of-range","internal-api"],"backgroundTag":"invalid-argument-value","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"}