Skip to content

Commit 3bf4dff

Browse files
oddbookwormankith26
authored andcommitted
Prevent segfault due to division by zero in music.get_pos (#2426)
music.get_pos now returns -1 on failure
1 parent d8e8ea2 commit 3bf4dff

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

docs/reST/ref/music.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ MP3 in most cases.
217217
The returned time only represents how long the music has been playing; it
218218
does not take into account any starting position offsets.
219219

220+
Returns -1 if ``get_pos`` failed due to music not playing.
221+
220222
.. ## pygame.mixer.music.get_pos ##
221223
222224
.. function:: queue

src_c/music.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,13 @@ music_get_pos(PyObject *self, PyObject *_null)
252252

253253
MIXER_INIT_CHECK();
254254

255-
if (music_pos_time < 0)
255+
Uint16 intermediate_step = (music_format & 0xff) >> 3;
256+
long denominator = music_channels * music_frequency * intermediate_step;
257+
if (music_pos_time < 0 || denominator == 0) {
256258
return PyLong_FromLong(-1);
259+
}
257260

258-
ticks = (long)(1000 * music_pos /
259-
(music_channels * music_frequency *
260-
((music_format & 0xff) >> 3)));
261+
ticks = (long)(1000 * music_pos / denominator);
261262
if (!Mix_PausedMusic())
262263
ticks += PG_GetTicks() - music_pos_time;
263264

0 commit comments

Comments
 (0)