fix: nil-pointer panic in WriteToSSA when Metadata is nil (#145)

* fix: nil-pointer panic in WriteToSSA when Metadata is nil

WriteToSSA dereferenced s.Metadata unconditionally when computing the
v4plus flag, but Metadata is an optional *Metadata that only ReadFromSSA
populates. Reading any other format (WebVTT, SRT, TTML, STL) and writing
SSA therefore panicked with a nil-pointer dereference.

Guard the read: nil Metadata now yields v4plus=false (legacy [V4 Styles]
output), matching the existing nil-safe newSSAScriptInfo one line above.

Adds TestWriteToSSANilMetadata as a regression test.

* Address review feedback on TestWriteToSSANilMetadata

Move the nil-metadata comment above the subtitles literal and simplify
its wording, and replace the post-write asserts with assert.NotPanics
to focus the test on the regression (writing SSA no longer panics).
This commit is contained in:
Steve Toro
2026-07-08 08:57:43 +02:00
committed by GitHub
parent b8dd60d1e7
commit 52190f1d60
2 changed files with 20 additions and 1 deletions
+1 -1
View File
@@ -1180,7 +1180,7 @@ func (s Subtitles) WriteToSSA(o io.Writer) (err error) {
return
}
var v4plus = s.Metadata.SSAScriptType == "v4.00+"
var v4plus = s.Metadata != nil && s.Metadata.SSAScriptType == "v4.00+"
// Write Styles block
if len(s.Styles) > 0 {
+19
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"io/ioutil"
"testing"
"time"
"github.com/asticode/go-astikit"
"github.com/asticode/go-astisub"
@@ -122,6 +123,24 @@ func TestSSAv4plus(t *testing.T) {
assert.Equal(t, string(c), w.String())
}
func TestWriteToSSANilMetadata(t *testing.T) {
// Metadata intentionally left nil
s := &astisub.Subtitles{
Items: []*astisub.Item{
{
StartAt: time.Second,
EndAt: 3 * time.Second,
Lines: []astisub.Line{{Items: []astisub.LineItem{{Text: "Hello world"}}}},
},
},
}
w := &bytes.Buffer{}
assert.NotPanics(t, func() {
s.WriteToSSA(w)
})
}
func TestInBetweenSSAEffect(t *testing.T) {
s, err := astisub.ReadFromSSA(bytes.NewReader([]byte(`[Events]
Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text