From 996f8cdc97ad8eccc6bedcd25218cb2d2ab465fa Mon Sep 17 00:00:00 2001 From: Marnie0415 Date: Wed, 15 Jul 2026 07:00:50 +0800 Subject: [PATCH] fix: reject negative --index in ExcelHandler.Add and PowerPointHandler.Add MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WordHandler.Add already rejects negative --index values (line 49-54) with a clean error message. ExcelHandler.Add and PowerPointHandler.Add lack this guard — a negative index propagates to collection indexing and surfaces as a raw ArgumentOutOfRangeException. Add the same early validation so all three handlers behave consistently. --- src/officecli/Handlers/Excel/ExcelHandler.Add.cs | 8 ++++++++ src/officecli/Handlers/Pptx/PowerPointHandler.Add.cs | 7 +++++++ 2 files changed, 15 insertions(+) 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);