Skip to content

Commit baf9a61

Browse files
committed
fix: search for end marker after start marker in context section methods
Address Copilot review: content.find(CONTEXT_MARKER_END) searched from the start of the file rather than after the located start marker. If the file contained a stray end marker before the start marker, the wrong slice could be replaced. Now both upsert_context_section() and remove_context_section() pass start_idx as the second argument to find() and validate end_idx > start_idx before performing the replacement.
1 parent 2f6e3c8 commit baf9a61

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

  • src/specify_cli/integrations

src/specify_cli/integrations/base.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,12 @@ def upsert_context_section(
431431
if ctx_path.exists():
432432
content = ctx_path.read_text(encoding="utf-8")
433433
start_idx = content.find(self.CONTEXT_MARKER_START)
434-
end_idx = content.find(self.CONTEXT_MARKER_END)
434+
end_idx = content.find(
435+
self.CONTEXT_MARKER_END,
436+
start_idx if start_idx != -1 else 0,
437+
)
435438

436-
if start_idx != -1 and end_idx != -1:
439+
if start_idx != -1 and end_idx != -1 and end_idx > start_idx:
437440
# Replace existing section (include the end marker + newline)
438441
end_of_marker = end_idx + len(self.CONTEXT_MARKER_END)
439442
if end_of_marker < len(content) and content[end_of_marker] == "\n":
@@ -468,9 +471,12 @@ def remove_context_section(self, project_root: Path) -> bool:
468471

469472
content = ctx_path.read_text(encoding="utf-8")
470473
start_idx = content.find(self.CONTEXT_MARKER_START)
471-
end_idx = content.find(self.CONTEXT_MARKER_END)
474+
end_idx = content.find(
475+
self.CONTEXT_MARKER_END,
476+
start_idx if start_idx != -1 else 0,
477+
)
472478

473-
if start_idx == -1 or end_idx == -1:
479+
if start_idx == -1 or end_idx == -1 or end_idx <= start_idx:
474480
return False
475481

476482
end_of_marker = end_idx + len(self.CONTEXT_MARKER_END)

0 commit comments

Comments
 (0)