Skip to content

Commit 4f279f2

Browse files
haasonsaasclaude
andcommitted
TDD: fix invalid slice panic in commit message extraction with malformed XML
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9b1d585 commit 4f279f2

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/core/commit_prompt.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ Commit: docs: update installation instructions for v2.0
6767
pub fn extract_commit_message(response: &str) -> String {
6868
// Try to extract from <commit> tags first
6969
if let Some(start) = response.find("<commit>") {
70-
if let Some(end) = response.find("</commit>") {
71-
let commit = response[start + 8..end].trim();
70+
let after_tag = start + 8;
71+
if let Some(end) = response[after_tag..].find("</commit>") {
72+
let commit = response[after_tag..after_tag + end].trim();
7273
return commit.to_string();
7374
}
7475
}
@@ -135,3 +136,32 @@ Analyze the changes and provide a PR title in <title> tags.
135136
(system_prompt.to_string(), user_prompt)
136137
}
137138
}
139+
140+
#[cfg(test)]
141+
mod tests {
142+
use super::*;
143+
144+
#[test]
145+
fn test_extract_commit_message_normal() {
146+
let response = "<analysis>Changed auth</analysis>\n<commit>fix(auth): handle null token</commit>";
147+
assert_eq!(
148+
CommitPromptBuilder::extract_commit_message(response),
149+
"fix(auth): handle null token"
150+
);
151+
}
152+
153+
#[test]
154+
fn test_extract_commit_message_malformed_closing_before_opening() {
155+
// Malformed: closing tag appears before opening tag — should NOT panic
156+
let response = "text</commit> more <commit>feat: real message</commit>";
157+
let result = CommitPromptBuilder::extract_commit_message(response);
158+
assert!(!result.is_empty());
159+
}
160+
161+
#[test]
162+
fn test_extract_commit_message_no_tags() {
163+
let response = "Some analysis\nfeat(auth): add login\nMore text";
164+
let result = CommitPromptBuilder::extract_commit_message(response);
165+
assert!(!result.is_empty());
166+
}
167+
}

0 commit comments

Comments
 (0)