mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-11 17:51:04 +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:
parent
46db688cc8
commit
a00b9cab36
5 changed files with 39 additions and 30 deletions
|
@ -131,16 +131,20 @@ public:
|
|||
|
||||
T &operator[](u32 index)
|
||||
{
|
||||
_IRR_DEBUG_BREAK_IF(index > 1) // access violation
|
||||
|
||||
return *(&X + index);
|
||||
switch (index) {
|
||||
case 0: return X;
|
||||
case 1: return Y;
|
||||
default: IRR_CODE_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
const T &operator[](u32 index) const
|
||||
{
|
||||
_IRR_DEBUG_BREAK_IF(index > 1) // access violation
|
||||
|
||||
return *(&X + index);
|
||||
switch (index) {
|
||||
case 0: return X;
|
||||
case 1: return Y;
|
||||
default: IRR_CODE_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
//! sort in order X, Y.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue