Skip to content

Add support for fade transitions for music segments#10

Closed
Kolfering wants to merge 1 commit intomasterfrom
music-review
Closed

Add support for fade transitions for music segments#10
Kolfering wants to merge 1 commit intomasterfrom
music-review

Conversation

@Kolfering
Copy link
Owner

No description provided.

auto slot = Music::instance()->GetSlot(index);
if (!slot) return luaL_error(L, "request_transition: index out of bounds");

auto [music_index, preset_index] = Get_MusicPreset(static_cast<uint32_t>(Lua_MusicPreset::Index(L, 2)));

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable music_index is not used.
auto slot = Music::instance()->GetSlot(music_index);
if (!slot) return luaL_error(L, "add_segment: index out of bounds");

auto segment_index = slot->AddSegmentToPreset(preset_index, lua_tonumber(L, 2));

Check warning

Code scanning / CodeQL

Lossy function result cast Warning

Return value of type double is implicitly converted to unsigned int.

Copilot Autofix

AI 6 months ago

The best way to fix this problem is to explicitly handle conversion from lua_tonumber to integer, clarifying the intended behaviour. The code should explicitly round, floor, ceil, or truncate as appropriate, and check that the value is within the allowed range for uint32_t (i.e., >= 0 and <= UINT32_MAX), giving an error if the value is invalid.

The recommended fix is to:

  • Retrieve the value from lua_tonumber(L, 2) into a lua_Number variable.
  • Check that the value is integral (e.g., that it matches its uint32_t representation, or that it equals floor(val)).
  • Check that the value is within the range for uint32_t (0 ≤ val ≤ UINT32_MAX).
  • If checks pass, cast to uint32_t (using static_cast<uint32_t>(val)).
  • If invalid, return a Lua error.

You may need to #include <cmath> for floor or similar, and possibly #include <limits> for UINT32_MAX.

All changes should be made only in the code snippets shown, specifically in the implementation of Lua_MusicPreset_Add_Segment around line 325.

Suggested changeset 1
Source_Files/Lua/lua_music.cpp

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/Source_Files/Lua/lua_music.cpp b/Source_Files/Lua/lua_music.cpp
--- a/Source_Files/Lua/lua_music.cpp
+++ b/Source_Files/Lua/lua_music.cpp
@@ -1,5 +1,7 @@
 #include "lua_music.h"
 #include "Music.h"
+#include <cmath>
+#include <limits>
 
 static std::vector<std::pair<uint32_t, uint32_t>> music_presets_indexes;
 static std::vector<std::tuple<uint32_t, uint32_t, uint32_t>> music_segment_indexes;
@@ -322,7 +324,11 @@
 	auto slot = Music::instance()->GetSlot(music_index);
 	if (!slot) return luaL_error(L, "add_segment: index out of bounds");
 
-	auto segment_index = slot->AddSegmentToPreset(preset_index, lua_tonumber(L, 2));
+	lua_Number segment_num = lua_tonumber(L, 2);
+	if (segment_num < 0 || segment_num > static_cast<lua_Number>(std::numeric_limits<uint32_t>::max()) || segment_num != floor(segment_num)) {
+		return luaL_error(L, "add_segment: segment index must be an integer between 0 and %u", std::numeric_limits<uint32_t>::max());
+	}
+	auto segment_index = slot->AddSegmentToPreset(preset_index, static_cast<uint32_t>(segment_num));
 	if (!segment_index.has_value()) return luaL_error(L, "add_segment: index out of bounds");
 
 	Lua_MusicSegment::Push(L, Add_MusicSegment(music_index, preset_index, segment_index.value()));
EOF
@@ -1,5 +1,7 @@
#include "lua_music.h"
#include "Music.h"
#include <cmath>
#include <limits>

static std::vector<std::pair<uint32_t, uint32_t>> music_presets_indexes;
static std::vector<std::tuple<uint32_t, uint32_t, uint32_t>> music_segment_indexes;
@@ -322,7 +324,11 @@
auto slot = Music::instance()->GetSlot(music_index);
if (!slot) return luaL_error(L, "add_segment: index out of bounds");

