Skip to content

Commit 53480a9

Browse files
authored
fix: underflow due to sub_segment data (#63)
1 parent f94a216 commit 53480a9

File tree

7 files changed

+83
-56
lines changed

7 files changed

+83
-56
lines changed

.golangci.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ run:
66
linters-settings:
77
errcheck:
88
check-type-assertions: true
9+
gosec:
10+
excludes:
11+
- G115
912

1013
linters:
1114
enable:
@@ -17,7 +20,6 @@ linters:
1720
- errname
1821
- errorlint
1922
- exhaustive
20-
- exportloopref
2123
- gci
2224
- gochecknoinits
2325
- gocritic

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ require (
88
github.com/bamiaux/iobit v0.0.0-20170418073505-498159a04883
99
github.com/spf13/cobra v1.8.1
1010
github.com/stretchr/testify v1.9.0
11-
golang.org/x/text v0.16.0
11+
golang.org/x/text v0.22.0
1212
)
1313

1414
require (
1515
github.com/davecgh/go-spew v1.1.1 // indirect
1616
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1717
github.com/kr/pretty v0.1.0 // indirect
1818
github.com/pmezard/go-difflib v1.0.0 // indirect
19-
github.com/spf13/pflag v1.0.5 // indirect
19+
github.com/spf13/pflag v1.0.6 // indirect
2020
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
2121
gopkg.in/yaml.v3 v3.0.1 // indirect
2222
)

go.sum

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
1515
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
1616
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
1717
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
18-
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
1918
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
19+
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
20+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
2021
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
2122
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
22-
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
23-
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
23+
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
24+
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
2425
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2526
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
2627
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

pkg/scte35/scte35_test.go

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestDecodeBase64(t *testing.T) {
5353
EncryptedPacket: scte35.EncryptedPacket{EncryptionAlgorithm: scte35.EncryptionAlgorithmNone, CWIndex: 255},
5454
SpliceCommand: &scte35.TimeSignal{
5555
SpliceTime: scte35.SpliceTime{
56-
PTSTime: uint64ptr(0x072bd0050),
56+
PTSTime: ptr(uint64(0x072bd0050)),
5757
},
5858
},
5959
SpliceDescriptors: []scte35.SpliceDescriptor{
@@ -65,7 +65,7 @@ func TestDecodeBase64(t *testing.T) {
6565
},
6666
SegmentationEventID: uint32(0x4800008e),
6767
SegmentationTypeID: scte35.SegmentationTypeProviderPOStart,
68-
SegmentationDuration: uint64ptr(0x0001a599b0),
68+
SegmentationDuration: ptr(uint64(0x0001a599b0)),
6969
SegmentationUPIDs: []scte35.SegmentationUPID{
7070
scte35.NewSegmentationUPID(scte35.SegmentationUPIDTypeTI, toBytes(0x000000002ca0a18a)),
7171
},
@@ -89,7 +89,7 @@ func TestDecodeBase64(t *testing.T) {
8989
OutOfNetworkIndicator: true,
9090
Program: &scte35.SpliceInsertProgram{
9191
SpliceTime: scte35.SpliceTime{
92-
PTSTime: uint64ptr(0x07369c02e),
92+
PTSTime: ptr(uint64(0x07369c02e)),
9393
},
9494
},
9595
},
@@ -108,7 +108,7 @@ func TestDecodeBase64(t *testing.T) {
108108
EncryptedPacket: scte35.EncryptedPacket{EncryptionAlgorithm: scte35.EncryptionAlgorithmNone, CWIndex: 255},
109109
SpliceCommand: &scte35.TimeSignal{
110110
SpliceTime: scte35.SpliceTime{
111-
PTSTime: uint64ptr(0x0746290a0),
111+
PTSTime: ptr(uint64(0x0746290a0)),
112112
},
113113
},
114114
SpliceDescriptors: []scte35.SpliceDescriptor{
@@ -137,7 +137,7 @@ func TestDecodeBase64(t *testing.T) {
137137
EncryptedPacket: scte35.EncryptedPacket{EncryptionAlgorithm: scte35.EncryptionAlgorithmNone, CWIndex: 255},
138138
SpliceCommand: &scte35.TimeSignal{
139139
SpliceTime: scte35.SpliceTime{
140-
PTSTime: uint64ptr(0x07a4d88b6),
140+
PTSTime: ptr(uint64(0x07a4d88b6)),
141141
},
142142
},
143143
SpliceDescriptors: []scte35.SpliceDescriptor{
@@ -178,7 +178,7 @@ func TestDecodeBase64(t *testing.T) {
178178
EncryptedPacket: scte35.EncryptedPacket{EncryptionAlgorithm: scte35.EncryptionAlgorithmNone, CWIndex: 255},
179179
SpliceCommand: &scte35.TimeSignal{
180180
SpliceTime: scte35.SpliceTime{
181-
PTSTime: uint64ptr(0x0aebfff64),
181+
PTSTime: ptr(uint64(0x0aebfff64)),
182182
},
183183
},
184184
SpliceDescriptors: []scte35.SpliceDescriptor{
@@ -206,7 +206,7 @@ func TestDecodeBase64(t *testing.T) {
206206
EncryptedPacket: scte35.EncryptedPacket{EncryptionAlgorithm: scte35.EncryptionAlgorithmNone, CWIndex: 255},
207207
SpliceCommand: &scte35.TimeSignal{
208208
SpliceTime: scte35.SpliceTime{
209-
PTSTime: uint64ptr(0x0932e380b),
209+
PTSTime: ptr(uint64(0x0932e380b)),
210210
},
211211
},
212212
SpliceDescriptors: []scte35.SpliceDescriptor{
@@ -247,7 +247,7 @@ func TestDecodeBase64(t *testing.T) {
247247
EncryptedPacket: scte35.EncryptedPacket{EncryptionAlgorithm: scte35.EncryptionAlgorithmNone, CWIndex: 255},
248248
SpliceCommand: &scte35.TimeSignal{
249249
SpliceTime: scte35.SpliceTime{
250-
PTSTime: uint64ptr(0x0aef17c4c),
250+
PTSTime: ptr(uint64(0x0aef17c4c)),
251251
},
252252
},
253253
SpliceDescriptors: []scte35.SpliceDescriptor{
@@ -275,7 +275,7 @@ func TestDecodeBase64(t *testing.T) {
275275
EncryptedPacket: scte35.EncryptedPacket{EncryptionAlgorithm: scte35.EncryptionAlgorithmNone, CWIndex: 255},
276276
SpliceCommand: &scte35.TimeSignal{
277277
SpliceTime: scte35.SpliceTime{
278-
PTSTime: uint64ptr(0x0a8cd44ed),
278+
PTSTime: ptr(uint64(0x0a8cd44ed)),
279279
},
280280
},
281281
SpliceDescriptors: []scte35.SpliceDescriptor{
@@ -460,11 +460,11 @@ func TestDecodeBase64(t *testing.T) {
460460
{
461461
Type: scte35.SegmentationUPIDTypeMPU,
462462
Format: scte35.SegmentationUPIDFormatBase64,
463-
FormatIdentifier: uint32ptr(1145656131),
463+
FormatIdentifier: ptr(uint32(1145656131)),
464464
Value: "WU1XRjA0NTIwMDBI",
465465
},
466466
},
467-
SegmentationDuration: uint64ptr(10800000),
467+
SegmentationDuration: ptr(uint64(10800000)),
468468
SegmentationEventID: 39965,
469469
SegmentationTypeID: scte35.SegmentationTypeProviderAdEnd,
470470
SegmentNum: 1,
@@ -497,7 +497,7 @@ func TestDecodeBase64(t *testing.T) {
497497
Value: "urn:nbcuni.com:brc:499866434",
498498
},
499499
},
500-
SegmentationDuration: uint64ptr(1347087),
500+
SegmentationDuration: ptr(uint64(1347087)),
501501
SegmentationEventID: 4294967295,
502502
SegmentationTypeID: scte35.SegmentationTypeProviderAdEnd,
503503
SegmentNum: 10,
@@ -534,12 +534,47 @@ func TestDecodeBase64(t *testing.T) {
534534
},
535535
legacy: true, // binary wont match because of stuffing
536536
},
537+
"Unused Subsegments Included": {
538+
binary: "/DCRAAAAAAAAAP/wBQb/9peOEAB7AjhDVUVJAAAAnH+/DilhdmFpbGlkPTkxNDg2NjA2NSZiaXRtYXA9JmluYWN0aXZpdHk9MzEyMDEHCgI/Q1VFSQAAAJ1//wAANu6ADilhdmFpbGlkPTkxMDkwMTM4OSZiaXRtYXA9JmluYWN0aXZpdHk9MzEyMDAICgAAoJMeaA==",
539+
expected: scte35.SpliceInfoSection{
540+
SAPType: scte35.SAPTypeNotSpecified,
541+
Tier: 4095,
542+
SpliceCommand: &scte35.TimeSignal{
543+
SpliceTime: scte35.SpliceTime{
544+
PTSTime: ptr(uint64(8432094736)),
545+
},
546+
},
547+
SpliceDescriptors: scte35.SpliceDescriptors{
548+
&scte35.SegmentationDescriptor{
549+
SegmentationEventID: uint32(156),
550+
SegmentationTypeID: scte35.SegmentationTypeProviderAdEnd,
551+
SegmentNum: 7,
552+
SegmentsExpected: 10,
553+
SegmentationUPIDs: []scte35.SegmentationUPID{
554+
{Type: scte35.SegmentationUPIDTypeADS, Format: scte35.SegmentationUPIDFormatText, Value: "availid=914866065\u0026bitmap=\u0026inactivity=3120"},
555+
},
556+
},
557+
&scte35.SegmentationDescriptor{
558+
SegmentationEventID: uint32(157),
559+
SegmentationTypeID: scte35.SegmentationTypeProviderAdStart,
560+
SegmentationDuration: ptr(uint64(3600000)),
561+
SegmentNum: 8,
562+
SegmentsExpected: 10,
563+
SubSegmentNum: ptr(uint32(0)),
564+
SubSegmentsExpected: ptr(uint32(0)),
565+
SegmentationUPIDs: []scte35.SegmentationUPID{
566+
{Type: scte35.SegmentationUPIDTypeADS, Format: scte35.SegmentationUPIDFormatText, Value: "availid=910901389\u0026bitmap=\u0026inactivity=3120"},
567+
},
568+
},
569+
},
570+
},
571+
},
537572
"UPID with Valid ASCII Invalid UTF8": {
538573
binary: "/DDHAAAAABc0AP/wBQb/tVo+agCxAhdDVUVJQA4hwH+fCAgAAAAAPj6IcCMAAAIXQ1VFSUAOI1x/nwgIAAAAAD4+iHARAAACF0NVRUlADiHgf58ICAAAAAA+Poi2EAAAAhxDVUVJQA4hyn/fAABSlKwICAAAAAA+Poi2IgAAAkZDVUVJQA4h1n/PAABSlKwNMgoMFHf5uXs0AAAAAAAADhh0eXBlPUxBJmR1cj02MDAwMCZ0aWVy/DDHAAAAAAAAAP8ABQb/HPCt2w==",
539574
expected: scte35.SpliceInfoSection{
540575
SpliceCommand: &scte35.TimeSignal{
541576
SpliceTime: scte35.SpliceTime{
542-
PTSTime: uint64ptr(7337557610),
577+
PTSTime: ptr(uint64(7337557610)),
543578
},
544579
},
545580
SpliceDescriptors: scte35.SpliceDescriptors{
@@ -584,7 +619,7 @@ func TestDecodeBase64(t *testing.T) {
584619
},
585620
&scte35.SegmentationDescriptor{
586621
SegmentationEventID: uint32(1074667978),
587-
SegmentationDuration: uint64ptr(5412012),
622+
SegmentationDuration: ptr(uint64(5412012)),
588623
SegmentationTypeID: scte35.SegmentationTypeBreakStart,
589624
DeliveryRestrictions: &scte35.DeliveryRestrictions{
590625
ArchiveAllowedFlag: true,
@@ -599,7 +634,7 @@ func TestDecodeBase64(t *testing.T) {
599634
&scte35.SegmentationDescriptor{
600635
SegmentationEventID: uint32(1074667990),
601636
SegmentationTypeID: 0x05,
602-
SegmentationDuration: uint64ptr(5412012),
637+
SegmentationDuration: ptr(uint64(5412012)),
603638
SegmentNum: 6,
604639
SegmentsExpected: 255,
605640
DeliveryRestrictions: &scte35.DeliveryRestrictions{
@@ -678,7 +713,7 @@ func TestDecodeHex(t *testing.T) {
678713
EncryptedPacket: scte35.EncryptedPacket{EncryptionAlgorithm: scte35.EncryptionAlgorithmNone, CWIndex: 255},
679714
SpliceCommand: &scte35.TimeSignal{
680715
SpliceTime: scte35.SpliceTime{
681-
PTSTime: uint64ptr(0x072bd0050),
716+
PTSTime: ptr(uint64(0x072bd0050)),
682717
},
683718
},
684719
SpliceDescriptors: []scte35.SpliceDescriptor{
@@ -690,7 +725,7 @@ func TestDecodeHex(t *testing.T) {
690725
},
691726
SegmentationEventID: uint32(0x4800008e),
692727
SegmentationTypeID: scte35.SegmentationTypeProviderPOStart,
693-
SegmentationDuration: uint64ptr(0x0001a599b0),
728+
SegmentationDuration: ptr(uint64(0x0001a599b0)),
694729
SegmentationUPIDs: []scte35.SegmentationUPID{
695730
scte35.NewSegmentationUPID(scte35.SegmentationUPIDTypeTI, toBytes(0x000000002ca0a18a)),
696731
},
@@ -714,7 +749,7 @@ func TestDecodeHex(t *testing.T) {
714749
OutOfNetworkIndicator: true,
715750
Program: &scte35.SpliceInsertProgram{
716751
SpliceTime: scte35.SpliceTime{
717-
PTSTime: uint64ptr(0x07369c02e),
752+
PTSTime: ptr(uint64(0x07369c02e)),
718753
},
719754
},
720755
},
@@ -803,10 +838,6 @@ func toXML(sis *scte35.SpliceInfoSection) string {
803838
return string(b)
804839
}
805840

806-
func uint32ptr(i uint32) *uint32 {
807-
return &i
808-
}
809-
810-
func uint64ptr(i uint64) *uint64 {
841+
func ptr[T any](i T) *T {
811842
return &i
812843
}

pkg/scte35/segmentation_descriptor.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,6 @@ func (sd *SegmentationDescriptor) SegmentationUpidLength() int {
326326

327327
// decode updates this splice_descriptor from binary.
328328
func (sd *SegmentationDescriptor) decode(b []byte) error {
329-
var err error
330-
331329
r := iobit.NewReader(b)
332330
r.Skip(8) // splice_descriptor_tag
333331
r.Skip(8) // descriptor_length
@@ -399,21 +397,15 @@ func (sd *SegmentationDescriptor) decode(b []byte) error {
399397
sd.SegmentNum = r.Uint32(8)
400398
sd.SegmentsExpected = r.Uint32(8)
401399

402-
// these fields are new in 2016 so we need a secondary check whether
403-
// they were actually included in the binary payload
404-
if sd.SegmentationTypeID == SegmentationTypeProviderPOStart || sd.SegmentationTypeID == SegmentationTypeDistributorPOStart {
405-
if r.LeftBits() == 16 {
406-
n := r.Uint32(8)
407-
e := r.Uint32(8)
408-
sd.SubSegmentNum = &n
409-
sd.SubSegmentsExpected = &e
410-
}
400+
// If exactly 2 bytes remain, these are subsegment indicators.
401+
if r.LeftBits() == 16 {
402+
n := r.Uint32(8)
403+
e := r.Uint32(8)
404+
sd.SubSegmentNum = &n
405+
sd.SubSegmentsExpected = &e
411406
}
412407
}
413408

414-
if err != nil {
415-
return err
416-
}
417409
if err := readerError(r); err != nil {
418410
return fmt.Errorf("segmentation_descriptor: %w", err)
419411
}

pkg/scte35/splice_descriptor.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,11 @@ func (sds *SpliceDescriptors) UnmarshalXML(d *xml.Decoder, start xml.StartElemen
155155
func decodeSpliceDescriptors(b []byte) ([]SpliceDescriptor, error) {
156156
r := iobit.NewReader(b)
157157

158+
var err error
158159
var sds []SpliceDescriptor
159160
for r.LeftBits() > 0 {
160161
// Peek to get splice_descriptor_tag, descriptor_length, and
161-
// identifier
162+
// identifier.
162163
sdr := r.Peek()
163164
spliceDescriptorTag := sdr.Uint32(8)
164165
descriptorLength := int(sdr.Uint32(8))
@@ -167,12 +168,12 @@ func decodeSpliceDescriptors(b []byte) ([]SpliceDescriptor, error) {
167168
// Decode the full splice_descriptor (including splice_descriptor_tag
168169
// and descriptor_length).
169170
sd := NewSpliceDescriptor(identifier, spliceDescriptorTag)
170-
err := sd.decode(r.Bytes(descriptorLength + 2))
171-
if err != nil {
172-
return sds, err
171+
if derr := sd.decode(r.Bytes(descriptorLength + 2)); derr != nil {
172+
// Store the error but continue decoding the rest of the signal.
173+
err = derr
173174
}
174175
sds = append(sds, sd)
175176
}
176177

177-
return sds, nil
178+
return sds, err
178179
}

0 commit comments

Comments
 (0)