{"record":{"id":"8f1c3626b25d70fc","repo":"iOfficeAI/OfficeCLI","slug":"invalid-path-index-idx-in-segment-part-in","errorCode":null,"errorMessage":"Invalid path index '{idx}' in segment '{part}'. Index must be >= 1.","messagePattern":"Invalid path index '(.+?)' in segment '(.+?)'\\. Index must be >= 1\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/GenericXmlQuery.cs","lineNumber":263,"sourceCode":"            {\n                // BUG-R36-01 fix: when ']' is missing (e.g. \"slide[\") the expression\n                // part[(bracketIdx+1)..^1] produces a negative-length range crash.\n                // Detect and reject unclosed brackets with a clean ArgumentException.\n                var closingIdx = part.IndexOf(']', bracketIdx + 1);\n                if (closingIdx < 0)\n                    throw new ArgumentException($\"Malformed path segment '{part}'. Bracket '[' is not closed. Expected format: name[index] or name[@attr=value].\");\n                var name = PathAliases.Resolve(part[..bracketIdx]);\n                var indexStr = part[(bracketIdx + 1)..^1];\n                if (!int.TryParse(indexStr, out var idx))\n                    // A predicate in the index slot (row[Score>0], row[not(V)])\n                    // means the caller reached the single-node path navigator\n                    // with a FILTER. get is one-node-by-path by contract;\n                    // point at the verbs that run the selector engine.\n                    throw new ArgumentException(AttributeFilter.IsContentFilterPath($\"[{indexStr}]\")\n                        ? $\"'{part}' is a predicate, but this verb navigates by position and expects a numeric index (e.g. {part[..part.IndexOf('[')]}[2]). Predicates work on 'query' (read) and 'set'/'remove' (mutate matched elements).\"\n                        : $\"Invalid path index '{indexStr}' in segment '{part}'. Expected a numeric index.\");\n                if (idx < 1)\n                    throw new ArgumentException($\"Invalid path index '{idx}' in segment '{part}'. Index must be >= 1.\");\n                segments.Add((name, idx));\n            }\n            else\n            {\n                segments.Add((PathAliases.Resolve(part), null));\n            }\n        }\n        return segments;\n    }\n\n    /// <summary>\n    /// Navigate an OpenXML element tree by path segments (localName + optional 1-based index).\n    /// Returns null if any segment cannot be resolved.\n    /// </summary>\n    public static OpenXmlElement? NavigateByPath(OpenXmlElement root, IReadOnlyList<(string Name, int? Index)> segments)\n    {\n        OpenXmlElement? current = root;\n        foreach (var seg in segments)","sourceCodeStart":245,"sourceCodeEnd":281,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/GenericXmlQuery.cs#L245-L281","documentation":"Thrown by the GenericXmlQuery path parser when a bracket index parses as an integer but is less than 1. The navigator uses 1-based indexing (matching document/XML positional conventions), so index 0 or negatives are rejected.","triggerScenarios":"Calling a path navigator with a segment like 'row[0]' or 'slide[-1]' — a valid integer that is below the 1-based minimum.","commonSituations":"Assuming zero-based indexing (common from array/JSON backgrounds); passing a computed index without adjusting from 0-based to 1-based; negative index from an off-by-one calc.","solutions":["Use a 1-based index: the first element is [1], not [0].","When converting from a 0-based programmatic index, add 1 before building the path.","Validate computed indices are >= 1 before formatting the path."],"exampleFix":"// before\nGet(xml, $\"rows/row[{arrayIndex}]\"); // arrayIndex is 0-based\n// after\nGet(xml, $\"rows/row[{arrayIndex + 1}]\"); // convert to 1-based","handlingStrategy":"validation","validationCode":"static string ToOneBasedSegment(string name, int zeroBased) =>\n    $\"{name}[{Math.Max(1, zeroBased + 1)}]\";","typeGuard":null,"tryCatchPattern":"try { Get(xml, path); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Index must be >= 1\"))\n{ /* adjust index to 1-based */ }","preventionTips":["Path indices are 1-based; convert programmatic 0-based indices by adding 1.","Reject non-positive computed indices before building the path."],"tags":["xml","path","validation","off-by-one"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}