Skip to content

Commit 1ccc497

Browse files
committed
fixes
1 parent a77bb7f commit 1ccc497

2 files changed

Lines changed: 78 additions & 1 deletion

File tree

internal/utils/tdl.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ func convertLiveCommentToTDLComment(liveComment LiveComment, chatStartTime time.
363363
var pos1, pos2 int
364364
var emoteFragment Fragment
365365
emotePositions := strings.Split(liveCommentEmoteLocation, "-")
366+
if len(emotePositions) < 2 {
367+
return Comment{}, false, fmt.Errorf("failed to convert emote position: invalid emote location %q", liveCommentEmoteLocation)
368+
}
366369
pos1, err = strconv.Atoi(emotePositions[0])
367370
if err != nil {
368371
return Comment{}, false, fmt.Errorf("failed to convert emote position: %v", err)
@@ -418,6 +421,14 @@ func convertLiveCommentToTDLComment(liveComment LiveComment, chatStartTime time.
418421

419422
formattedEmoteFragments := []Fragment{}
420423
for i, emoteFragment := range emoteFragments {
424+
if emoteFragment.Pos1 < 0 ||
425+
emoteFragment.Pos1 > len(tdlComment.Message.Body) ||
426+
emoteFragment.Pos2 < emoteFragment.Pos1 ||
427+
emoteFragment.Pos2 > len(tdlComment.Message.Body) {
428+
log.Warn().Str("message_id", liveComment.MessageID).Msg("skipping invalid emote fragment range")
429+
continue
430+
}
431+
421432
if i == 0 {
422433
fragmentText := tdlComment.Message.Body[:emoteFragment.Pos1]
423434
fragment := Fragment{
@@ -431,7 +442,14 @@ func convertLiveCommentToTDLComment(liveComment LiveComment, chatStartTime time.
431442
log.Warn().Str("message_id", liveComment.MessageID).Msg("skipping invalid emote position")
432443
continue
433444
}
434-
fragmentText := tdlComment.Message.Body[emoteFragments[i-1].Pos2:emoteFragment.Pos1]
445+
previousPos2 := emoteFragments[i-1].Pos2
446+
if previousPos2 < 0 ||
447+
previousPos2 > len(tdlComment.Message.Body) ||
448+
previousPos2 > emoteFragment.Pos1 {
449+
log.Warn().Str("message_id", liveComment.MessageID).Msg("skipping overlapping or invalid emote fragment")
450+
continue
451+
}
452+
fragmentText := tdlComment.Message.Body[previousPos2:emoteFragment.Pos1]
435453
fragment := Fragment{
436454
Text: fragmentText,
437455
Emoticon: nil,

internal/utils/tdl_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,65 @@ func TestConvertTwitchLiveChatToTDLChatDoesNotReplaceOutputOnInvalidInput(t *tes
377377
}
378378
}
379379

380+
func TestConvertLiveCommentToTDLCommentRejectsMalformedEmoteLocations(t *testing.T) {
381+
chatStart := time.Unix(1_700_000_000, 0)
382+
383+
for _, location := range []string{"", "missing-delimiter"} {
384+
t.Run(location, func(t *testing.T) {
385+
comment := LiveComment{
386+
Message: "Kappa",
387+
MessageID: "message-id",
388+
Timestamp: chatStart.UnixMicro(),
389+
Emotes: []LiveCommentEmote{
390+
{
391+
Name: "Kappa",
392+
Locations: []string{location},
393+
},
394+
},
395+
}
396+
397+
if _, _, err := convertLiveCommentToTDLComment(comment, chatStart); err == nil {
398+
t.Fatalf("expected malformed emote location %q to return an error", location)
399+
}
400+
})
401+
}
402+
}
403+
404+
func TestConvertLiveCommentToTDLCommentSkipsOverlappingEmoteFragments(t *testing.T) {
405+
chatStart := time.Unix(1_700_000_000, 0)
406+
comment := LiveComment{
407+
Message: "Kappa",
408+
MessageID: "message-id",
409+
Timestamp: chatStart.UnixMicro(),
410+
Emotes: []LiveCommentEmote{
411+
{
412+
ID: "first-emote",
413+
Name: "Kappa",
414+
Locations: []string{"0-4"},
415+
},
416+
{
417+
ID: "overlapping-emote",
418+
Name: "appa",
419+
Locations: []string{"1-4"},
420+
},
421+
},
422+
}
423+
424+
converted, include, err := convertLiveCommentToTDLComment(comment, chatStart)
425+
if err != nil {
426+
t.Fatalf("expected overlapping emote to be skipped, got error: %v", err)
427+
}
428+
if !include {
429+
t.Fatal("expected comment to be included")
430+
}
431+
432+
for _, fragment := range converted.Message.Fragments {
433+
if fragment.Emoticon != nil && fragment.Emoticon.EmoticonID == "overlapping-emote" {
434+
t.Fatalf("expected overlapping emote fragment to be skipped, got %#v", converted.Message.Fragments)
435+
}
436+
}
437+
}
438+
380439
func TestEnrichTwitchChatMetadataFromLiveChat(t *testing.T) {
381440
tmpDir := t.TempDir()
382441
liveChatPath := filepath.Join(tmpDir, "live-chat.json")

0 commit comments

Comments
 (0)