summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorOmar Shaarawi <[email protected]>2025-08-05 05:18:50 -0500
committerGitHub <[email protected]>2025-08-05 05:18:50 -0500
commit58e889796c7e1fcf7d1c78a61fbd5443ff79a3f2 (patch)
treef0c4af52cefdf015ce3bb0bbffdac2e807ea2fb5
parent51498c8de452eb3337e9e20d91c9cacf2c5c277b (diff)
downloadopencode-58e889796c7e1fcf7d1c78a61fbd5443ff79a3f2.tar.gz
opencode-58e889796c7e1fcf7d1c78a61fbd5443ff79a3f2.zip
validate file part bounds to prevent panic (#1612)
-rw-r--r--packages/tui/internal/components/chat/message.go16
-rw-r--r--packages/tui/internal/components/chat/messages.go4
2 files changed, 15 insertions, 5 deletions
diff --git a/packages/tui/internal/components/chat/message.go b/packages/tui/internal/components/chat/message.go
index f320a3fa3..8eb48c69f 100644
--- a/packages/tui/internal/components/chat/message.go
+++ b/packages/tui/internal/components/chat/message.go
@@ -221,22 +221,31 @@ func renderText(
lastEnd := int64(0)
// Apply highlighting to filenames and base style to rest of text
+ textLen := int64(len(text))
for _, filePart := range fileParts {
highlight := base.Foreground(t.Secondary())
start, end := filePart.Source.Text.Start, filePart.Source.Text.End
+ if end > textLen {
+ end = textLen
+ }
+ if start > textLen {
+ start = textLen
+ }
+
if start > lastEnd {
result.WriteString(base.Render(text[lastEnd:start]))
}
- result.WriteString(highlight.Render(text[start:end]))
+ if start < end {
+ result.WriteString(highlight.Render(text[start:end]))
+ }
lastEnd = end
}
- if lastEnd < int64(len(text)) {
+ if lastEnd < textLen {
result.WriteString(base.Render(text[lastEnd:]))
}
-
content = base.Width(width - 6).Render(result.String())
}
@@ -244,7 +253,6 @@ func renderText(
Local().
Format("02 Jan 2006 03:04 PM")
if time.Now().Format("02 Jan 2006") == timestamp[:11] {
- // don't show the date if it's today
timestamp = timestamp[12:]
}
info := fmt.Sprintf("%s (%s)", author, timestamp)
diff --git a/packages/tui/internal/components/chat/messages.go b/packages/tui/internal/components/chat/messages.go
index a4e861d6a..5702cec5d 100644
--- a/packages/tui/internal/components/chat/messages.go
+++ b/packages/tui/internal/components/chat/messages.go
@@ -303,7 +303,9 @@ func (m *messagesComponent) renderView() tea.Cmd {
for _, part := range remainingParts {
switch part := part.(type) {
case opencode.FilePart:
- fileParts = append(fileParts, part)
+ if part.Source.Text.Start >= 0 && part.Source.Text.End >= part.Source.Text.Start {
+ fileParts = append(fileParts, part)
+ }
}
}
flexItems := []layout.FlexItem{}