auto segment_index = slot->AddSegmentToPreset(preset_index, lua_tonumber(L, 2));
lua_Number segment_num = lua_tonumber(L, 2);
if (segment_num < 0 || segment_num > static_cast<lua_Number>(std::numeric_limits<uint32_t>::max()) || segment_num != floor(segment_num)) {
return luaL_error(L, "add_segment: segment index must be an integer between 0 and %u", std::numeric_limits<uint32_t>::max());
}
auto segment_index = slot->AddSegmentToPreset(preset_index, static_cast<uint32_t>(segment_num));
if (!segment_index.has_value()) return luaL_error(L, "add_segment: index out of bounds");

Lua_MusicSegment::Push(L, Add_MusicSegment(music_index, preset_index, segment_index.value()));
Copilot is powered by AI and may make mistakes. Always verify output.
static int Lua_MusicSegment_Get_Preset(lua_State* L)
{
auto index = static_cast<uint32_t>(Lua_MusicSegment::Index(L, 1));
auto [music_index, preset_index, segment_index] = Get_MusicSegment(index);

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable segment_index is not used.

Copilot Autofix

AI 6 months ago

The best way to fix this problem is to remove the unused variable from the unpacking/destructuring assignment on line 368 in Source_Files/Lua/lua_music.cpp. Specifically, change the line

auto [music_index, preset_index, segment_index] = Get_MusicSegment(index);

to

auto [music_index, preset_index, /*segment_index*/] = Get_MusicSegment(index);

or, preferably, omit it entirely via C++17's structured binding and the ignore-diskards:

In C++, if you are using structured bindings and don't need one of the unpacked values, you can simply ignore it, like so:

auto [music_index, preset_index, std::ignore] = Get_MusicSegment(index);

But C++17 does not actually support std::ignore for structured bindings; it's usually used only for std::tuple::tie(). Therefore, the standard way is just to declare only the variables you need. Since the current code returns a std::tuple<uint32_t, uint32_t, uint32_t>, and structured bindings force you to bind all three values, a workaround is to retrieve the tuple into a named variable, and then unpack only the necessary elements, or to keep the unused variable but name it _ to indicate that it's intentionally unused.

But to keep things clean, it's best just to bind the variables you actually use, or assign the unused value to a variable named _ as per common convention.

Modification required:

  • On line 368, change auto [music_index, preset_index, segment_index] = ... to auto [music_index, preset_index, _] = ... or, if your team does not use _ for unused variables, simply revert to using a std::tuple and access only the first two indices.

But as per convention and for clarity, replacing segment_index with _ will suffice.

No other changes or new imports/methods are required.

Suggested changeset 1
Source_Files/Lua/lua_music.cpp

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/Source_Files/Lua/lua_music.cpp b/Source_Files/Lua/lua_music.cpp
--- a/Source_Files/Lua/lua_music.cpp
+++ b/Source_Files/Lua/lua_music.cpp
@@ -365,7 +365,7 @@
 static int Lua_MusicSegment_Get_Preset(lua_State* L)
 {
 	auto index = static_cast<uint32_t>(Lua_MusicSegment::Index(L, 1));
-	auto [music_index, preset_index, segment_index] = Get_MusicSegment(index);
+	auto [music_index, preset_index, _] = Get_MusicSegment(index);
 	auto lua_preset_index = Get_LuaMusicPreset(music_index, preset_index);
 	Lua_MusicPreset::Push(L, lua_preset_index);
 	return 1;
EOF
@@ -365,7 +365,7 @@
static int Lua_MusicSegment_Get_Preset(lua_State* L)
{
auto index = static_cast<uint32_t>(Lua_MusicSegment::Index(L, 1));
auto [music_index, preset_index, segment_index] = Get_MusicSegment(index);
auto [music_index, preset_index, _] = Get_MusicSegment(index);
auto lua_preset_index = Get_LuaMusicPreset(music_index, preset_index);
Lua_MusicPreset::Push(L, lua_preset_index);
return 1;
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant