1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-22 17:18:39 +00:00

Fix operator[] for vector2d and vector3d being potentially UB (#15977)

We don't have a C++ expert on hand, but taking a pointer to one member
and expecting to access another by an offset is very fishy:
- for one, there could theoretically be padding
- the compiler might assume that we are only writing to that first member

The new code has shown to be free for constant parameter values.
Non-constant ones cause the assembly to have branches (why?), but we don't
use that much.
This commit is contained in:
sfan5 2025-04-08 22:25:45 +02:00 committed by GitHub
parent 46db688cc8
commit a00b9cab36
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 39 additions and 30 deletions

View file

@ -10,22 +10,6 @@
#include "ILogger.h"
#include "ITimer.h"
// CODE_UNREACHABLE(): Invokes undefined behavior for unreachable code optimization
#if defined(__cpp_lib_unreachable)
#include <utility>
#define CODE_UNREACHABLE() std::unreachable()
#elif defined(__has_builtin)
#if __has_builtin(__builtin_unreachable)
#define CODE_UNREACHABLE() __builtin_unreachable()
#endif
#elif defined(_MSC_VER)
#define CODE_UNREACHABLE() __assume(false)
#endif
#ifndef CODE_UNREACHABLE
#define CODE_UNREACHABLE() (void)0
#endif
namespace irr
{