From cc7481dd326c999f22eaa113c785104ad43b2b6b Mon Sep 17 00:00:00 2001 From: gravity-zero Date: Mon, 9 Nov 2026 23:14:21 +0100 Subject: [PATCH 01/10] fix(mp4): a subtitle scan reads ahead no further than the file, whatever size its Segment declares --- mp4/hlsplan_ahead.go | 2 +- mp4/hlsplan_subtitle_hint_forged_test.go | 44 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 mp4/hlsplan_subtitle_hint_forged_test.go diff --git a/mp4/hlsplan_ahead.go b/mp4/hlsplan_ahead.go index 5fc4063..6f24a13 100644 --- a/mp4/hlsplan_ahead.go +++ b/mp4/hlsplan_ahead.go @@ -80,7 +80,7 @@ func (p *HLSPlan) hintRange(src io.Reader, start, end int64) { // first window at or past ms, plus the look into it, else the Segment's end. func (p *HLSPlan) clusterEndOff(ms int64) int64 { end := p.segEnd - if end <= 0 && p.srcInfo != nil { // a Segment of unknown size ends with the file + if p.srcInfo != nil && (end <= 0 || end > p.srcInfo.Size()) { // a Segment of unknown size ends with the file, and none runs past it end = p.srcInfo.Size() } if ms == math.MaxInt64 { diff --git a/mp4/hlsplan_subtitle_hint_forged_test.go b/mp4/hlsplan_subtitle_hint_forged_test.go new file mode 100644 index 0000000..88fca1e --- /dev/null +++ b/mp4/hlsplan_subtitle_hint_forged_test.go @@ -0,0 +1,44 @@ +package mp4 + +import ( + "context" + "os" + "path/filepath" + "testing" +) + +// A Segment declaring more bytes than its file holds (a forged size, a file cut short) has its subtitle scan read +// ahead no further than the file: the range asked of the disk was the declared one, 2^56 bytes one call at a time. +func TestSubtitleScanReadsAheadNoFurtherThanTheFile(t *testing.T) { + data, err := os.ReadFile("../internal/testdata/sample_subs.mkv") + if err != nil { + t.Fatal(err) + } + copy(data[44:52], []byte{0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}) // the Segment's size: 2^56-2 + path := filepath.Join(t.TempDir(), "forged.mkv") + if err := os.WriteFile(path, data, 0o644); err != nil { + t.Fatal(err) + } + type rng struct{ off, n int64 } + var will []rng + defer func(w func(*os.File, int64, int64)) { adviseWillNeed = w }(adviseWillNeed) + adviseWillNeed = func(_ *os.File, off, n int64) { will = append(will, rng{off, n}) } + defer func(f func(*os.File) bool) { networkSource = f }(networkSource) + networkSource = func(*os.File) bool { return false } + ctx := context.Background() + plan, err := PlanHLS(ctx, path, Options{SegmentMs: 2000, StreamFromFS: true}) + if err != nil { + t.Fatal(err) + } + if _, err := plan.subtitleSegment(ctx, 0, 0); err != nil { + t.Fatal(err) + } + if len(will) == 0 { + t.Fatal("the subtitle scan read nothing ahead") + } + for _, w := range will { + if w.off+w.n > int64(len(data)) { + t.Errorf("read ahead [%d, %d) of a file of %d bytes", w.off, w.off+w.n, len(data)) + } + } +} From ad8ecac0957d8a6189e370deeffeea20c1e98c1d Mon Sep 17 00:00:00 2001 From: gravity-zero Date: Mon, 9 Nov 2026 23:14:39 +0100 Subject: [PATCH 02/10] fix(ops): an in-place edit voids a tail element no further than the end of the file, its padding written in chunks --- mkv/ops/inplace.go | 12 ++++++++++-- mkv/ops/inplace_tail_past_eof_test.go | 28 +++++++++++++++++++++++++++ mkv/writer/writer.go | 5 ++++- 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 mkv/ops/inplace_tail_past_eof_test.go diff --git a/mkv/ops/inplace.go b/mkv/ops/inplace.go index 2decd46..3ef5576 100644 --- a/mkv/ops/inplace.go +++ b/mkv/ops/inplace.go @@ -765,13 +765,21 @@ func scanTail(f mkv.ReadSeekCloser, pos, segEnd int64, region *metadataRegion) e region.tailUnknown = true // more than one: not one element to keep or rewrite return nil } - region.tail[eh.ID] = tailElement{pos: pos, size: int64(hdrLen) + eh.Size} + region.tail[eh.ID] = tailElement{pos: pos, size: heldSize(f, pos, int64(hdrLen)+eh.Size)} } pos += int64(hdrLen) + eh.Size } return nil } +// heldSize is how much of an element declaring size bytes at pos the file holds: a file cut short ends it early. +func heldSize(f io.Seeker, pos, size int64) int64 { + if end, err := f.Seek(0, io.SeekEnd); err == nil && pos+size > end { + return end - pos + } + return size +} + // classifyTailEntries re-reads the old SeekHead and sorts its entries: those // pointing outside the head region are preserved for the rebuilt SeekHead - // except a post-cluster Tags, Chapters or Attachments element, recorded with its @@ -798,7 +806,7 @@ func classifyTailEntries(f mkv.ReadSeekCloser, path string, fs *mkv.FS, region * region.tailUnknown = true continue } - region.tail[e.ID] = tailElement{pos: abs, size: int64(hdrLen) + th.Size, entry: e} + region.tail[e.ID] = tailElement{pos: abs, size: heldSize(f, abs, int64(hdrLen)+th.Size), entry: e} continue } if (e.ID == mkv.IDInfo || e.ID == mkv.IDTracks) && region.inHead[e.ID] { diff --git a/mkv/ops/inplace_tail_past_eof_test.go b/mkv/ops/inplace_tail_past_eof_test.go new file mode 100644 index 0000000..6358ebe --- /dev/null +++ b/mkv/ops/inplace_tail_past_eof_test.go @@ -0,0 +1,28 @@ +package ops + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/gravity-zero/mkvgo/mkv" +) + +// A tail Tags element declaring more bytes than the file holds (a file cut short, a forged size) is not voided over +// its declared size: the edit wrote that many bytes, past the end of the file, from one allocation of the same size. +func TestEditInPlaceVoidsNoTailElementPastTheFile(t *testing.T) { + path := filepath.Join(t.TempDir(), "cut.mkv") + tailTagsMKV(t, path, 2) + whole, err := os.Stat(path) + mustNil(t, err) + mustNil(t, os.Truncate(path, whole.Size()-100)) + err = EditInPlace(context.Background(), path, func(c *mkv.Container) { + c.Tags = []mkv.Tag{{TargetType: "MOVIE", SimpleTags: []mkv.SimpleTag{{Name: "A", Value: "b"}}}} + }) + after, serr := os.Stat(path) + mustNil(t, serr) + if after.Size() > whole.Size()-100 { + t.Errorf("the edit (err %v) grew a file of %d bytes to %d", err, whole.Size()-100, after.Size()) + } +} diff --git a/mkv/writer/writer.go b/mkv/writer/writer.go index c4e2994..4e6a75c 100644 --- a/mkv/writer/writer.go +++ b/mkv/writer/writer.go @@ -1123,7 +1123,10 @@ func WriteVoid(w io.Writer, totalSize int) error { if _, err := w.Write(hdr); err != nil { return err } - _, err = w.Write(make([]byte, totalSize-len(hdr))) + zeros := make([]byte, min(totalSize-len(hdr), 64<<10)) + for left := totalSize - len(hdr); left > 0 && err == nil; left -= len(zeros) { + _, err = w.Write(zeros[:min(left, len(zeros))]) + } return err } From aa1cff6f54097ed547c2dbbc4b39761255ea0880 Mon Sep 17 00:00:00 2001 From: gravity-zero Date: Mon, 9 Nov 2026 23:14:39 +0100 Subject: [PATCH 03/10] fix(ops): an in-place edit replaces an Info or Tracks an earlier edit moved to the end of the Segment, where it appended a second copy --- mkv/ops/inplace.go | 5 +-- mkv/ops/inplace_append_twice_test.go | 50 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 mkv/ops/inplace_append_twice_test.go diff --git a/mkv/ops/inplace.go b/mkv/ops/inplace.go index 3ef5576..1f229b8 100644 --- a/mkv/ops/inplace.go +++ b/mkv/ops/inplace.go @@ -794,7 +794,8 @@ func classifyTailEntries(f mkv.ReadSeekCloser, path string, fs *mkv.FS, region * if abs >= region.start && abs < region.end { continue // head element: rewritten, gets a fresh entry } - if isTailID(e.ID) { + movedOut := (e.ID == mkv.IDInfo || e.ID == mkv.IDTracks) && !region.inHead[e.ID] // an earlier edit moved it to the Segment's end + if isTailID(e.ID) || movedOut { if _, err := f.Seek(abs, io.SeekStart); err != nil { return err } @@ -809,7 +810,7 @@ func classifyTailEntries(f mkv.ReadSeekCloser, path string, fs *mkv.FS, region * region.tail[e.ID] = tailElement{pos: abs, size: heldSize(f, abs, int64(hdrLen)+th.Size), entry: e} continue } - if (e.ID == mkv.IDInfo || e.ID == mkv.IDTracks) && region.inHead[e.ID] { + if e.ID == mkv.IDInfo || e.ID == mkv.IDTracks { continue // stale: the head holds the element, which gets a fresh entry or moves with one } region.keptSeekEntries = append(region.keptSeekEntries, e) diff --git a/mkv/ops/inplace_append_twice_test.go b/mkv/ops/inplace_append_twice_test.go new file mode 100644 index 0000000..372c882 --- /dev/null +++ b/mkv/ops/inplace_append_twice_test.go @@ -0,0 +1,50 @@ +package ops + +import ( + "bytes" + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/gravity-zero/mkvgo/mkv" + "github.com/gravity-zero/mkvgo/mkv/reader" +) + +// A second edit of a file whose Info an earlier edit moved to the Segment's end is the one every reader sees, and +// leaves one Info in the file: it appended a second copy behind the first, which the readers kept returning. +func TestEditInPlaceTwiceOnATightHeadKeepsTheLastEdit(t *testing.T) { + path := filepath.Join(t.TempDir(), "tight.mkv") + tightHeadMKV(t, path, nil) + ctx := context.Background() + first, second := "A"+strings.Repeat("a", 300), "B"+strings.Repeat("b", 600) + mustNil(t, EditInPlace(ctx, path, func(c *mkv.Container) { c.Info.Title = first })) + mustNil(t, EditInPlace(ctx, path, func(c *mkv.Container) { c.Info.Title = second })) + full, err := reader.Open(ctx, path) + mustNil(t, err) + meta, err := reader.OpenMeta(ctx, path) + mustNil(t, err) + if full.Info.Title != second || meta.Info.Title != second { + t.Errorf("titles after the second edit: full read %.1q..., head read %.1q..., want B", full.Info.Title, meta.Info.Title) + } + for _, name := range []string{"N" + strings.Repeat("n", 900), "M" + strings.Repeat("m", 1800)} { + mustNil(t, EditInPlace(ctx, path, func(c *mkv.Container) { c.Tracks[0].Name = name })) + full, err = reader.Open(ctx, path) + mustNil(t, err) + meta, err = reader.OpenMeta(ctx, path) + mustNil(t, err) + if full.Tracks[0].Name != name || meta.Tracks[0].Name != name || full.Info.Title != second || meta.Info.Title != second { + t.Errorf("after naming the track %.1q...: full read %.1q... title %.1q..., head read %.1q... title %.1q...", name, + full.Tracks[0].Name, full.Info.Title, meta.Tracks[0].Name, meta.Info.Title) + } + } + data, err := os.ReadFile(path) + mustNil(t, err) + if n := bytes.Count(data, []byte(first)); n != 0 { + t.Errorf("the first edit's title is still in the file %d time(s)", n) + } + if n := bytes.Count(data, []byte("N"+strings.Repeat("n", 900))); n != 0 { + t.Errorf("the first track name is still in the file %d time(s)", n) + } +} From 1fe18a676ca151f874c40556b4cbdad28143eefb Mon Sep 17 00:00:00 2001 From: gravity-zero Date: Mon, 9 Nov 2026 23:14:39 +0100 Subject: [PATCH 04/10] fix(ops): demux refuses an empty ALAC packet and keeps the CAF packet table as its varints --- mkv/ops/demux_caf_empty_test.go | 23 +++++++++++++++++++++++ mkv/ops/demux_format.go | 18 +++++++++--------- 2 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 mkv/ops/demux_caf_empty_test.go diff --git a/mkv/ops/demux_caf_empty_test.go b/mkv/ops/demux_caf_empty_test.go new file mode 100644 index 0000000..dc910af --- /dev/null +++ b/mkv/ops/demux_caf_empty_test.go @@ -0,0 +1,23 @@ +package ops + +import ( + "errors" + "io" + "testing" + + "github.com/gravity-zero/mkvgo/mkv" +) + +// An empty ALAC packet is refused: a laced block of 7 bytes holds 256 of them, each one more entry of the packet +// table kept until the end of the stream, 2 KiB of memory for 7 bytes read. +func TestDemuxCAFRefusesAnEmptyPacket(t *testing.T) { + f := cafALACFormat(mkv.Track{CodecPrivate: make([]byte, 24)}) + if f == nil { + t.Fatal("no CAF format for a 24-byte cookie") + } + err := f.frame(io.Discard, mkv.Block{}, nil) + var fe *frameError + if !errors.As(err, &fe) { + t.Errorf("an empty packet: err = %v, want a frame the file cannot hold", err) + } +} diff --git a/mkv/ops/demux_format.go b/mkv/ops/demux_format.go index 270bfd7..e7865cf 100644 --- a/mkv/ops/demux_format.go +++ b/mkv/ops/demux_format.go @@ -204,8 +204,8 @@ func cafALACFormat(t mkv.Track) *demuxFormat { if rate == 0 && t.SampleRate != nil { rate = *t.SampleRate } - var sizes []int64 - var dataSizePos, total int64 + var table []byte // the packet table: each packet's size as a varint, 1 or 2 bytes a packet + var dataSizePos, total, packets int64 return &demuxFormat{ext: "caf", head: func(w io.Writer) error { var b []byte @@ -227,8 +227,12 @@ func cafALACFormat(t mkv.Track) *demuxFormat { return err }, frame: func(w io.Writer, _ mkv.Block, data []byte) error { - sizes = append(sizes, int64(len(data))) + if len(data) == 0 { + return cannotHold("empty ALAC packet") + } + table = cafVarint(table, uint64(len(data))) total += int64(len(data)) + packets++ _, err := w.Write(data) return err }, @@ -244,14 +248,10 @@ func cafALACFormat(t mkv.Track) *demuxFormat { if _, err := f.Seek(0, io.SeekEnd); err != nil { return err } - var table []byte - for _, n := range sizes { - table = cafVarint(table, uint64(n)) - } var pakt []byte pakt = cafChunk(pakt, "pakt", int64(24+len(table))) - pakt = binary.BigEndian.AppendUint64(pakt, uint64(len(sizes))) - pakt = binary.BigEndian.AppendUint64(pakt, uint64(len(sizes))*uint64(framesPerPacket)) + pakt = binary.BigEndian.AppendUint64(pakt, uint64(packets)) + pakt = binary.BigEndian.AppendUint64(pakt, uint64(packets)*uint64(framesPerPacket)) pakt = binary.BigEndian.AppendUint32(pakt, 0) // priming frames pakt = binary.BigEndian.AppendUint32(pakt, 0) // remainder frames pakt = append(pakt, table...) From 971756698a13d799c75a63bcd242d82a5de69930 Mon Sep 17 00:00:00 2001 From: gravity-zero Date: Mon, 9 Nov 2026 23:14:39 +0100 Subject: [PATCH 05/10] fix(mp4): an encrypted text subtitle is refused or dropped by the HLS producers, no longer served as WebVTT --- mp4/hls.go | 10 ++++++++++ mp4/hls_encrypted_subtitle_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 mp4/hls_encrypted_subtitle_test.go diff --git a/mp4/hls.go b/mp4/hls.go index 69b3b72..98db696 100644 --- a/mp4/hls.go +++ b/mp4/hls.go @@ -516,6 +516,16 @@ func planSubTracks(c *mkv.Container, o Options) ([]hlsSubTrack, error) { if t.Type != mkv.SubtitleTrack { continue } + if t.Encryption != "" { // its bytes are not its cues: refused or dropped as towards MP4, never served as text + reason := "encrypted (" + t.Encryption + "): mkvgo does not decrypt" + if !o.SkipUnsupported { + return nil, errf("track %d (%s) is %s (set Options.SkipUnsupported to drop it)", t.ID, t.Codec, reason) + } + if err := o.drop(DroppedTrack{ID: t.ID, Type: t.Type, Codec: t.Codec, Reason: reason}); err != nil { + return nil, err + } + continue + } var reason string switch canonicalSubCodec(t.Codec) { case "srt", "webvtt", "ass", "ssa": diff --git a/mp4/hls_encrypted_subtitle_test.go b/mp4/hls_encrypted_subtitle_test.go new file mode 100644 index 0000000..0ea8a50 --- /dev/null +++ b/mp4/hls_encrypted_subtitle_test.go @@ -0,0 +1,25 @@ +package mp4 + +import ( + "strings" + "testing" + + "github.com/gravity-zero/mkvgo/mkv" +) + +// An encrypted text subtitle is no WebVTT rendition: refused by name, or dropped with its reason under +// SkipUnsupported, as towards MP4. The HLS producers listed it and built its cues from the undecrypted bytes. +func TestHLSCarriesNoEncryptedSubtitle(t *testing.T) { + c := &mkv.Container{Tracks: []mkv.Track{{ID: 3, Type: mkv.SubtitleTrack, Codec: "srt", Encryption: "AES-CTR"}}} + if subs, err := planSubTracks(c, Options{}); err == nil || !strings.Contains(err.Error(), "track 3 (srt) is encrypted") { + t.Errorf("%d renditions, err = %v; want the track refused as encrypted", len(subs), err) + } + var dropped []DroppedTrack + subs, err := planSubTracks(c, Options{SkipUnsupported: true, OnDrop: func(d DroppedTrack) { dropped = append(dropped, d) }}) + if err != nil || len(subs) != 0 || len(dropped) != 1 || dropped[0].ID != 3 { + t.Errorf("under SkipUnsupported: %d renditions, dropped %+v, err %v; want none, track 3 dropped", len(subs), dropped, err) + } + if _, err := planSubTracks(c, Options{SkipUnsupported: true, Strict: true}); err == nil { + t.Error("Strict let an encrypted subtitle be left out") + } +} From bbdd4b90ee13c3b7a198ed8cfc3a93d132ab7dc1 Mon Sep 17 00:00:00 2001 From: gravity-zero Date: Mon, 9 Nov 2026 23:14:39 +0100 Subject: [PATCH 06/10] fix(mp4): an indexed subtitle scan of a remote source asks for its blocks, not for the whole stride ahead --- mp4/hlsplan_subtitle.go | 8 +++- mp4/remote_subtitle_index_test.go | 74 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 mp4/remote_subtitle_index_test.go diff --git a/mp4/hlsplan_subtitle.go b/mp4/hlsplan_subtitle.go index 1e5ff21..902e371 100644 --- a/mp4/hlsplan_subtitle.go +++ b/mp4/hlsplan_subtitle.go @@ -5,6 +5,7 @@ import ( "errors" "io" "math" + "os" "sort" "strings" @@ -377,8 +378,11 @@ func (p *HLSPlan) extendCursorIndexed(ctx context.Context, i int, cur *subCursor } // The blocks sit in the clusters up to stopAt, read a few KiB at a time from scattered positions ahead of the // media: asked for whole first, they come in the disk's own request sizes and the pages left behind no - // longer split the windows' read-ahead. - p.hintRange(src, from, p.clusterEndOff(stopAt)) + // longer split the windows' read-ahead. A remote source is asked for each block as it is reached: the whole + // range in one request is the media between them too, a minute of the file for a few KiB of cues. + if _, local := src.(*os.File); local { + p.hintRange(src, from, p.clusterEndOff(stopAt)) + } j := sort.Search(len(entries), func(k int) bool { return entries[k].Pos.ClusterStart >= from }) var local []subtitle.Cue for ; j < len(entries); j++ { diff --git a/mp4/remote_subtitle_index_test.go b/mp4/remote_subtitle_index_test.go new file mode 100644 index 0000000..2d1cf2c --- /dev/null +++ b/mp4/remote_subtitle_index_test.go @@ -0,0 +1,74 @@ +package mp4 + +import ( + "context" + "os" + "strconv" + "strings" + "testing" + + "github.com/gravity-zero/mkvgo/httpfs" + "github.com/gravity-zero/mkvgo/mkv/reader" +) + +// An indexed subtitle scan over a remote source asks for its blocks, a window each: asked for the whole stride ahead +// (a minute of the file), every seek to the next block asked for the rest of it again and gave up the response. +func TestRemoteIndexedSubtitleScanAsksForItsBlocksOnly(t *testing.T) { + const src = "../internal/testdata/sample_subs.mkv" + ctx := context.Background() + c, err := reader.Open(ctx, src) + if err != nil { + t.Fatal(err) + } + f, err := os.Open(src) + if err != nil { + t.Fatal(err) + } + defer f.Close() + br, err := reader.NewBlockReader(f, c.Info.TimecodeScale) + if err != nil { + t.Fatal(err) + } + ix := fakeSubIndex{track: 2} + for { + b, err := br.Next() + if err != nil { + break + } + if b.TrackNumber == 2 { + ix.blocks = append(ix.blocks, reader.IndexedBlock{Pos: br.Pos(), TimeMs: b.Timecode, Frames: 1}) + } + } + rs := newRemoteSource(t, src) + const window = 4 << 10 + fs := httpfs.New(httpfs.Options{WindowSize: window}).Port() + plan, err := PlanHLS(ctx, rs.srv.URL+"/s.mkv", Options{SegmentMs: 2000, FS: fs, StreamFromFS: true, SubtitleIndex: ix}) + if err != nil { + t.Fatal(err) + } + rs.mu.Lock() + rs.ranges = nil + rs.mu.Unlock() + if _, err := plan.subtitleSegment(ctx, 0, 0); err != nil { + t.Fatal(err) + } + if plan.stats.SubtitleIndexedBlocks == 0 { + t.Fatal("the index did not serve the scan") + } + rs.mu.Lock() + defer rs.mu.Unlock() + var asked int64 + for _, r := range rs.ranges { + lo, hi, ok := strings.Cut(strings.TrimPrefix(r, "bytes="), "-") + a, _ := strconv.ParseInt(lo, 10, 64) + b, err := strconv.ParseInt(hi, 10, 64) + if !ok || err != nil { + t.Fatalf("range %q", r) + } + asked += b - a + 1 + if b-a+1 > 2*window { + t.Errorf("a request of %d bytes (%s) for a subtitle block, window %d", b-a+1, r, window) + } + } + t.Logf("%d requests, %d bytes asked for %d indexed blocks", len(rs.ranges), asked, plan.stats.SubtitleIndexedBlocks) +} From 82233e4d769550504533fd8ae62abbde867fcd58 Mon Sep 17 00:00:00 2001 From: gravity-zero Date: Mon, 9 Nov 2026 23:14:40 +0100 Subject: [PATCH 07/10] fix(cli): every command's refusal names the flags, not the library's Options fields --- cmd/mkvgo/commands/helpers.go | 4 +++- cmd/mkvgo/commands/strict_split_cli_test.go | 22 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 cmd/mkvgo/commands/strict_split_cli_test.go diff --git a/cmd/mkvgo/commands/helpers.go b/cmd/mkvgo/commands/helpers.go index 401ff1c..ef80316 100644 --- a/cmd/mkvgo/commands/helpers.go +++ b/cmd/mkvgo/commands/helpers.go @@ -202,7 +202,7 @@ func FatalErr(err error) { dieOfInterrupt() return } - msg := err.Error() + msg := flagPhrases.Replace(err.Error()) // every command exits here: the library's option names read as its flags if errors.Is(err, matroska.ErrDamagedRegion) { msg += "\nrepair it with `mkvgo reindex --resync`" } @@ -227,6 +227,8 @@ var flagPhrases = strings.NewReplacer( "which DemuxOptions.Strict refuses", "which --strict refuses", "which Options.Strict refuses", "which --strict refuses", "set Options.FlattenStyledSubs", "pass --flatten-subs", + "plan with Options.SynthesizeIndex", "pass --synthesize-index", + "Options.NoIFrames is set", "--no-iframes is set", ) func (f flagWords) Error() string { return flagPhrases.Replace(f.error.Error()) } diff --git a/cmd/mkvgo/commands/strict_split_cli_test.go b/cmd/mkvgo/commands/strict_split_cli_test.go new file mode 100644 index 0000000..8159a88 --- /dev/null +++ b/cmd/mkvgo/commands/strict_split_cli_test.go @@ -0,0 +1,22 @@ +package commands_test + +import ( + "path/filepath" + "strings" + "testing" + + "github.com/gravity-zero/mkvgo/cmd/mkvgo/commands" +) + +// --strict is global: a command other than the remuxes that refuses a dropped track names the flag too, not the +// library's Options field (split printed "which Options.Strict refuses"). +func TestStrictRefusalNamesTheFlagOnEveryCommand(t *testing.T) { + commands.Strict = true + t.Cleanup(func() { commands.Strict = false }) + msg := fatalMessage(t, 1, func() { + commands.CmdSplit([]string{"../../../internal/testdata/tmcd.mov", "-o", filepath.Join(t.TempDir(), "parts"), "-every", "1000"}) + }) + if !strings.Contains(msg, "--strict") || strings.Contains(msg, "Options.") { + t.Errorf("split --strict: %q, want the track refused by --strict", msg) + } +} From fad70593fbd198f3b9993a72bf2c7cefe29c424e Mon Sep 17 00:00:00 2001 From: gravity-zero Date: Mon, 9 Nov 2026 23:14:40 +0100 Subject: [PATCH 08/10] ci: the js/wasm tests run under Node on Linux, and the preflight vets them --- .github/workflows/ci.yml | 4 ++++ Makefile | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed24535..14ca15c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,6 +44,10 @@ jobs: if: runner.os == 'Linux' run: make wasm-smoke + - name: Test (WebAssembly, Node) + if: runner.os == 'Linux' + run: GOOS=js GOARCH=wasm go test -count=1 -exec "$(go env GOROOT)/lib/wasm/go_js_wasm_exec" ./cmd/mkvgo-wasm/ + - name: Build (32-bit) shell: bash run: GOOS=linux GOARCH=arm CGO_ENABLED=0 go build ./... diff --git a/Makefile b/Makefile index 7f6b71a..fc5e7d8 100644 --- a/Makefile +++ b/Makefile @@ -78,7 +78,7 @@ preflight: GOOS=$$os CGO_ENABLED=0 go build ./... || exit 1; \ GOOS=$$os CGO_ENABLED=0 go vet ./... || exit 1; \ done - @echo "== wasm build =="; GOOS=js GOARCH=wasm CGO_ENABLED=0 go build ./cmd/mkvgo-wasm/ + @echo "== wasm build+vet =="; GOOS=js GOARCH=wasm CGO_ENABLED=0 go build ./cmd/mkvgo-wasm/ && GOOS=js GOARCH=wasm CGO_ENABLED=0 go vet ./cmd/mkvgo-wasm/ @echo "== GOARCH=arm (32-bit) build =="; GOOS=linux GOARCH=arm CGO_ENABLED=0 go build ./... @echo "== lint =="; \ lint="$$(command -v golangci-lint || echo "$$(go env GOPATH)/bin/golangci-lint")"; \ From d126e11df0cd36f52248e1eba2366a34a0a8054f Mon Sep 17 00:00:00 2001 From: gravity-zero Date: Mon, 9 Nov 2026 23:14:40 +0100 Subject: [PATCH 09/10] docs(changelog): the audit fixes of 2026-11-09 under Unreleased --- CHANGELOG.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 969689d..519653e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,37 @@ its series' file in [changelog/](changelog/) when a newer one ships. ## [Unreleased] +### Security + +- `serve`, `PlanHLS`: a Matroska whose Segment declares more bytes than the + file holds (a forged size, a file cut short) no longer has a subtitle + request ask the kernel to read ahead over the declared size, one call per + chunk, with the rendition locked meanwhile (since 0.66.0). +- `edit-inplace`: a Tags, Chapters or Attachments element after the clusters + that declares more bytes than the file holds is voided up to the end of the + file; the edit wrote its declared size, from one allocation of that size. +- `demux`: an empty ALAC packet is refused by name (`--raw` keeps the stored + payloads); a few bytes of laced empty packets held 2 KiB of packet table + each (since 0.66.0). + +### Fixed + +- `edit-inplace`: a second edit of a file whose Info or Tracks an earlier + edit had moved to the end of the Segment replaces them. It appended a + second copy behind the first, which a full read kept returning: the edit + reported success and was not seen (since 0.66.0). A file edited twice by + 0.66.0 or 0.66.1 is repaired by `mkvgo edit` (full rewrite). +- `to-hls`, `serve` and the other HLS commands: an encrypted text subtitle + track is refused by name, or dropped with its reason under + `--skip-unsupported`, as `to-mp4` does; since 0.66.0 it was listed as a + WebVTT rendition built from its undecrypted bytes. +- `serve` on a remote source with a subtitle index: the scan asks for each + block it reads again, not for the whole minute of media ahead in one request + (since 0.66.0). +- Every command names `--strict`, `--skip-unsupported`, `--synthesize-index` + and `--no-iframes` in its refusals; all but `demux`, `to-mp4`, `from-mp4` + and `to-hls` printed the library's `Options` field names. + ## [0.66.1] - 2026-11-08 ### Fixed From 0ff84d2f01b63ec01491f4585ecbb8d31aa8dd70 Mon Sep 17 00:00:00 2001 From: gravity-zero Date: Tue, 10 Nov 2026 09:17:03 +0100 Subject: [PATCH 10/10] docs(changelog): 0.66.2 dated 2026-11-10, 0.66.1 archived in changelog/0.60-0.69.md --- CHANGELOG.md | 18 ++---------------- changelog/0.60-0.69.md | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 519653e..4115b52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ its series' file in [changelog/](changelog/) when a newer one ships. ## [Unreleased] +## [0.66.2] - 2026-11-10 + ### Security - `serve`, `PlanHLS`: a Matroska whose Segment declares more bytes than the @@ -39,19 +41,3 @@ its series' file in [changelog/](changelog/) when a newer one ships. - Every command names `--strict`, `--skip-unsupported`, `--synthesize-index` and `--no-iframes` in its refusals; all but `demux`, `to-mp4`, `from-mp4` and `to-hls` printed the library's `Options` field names. - -## [0.66.1] - 2026-11-08 - -### Fixed - -- HLS plans (`serve`, `Open`): a chmod, a chown or a hard link of the source - no longer counts as a changed source. 0.66.0 had added the inode change - time to the source stamp, so any of them made every later segment of a - running serve fail with `ErrSourceChanged` (HTTP 502) until the process was - restarted, and a player gave up after its retries. The stamp is the size, - the mtime and the file identity again; a file rewritten in place at the - same size with its mtime put back is, as before 0.66.0, not detected. - -## Older versions - -Archived in [changelog/](changelog/): [0.60-0.69](changelog/0.60-0.69.md), [0.56-0.59](changelog/0.56-0.59.md), [0.50-0.55](changelog/0.50-0.55.md), [0.40-0.49](changelog/0.40-0.49.md), [0.30-0.39](changelog/0.30-0.39.md), [0.20-0.29](changelog/0.20-0.29.md), [0.10-0.19](changelog/0.10-0.19.md), [0.3-0.9](changelog/0.3-0.9.md). diff --git a/changelog/0.60-0.69.md b/changelog/0.60-0.69.md index 3bef3fc..72a32c2 100644 --- a/changelog/0.60-0.69.md +++ b/changelog/0.60-0.69.md @@ -2,6 +2,22 @@ Archived entries of mkvgo 0.60.x to 0.69.x, as they were published; a version moves here once a newer one ships. The current versions are in [CHANGELOG.md](../CHANGELOG.md). +## [0.66.1] - 2026-11-08 + +### Fixed + +- HLS plans (`serve`, `Open`): a chmod, a chown or a hard link of the source + no longer counts as a changed source. 0.66.0 had added the inode change + time to the source stamp, so any of them made every later segment of a + running serve fail with `ErrSourceChanged` (HTTP 502) until the process was + restarted, and a player gave up after its retries. The stamp is the size, + the mtime and the file identity again; a file rewritten in place at the + same size with its mtime put back is, as before 0.66.0, not detected. + +## Older versions + +Archived in [changelog/](changelog/): [0.60-0.69](changelog/0.60-0.69.md), [0.56-0.59](changelog/0.56-0.59.md), [0.50-0.55](changelog/0.50-0.55.md), [0.40-0.49](changelog/0.40-0.49.md), [0.30-0.39](changelog/0.30-0.39.md), [0.20-0.29](changelog/0.20-0.29.md), [0.10-0.19](changelog/0.10-0.19.md), [0.3-0.9](changelog/0.3-0.9.md). + ## [0.66.0] - 2026-11-08 ### Breaking