diff --git a/src/officecli/Handlers/Excel/ExcelHandler.Add.cs b/src/officecli/Handlers/Excel/ExcelHandler.Add.cs index 9924ac494..b06957412 100644 --- a/src/officecli/Handlers/Excel/ExcelHandler.Add.cs +++ b/src/officecli/Handlers/Excel/ExcelHandler.Add.cs @@ -20,6 +20,14 @@ public string Add(string parentPath, string type, InsertPosition? position, Dict { Modified = true; var index = position?.Index; + + // Reject negative --index up front with a clean message instead of + // letting it fall through and surface as a raw .NET + // ArgumentOutOfRangeException from collection indexing. Matches + // the guard in WordHandler.Add. + if (index.HasValue && index.Value < 0) + throw new ArgumentException("--index must be non-negative."); + // Normalize to case-insensitive lookup so camelCase keys (e.g. minColor) match lowercase lookups. // Preserve TrackingPropertyDictionary so handler-as-truth read // tracking survives — its comparer wraps OrdinalIgnoreCase already. diff --git a/src/officecli/Handlers/Pptx/PowerPointHandler.Add.cs b/src/officecli/Handlers/Pptx/PowerPointHandler.Add.cs index f96c3eda1..be3360a63 100644 --- a/src/officecli/Handlers/Pptx/PowerPointHandler.Add.cs +++ b/src/officecli/Handlers/Pptx/PowerPointHandler.Add.cs @@ -38,6 +38,13 @@ public string Add(string parentPath, string type, InsertPosition? position, Dict parentPath = ResolveIdPath(parentPath); parentPath = ResolveLastPredicates(parentPath); + // Reject negative --index up front with a clean message instead of + // letting it fall through and surface as a raw .NET + // ArgumentOutOfRangeException from collection indexing. Matches + // the guard in WordHandler.Add. + if (position?.Index.HasValue == true && position.Index.Value < 0) + throw new ArgumentException("--index must be non-negative."); + // Resolve --after/--before to index (handles find: prefix) var index = ResolveAnchorPosition(parentPath, position);