mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-19 22:44:59 +01:00
Fix the four issues failing the Sonar quality gate on master (#323)
* fix(scripting): forward the object in push_object of the Lua screens (cpp:S5417) push_object takes a forwarding reference and called std::move on it, which SonarCloud reports as a blocker bug. It only worked because every caller passes an rvalue: with an lvalue Type_ is deduced as a reference, and new Type_(std::move(Object)) doesn't even compile. The object is now forwarded, and the userdata is sized and built for std::remove_cvref_t<Type_>, the type collect<> and check_object<> are used with: an rvalue is moved in, an lvalue would be copied. Every caller passes an rvalue of lua_image, bitmap_font or glyph, so they move the object in exactly as before. * fix(simulation): turbidity from the overcast without branches of the same value (cpp:S3923) The chain of conditionals setting Global.fTurbidity had pairs of branches with the same result (<= 0.10 and <= 0.20 both 4, and so on, the last two both 9), which SonarCloud reports as a bug. The thresholds that changed nothing are gone and the mapping is a small function: up to 0.20 gives 4, up to 0.40 5, up to 0.60 6, up to 0.70 7, up to 0.80 8, anything else 9. The comparisons are the same ones, in the same order, so every overcast value gives the turbidity it gave before. * test: script screen tests keep their files in the build folder, not in the temporary one (cpp:S5443) The folders of the script screen host tests were made in the system's temporary directory, and the sandbox test put its "outside.txt" straight into that directory. SonarCloud reports both uses as vulnerabilities: the directory is writable by everyone. The folders are now made in the build folder of the unit tests, passed to them as EU07_UNIT_TESTS_SCRATCH, the way EU07_UNIT_TESTS_DATA is. The scripts are one level down in the folder of a test, so the file the sandbox test puts outside the folder of the scripts is still inside the folder of the test and goes with it. Nothing depends on the working directory, and nothing is left behind.
This commit is contained in:
@@ -33,6 +33,8 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include <iterator>
|
||||
#include <new>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace script_screens
|
||||
{
|
||||
@@ -106,10 +108,13 @@ void count_hook(lua_State *L, lua_Debug * /*Debug*/)
|
||||
|
||||
// objects of the api -------------------------------------------------------------------------------------------------
|
||||
|
||||
template <typename Type_> Type_ *push_object(lua_State *L, Type_ &&Object, char const *Metatable)
|
||||
// puts the object in a new userdata with the metatable, left on the stack: an rvalue is moved in, an lvalue copied.
|
||||
// what's stored is the plain type (no reference, no const), the one collect<> and check_object<> are given
|
||||
template <typename Type_> std::remove_cvref_t<Type_> *push_object(lua_State *L, Type_ &&Object, char const *Metatable)
|
||||
{
|
||||
auto *memory{lua_newuserdata(L, sizeof(Type_))};
|
||||
auto *object{new (memory) Type_(std::move(Object))};
|
||||
using object_type = std::remove_cvref_t<Type_>;
|
||||
auto *memory{lua_newuserdata(L, sizeof(object_type))};
|
||||
auto *object{new (memory) object_type(std::forward<Type_>(Object))};
|
||||
luaL_getmetatable(L, Metatable);
|
||||
lua_setmetatable(L, -2);
|
||||
return object;
|
||||
|
||||
@@ -20,6 +20,22 @@ world_environment Environment;
|
||||
|
||||
} // simulation
|
||||
|
||||
namespace {
|
||||
|
||||
// turbidity of the sky under the given cloud cover
|
||||
float
|
||||
turbidity_of( float const Overcast ) {
|
||||
|
||||
if( Overcast <= 0.20 ) { return 4.f; }
|
||||
if( Overcast <= 0.40 ) { return 5.f; }
|
||||
if( Overcast <= 0.60 ) { return 6.f; }
|
||||
if( Overcast <= 0.70 ) { return 7.f; }
|
||||
if( Overcast <= 0.80 ) { return 8.f; }
|
||||
return 9.f;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void
|
||||
world_environment::on_daylight_change() {
|
||||
|
||||
@@ -68,16 +84,7 @@ world_environment::compute_weather() const {
|
||||
Global.AirTemperature > 1 ? "rain:" :
|
||||
"snow:";
|
||||
|
||||
Global.fTurbidity = Global.Overcast <= 0.10 ? 4 :
|
||||
Global.Overcast <= 0.20 ? 4 :
|
||||
Global.Overcast <= 0.30 ? 5 :
|
||||
Global.Overcast <= 0.40 ? 5 :
|
||||
Global.Overcast <= 0.50 ? 6 :
|
||||
Global.Overcast <= 0.60 ? 6 :
|
||||
Global.Overcast <= 0.70 ? 7 :
|
||||
Global.Overcast <= 0.80 ? 8 :
|
||||
Global.Overcast <= 0.90 ? 9 :
|
||||
9;
|
||||
Global.fTurbidity = turbidity_of( Global.Overcast );
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -37,6 +37,9 @@ endif()
|
||||
target_include_directories(${UNIT_TESTS_NAME} PRIVATE "${DEPS_DIR}/doctest")
|
||||
# the names the tests CAPTURE are often C strings, shown as text rather than pointers
|
||||
target_compile_definitions(${UNIT_TESTS_NAME} PRIVATE EU07_UNIT_TESTS_DATA="${CMAKE_CURRENT_SOURCE_DIR}/data" DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING)
|
||||
# the folders of the tests that write files (the scripts of test_script_screen_host.cpp) are made in the build folder,
|
||||
# and removed by the test that made them
|
||||
target_compile_definitions(${UNIT_TESTS_NAME} PRIVATE EU07_UNIT_TESTS_SCRATCH="${CMAKE_CURRENT_BINARY_DIR}")
|
||||
if (MSVC)
|
||||
target_compile_options(${UNIT_TESTS_NAME} PRIVATE /bigobj)
|
||||
endif()
|
||||
|
||||
@@ -77,17 +77,20 @@ struct recording_texture : ITexture {
|
||||
std::vector<std::uint8_t> pixels;
|
||||
};
|
||||
|
||||
// a folder of its own for the scripts of a test, removed with the object
|
||||
// a folder of its own for the scripts of a test, removed with the object. it's made in the build folder of the tests
|
||||
// (EU07_UNIT_TESTS_SCRATCH, see CMakeLists.txt), not in the system's temporary folder, which anyone can write to. the
|
||||
// scripts are one level down in it, so a file a test puts next to their folder ("../name") is still inside the folder
|
||||
// of the test, and is removed with it
|
||||
class script_folder {
|
||||
public:
|
||||
explicit script_folder(std::string const &Name)
|
||||
: m_folder{std::filesystem::temp_directory_path() / std::format("eu07_script_screen_test_{}", Name)} {
|
||||
std::filesystem::remove_all(m_folder);
|
||||
: m_root{std::filesystem::path{EU07_UNIT_TESTS_SCRATCH} / std::format("eu07_script_screen_test_{}", Name)}, m_folder{m_root / "scripts"} {
|
||||
std::filesystem::remove_all(m_root);
|
||||
std::filesystem::create_directories(m_folder);
|
||||
}
|
||||
~script_folder() {
|
||||
std::error_code error;
|
||||
std::filesystem::remove_all(m_folder, error);
|
||||
std::filesystem::remove_all(m_root, error);
|
||||
}
|
||||
script_folder(script_folder const &) = delete;
|
||||
script_folder &operator=(script_folder const &) = delete;
|
||||
@@ -105,7 +108,8 @@ class script_folder {
|
||||
}
|
||||
|
||||
private:
|
||||
std::filesystem::path m_folder;
|
||||
std::filesystem::path m_root; // the folder of the test
|
||||
std::filesystem::path m_folder; // the folder of its scripts
|
||||
};
|
||||
|
||||
// runs frames until the condition holds, or gives up after a few seconds
|
||||
@@ -484,6 +488,7 @@ TEST_CASE("lua screens: the texture gets the bottom row first, as from the pytho
|
||||
|
||||
TEST_CASE("lua screens: a script reaches no files outside its folder, nor the system, nor ffi") {
|
||||
script_folder const folder{"lua_sandbox"};
|
||||
// next to the folder of the scripts, where the script mustn't reach
|
||||
folder.write("../outside.txt", std::string_view{"outside"});
|
||||
folder.write("data/inside.txt", std::string_view{"inside"});
|
||||
auto const script{folder.write("sandbox.lua", sandbox_script)};
|
||||
@@ -497,7 +502,6 @@ TEST_CASE("lua screens: a script reaches no files outside its folder, nor the sy
|
||||
REQUIRE(texture.pixels.size() == 4);
|
||||
CHECK(texture.pixels[0] == 255);
|
||||
queue.exit();
|
||||
std::filesystem::remove(std::filesystem::temp_directory_path() / "outside.txt");
|
||||
}
|
||||
|
||||
TEST_CASE("lua screens: precompiled code isn't loaded") {
|
||||
|
||||
Reference in New Issue
Block a user