From d063aa077a481000192fbff9c5d1fa7c47962ba7 Mon Sep 17 00:00:00 2001 From: Alexander van Gessel Date: Fri, 20 Oct 2017 17:31:09 +0200 Subject: [PATCH] Don't use invalid music indices. Fixes #1861 --- src/scripting/lua_audio.cpp | 4 ++-- src/sound.cpp | 6 +++++- src/sound.hpp | 3 ++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/scripting/lua_audio.cpp b/src/scripting/lua_audio.cpp index 23122c9c8c5f..8df985b3bf39 100644 --- a/src/scripting/lua_audio.cpp +++ b/src/scripting/lua_audio.cpp @@ -96,8 +96,8 @@ static int impl_music_get(lua_State* L) { } if(strcmp(m, "current_i") == 0) { - size_t i = sound::get_current_track(); - if(i == sound::get_num_tracks()) { + size_t i = sound::get_current_track_index(); + if(i >= sound::get_num_tracks()) { lua_pushnil(L); } else { lua_pushinteger(L, i + 1); diff --git a/src/sound.cpp b/src/sound.cpp index 71f0ae08d274..cf458967339e 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -190,10 +190,14 @@ std::vector>::const_iterator find_track(cons namespace sound { -unsigned int get_current_track() +unsigned int get_current_track_index() { return current_track_index; } +std::shared_ptr get_current_track() +{ + return current_track; +} std::shared_ptr get_previous_music_track() { return previous_track; diff --git a/src/sound.hpp b/src/sound.hpp index 20eb6ff6dc68..c1707c5e9fff 100644 --- a/src/sound.hpp +++ b/src/sound.hpp @@ -104,7 +104,8 @@ void set_sound_volume(int vol); void set_bell_volume(int vol); void set_UI_volume(int vol); -unsigned int get_current_track(); +unsigned int get_current_track_index(); // This function may return a value >= get_num_tracks(). Use with caution +std::shared_ptr get_current_track(); std::shared_ptr get_previous_music_track(); void set_previous_track(std::shared_ptr); unsigned int get_num_tracks();