Skip to content

Commit

Permalink
Prevent segfault due to division by zero in music.get_pos (pygame-com…
Browse files Browse the repository at this point in the history
…munity#2426)

music.get_pos now returns -1 on failure
  • Loading branch information
oddbookworm committed Aug 30, 2023
1 parent 578ed7d commit 9e6133a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/reST/ref/music.rst
Expand Up @@ -217,6 +217,8 @@ MP3 in most cases.
The returned time only represents how long the music has been playing; it
does not take into account any starting position offsets.

Returns -1 if ``get_pos`` failed due to music not playing.

.. ## pygame.mixer.music.get_pos ##
.. function:: queue
Expand Down
9 changes: 5 additions & 4 deletions src_c/music.c
Expand Up @@ -252,12 +252,13 @@ music_get_pos(PyObject *self, PyObject *_null)

MIXER_INIT_CHECK();

if (music_pos_time < 0)
Uint16 intermediate_step = (music_format & 0xff) >> 3;
long denominator = music_channels * music_frequency * intermediate_step;
if (music_pos_time < 0 || denominator == 0) {
return PyLong_FromLong(-1);
}

ticks = (long)(1000 * music_pos /
(music_channels * music_frequency *
((music_format & 0xff) >> 3)));
ticks = (long)(1000 * music_pos / denominator);
if (!Mix_PausedMusic())
ticks += PG_GetTicks() - music_pos_time;

Expand Down

0 comments on commit 9e6133a

Please sign in to comment.