{"record":{"id":"7f8662009e55dd3b","repo":"dotnet/machinelearning","slug":"positivenumberofcharacters","errorCode":null,"errorMessage":"PositiveNumberOfCharacters","messagePattern":"PositiveNumberOfCharacters","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/TextFieldParser.cs","lineNumber":453,"sourceCode":"                    return ParseFixedWidthLine();\n                case FieldType.Delimited:\n                    return ParseDelimitedLine();\n                default:\n                    Debug.Fail(\"The TextFieldType is not supported\");\n                    return null;\n            }\n        }\n\n        ///<summary>\n        /// Peek at <paramref name=\"numberOfChars\"/> characters of the next data line without reading the line\n        ///</summary>\n        ///<param name=\"numberOfChars\">The number of characters to look at in the next data line.</param>\n        ///<returns>A string consisting of the first <paramref name=\"numberOfChars\"/> characters of the next line. >If numberOfChars is greater than the next line, only the next line is returned</returns>\n        public string PeekChars(int numberOfChars)\n        {\n            if (numberOfChars <= 0)\n            {\n                throw new ArgumentException(string.Format(Strings.PositiveNumberOfCharacters, nameof(numberOfChars)));\n            }\n\n            if ((_reader == null) || (_buffer == null))\n            {\n                return null;\n            }\n\n            if (_endOfData)\n            {\n                return null;\n            }\n\n            string line = PeekNextDataLine();\n            if (line == null)\n            {\n                _endOfData = true;\n                return null;\n            }","sourceCodeStart":435,"sourceCodeEnd":471,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/TextFieldParser.cs#L435-L471","documentation":"TextFieldParser.PeekChars throws ArgumentException(\"PositiveNumberOfCharacters\") when numberOfChars is zero or negative. PeekChars only makes sense for a positive count of characters to inspect in the next line, so non-positive arguments are rejected up front.","triggerScenarios":"Calling PeekChars(0) or PeekChars with a negative int, often when the value comes from a computed variable or user configuration rather than a literal.","commonSituations":"A config value for 'preview length' defaulting to 0; an off-by-one in decrementing a counter before calling PeekChars; passing an uninitialized/zero int field.","solutions":["Pass a strictly positive integer to PeekChars (numberOfChars >= 1)","Guard the call site: only call PeekChars when the computed count is > 0","Fix the source of the non-positive value (config default, counter, calculation)"],"exampleFix":"// before\nparser.PeekChars(previewLength); // previewLength could be 0\n// after\nstring peeked = previewLength > 0 ? parser.PeekChars(previewLength) : null;","handlingStrategy":"validation","validationCode":"if (numberOfChars <= 0) throw new ArgumentOutOfRangeException(nameof(numberOfChars), \"PeekChars requires a positive character count.\");","typeGuard":null,"tryCatchPattern":"try { peeked = parser.PeekChars(n); } catch (ArgumentException ex) when (ex.Message.Contains(\"PositiveNumberOfCharacters\")) { peeked = null; }","preventionTips":["Never pass literal 0 or negative values to PeekChars","Clamp computed values: Math.Max(1, count)","Validate config-driven preview sizes at startup"],"tags":["textfieldparser","argumentexception","argument-out-of-range","input-validation"],"backgroundTag":"argument-out-of-range","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}