{"record":{"id":"d71a72df97ca433d","repo":"dotnet/machinelearning","slug":"invalidfieldwidths","errorCode":null,"errorMessage":"InvalidFieldWidths","messagePattern":"InvalidFieldWidths","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/TextFieldParser.cs","lineNumber":978,"sourceCode":"                    Debug.Assert(_fieldWidths[i] > 0, \"Bad field width, this should have been caught on input\");\n                    _lineLength += _fieldWidths[i];\n                }\n                if (_fieldWidths[widthBound] > 0)\n                {\n                    _lineLength += _fieldWidths[widthBound];\n                }\n            }\n        }\n\n        private void ValidateFieldWidthsOnInput(int[] widths)\n        {\n            Debug.Assert(widths != null, \"There are no field widths\");\n            int bound = widths.Length - 1;\n            for (int i = 0; i <= bound - 1; i++)\n            {\n                if (widths[i] < 1)\n                {\n                    throw new ArgumentException(Strings.InvalidFieldWidths);\n                }\n            }\n        }\n\n        private void ValidateAndEscapeDelimiters()\n        {\n            if (_delimiters == null)\n            {\n                throw new Exception(Strings.NullDelimiters);\n            }\n            if (_delimiters.Length == 0)\n            {\n                throw new Exception(Strings.EmptyDelimiters);\n            }\n            int length = _delimiters.Length;\n            StringBuilder builder = new StringBuilder();\n            StringBuilder quoteBuilder = new StringBuilder();\n            quoteBuilder.Append(EndQuotePattern + \"(\");","sourceCodeStart":960,"sourceCodeEnd":996,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/TextFieldParser.cs#L960-L996","documentation":"TextFieldParser.SetFieldWidths validates that every fixed-width entry is at least 1 and throws ArgumentException with InvalidFieldWidths when any width is zero or negative. A field width of 0 or less makes line slicing impossible/meaningless, so the library rejects the widths configuration up front.","triggerScenarios":"Calling SetFieldWidths with an array containing 0 or a negative number, e.g. SetFieldWidths(new int[] { 5, 0, 10 }) or SetFieldWidths(new int[] { -1, 8 }), then reading fields.","commonSituations":"Computing widths from parsed format specs where a missing value defaults to 0; off-by-one arithmetic producing 0; subtracting offsets in the wrong order yielding negative widths; user-supplied column layout containing a blank entry.","solutions":["Fix the widths array so every entry is >= 1.","Validate each width before calling SetFieldWidths and surface which entry is invalid.","If a column can be variable/optional, model it as a delimiter or handle it in code rather than a zero width.","Check width computation math (start/end offsets) for the off-by-one or sign error."],"exampleFix":"// before\nparser.SetFieldWidths(new int[] { 10, 0, 15 }); // throws InvalidFieldWidths\n// after\nint[] widths = new int[] { 10, 5, 15 };\nif (widths.Any(w => w < 1))\n    throw new ArgumentException(\"All field widths must be positive integers.\");\nparser.SetFieldWidths(widths);","handlingStrategy":"validation","validationCode":"if (widths == null || widths.Any(w => w < 1))\n    throw new ArgumentException(\"All field widths must be >= 1.\");\nparser.SetFieldWidths(widths);","typeGuard":"static bool AllWidthsPositive(int[] widths) => widths != null && widths.All(w => w >= 1);","tryCatchPattern":"try\n{\n    parser.SetFieldWidths(widths);\n}\ncatch (ArgumentException ex)\n{\n    logger.LogError(ex, \"Invalid field widths: {Widths}\", string.Join(\",\", widths ?? Array.Empty<int>()));\n    throw;\n}","preventionTips":["Validate every width is a positive integer before calling SetFieldWidths","Log the widths array when computing it programmatically to catch 0/negative values early","Add unit tests for the offset/width arithmetic that produces the widths"],"tags":["fixed-width-parsing","argument-validation","value-out-of-range","microsoft-data-analysis"],"backgroundTag":"invalid-argument-value","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"}