Skip to content

Commit 45eaeba

Browse files
authored
Prevent segfault on data stream template
1 parent cddcef8 commit 45eaeba

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

av/container/output.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ def add_stream_from_template(
144144
if opaque is None:
145145
opaque = template.type != "video"
146146

147+
if template.codec_context is None:
148+
raise ValueError(
149+
f"template stream of type {template.type} has no codec context"
150+
)
151+
147152
codec_obj: Codec
148153
if opaque: # Copy ctx from template.
149154
codec_obj = template.codec_context.codec

tests/test_streams.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from fractions import Fraction
3+
from typing import Any, cast
34

45
import pytest
56

@@ -146,3 +147,23 @@ def test_data_stream(self) -> None:
146147
assert repr.startswith("<av.DataStream #0") and repr.endswith(">")
147148

148149
container.close()
150+
151+
def test_data_stream_from_template(self) -> None:
152+
"""Test that adding a data stream from a template raises ValueError."""
153+
154+
# Open an existing container with a data stream
155+
input_container = av.open(fate_suite("mxf/track_01_v02.mxf"))
156+
input_data_stream = input_container.streams.data[0]
157+
158+
# Create a new container and ensure using a data stream as a template raises ValueError
159+
output_container = av.open("out.mkv", "w")
160+
with pytest.raises(ValueError):
161+
# input_data_stream is a DataStream at runtime; the test asserts that
162+
# using it as a template raises ValueError. The static type stubs
163+
# intentionally restrict which Stream subclasses are valid templates,
164+
# so cast to Any here to keep the runtime check while satisfying
165+
# the type checker.
166+
output_container.add_stream_from_template(cast(Any, input_data_stream))
167+
168+
input_container.close()
169+
output_container.close()

0 commit comments

Comments
 (0)