Skip to content

Commit 0c8b65d

Browse files
committed
Fix memory error for AudioFifo properties
Validate self.ptr before access: uninitialized properties should return None rather than crash the Python interpreter
1 parent 7299106 commit 0c8b65d

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

av/audio/fifo.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,14 @@ cdef class AudioFifo:
176176
@property
177177
def format(self):
178178
"""The :class:`.AudioFormat` of this FIFO."""
179-
return self.template.format
179+
return self.template.format if self.ptr else None
180180
@property
181181
def layout(self):
182182
"""The :class:`.AudioLayout` of this FIFO."""
183-
return self.template.layout
183+
return self.template.layout if self.ptr else None
184184
@property
185185
def sample_rate(self):
186-
return self.template.sample_rate
186+
return self.template.sample_rate if self.ptr else None
187187

188188
@property
189189
def samples(self):

tests/test_audiofifo.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,10 @@ def test_missing_time_base(self) -> None:
115115
assert oframe is not None
116116
assert oframe.pts is None and oframe.time_base is None
117117
assert oframe.sample_rate == iframe.sample_rate
118+
119+
def test_missing_template(self) -> None:
120+
fifo = av.AudioFifo()
121+
assert fifo.format is None
122+
assert fifo.layout is None
123+
assert fifo.sample_rate is None
124+
assert fifo.samples == 0

0 commit comments

Comments
 (0)