mirror of
https://github.com/zoriya/go-astisub.git
synced 2026-08-16 02:33:21 +00:00
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:
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user