From 2ca69b4a35c25dde8c2dec75a4f6af7f9086d5c6 Mon Sep 17 00:00:00 2001 From: Nir Azkiel <64067618+Nir-Az@users.noreply.github.com> Date: Tue, 13 Apr 2021 10:21:10 +0300 Subject: [PATCH 1/3] protect out of bound entries on floats use --- src/types.h | 71 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 7 deletions(-) diff --git a/src/types.h b/src/types.h index ca7a838f5e..c8bff84379 100644 --- a/src/types.h +++ b/src/types.h @@ -567,13 +567,70 @@ namespace librealsense //////////////////////////////////////////// // World's tiniest linear algebra library // //////////////////////////////////////////// -#pragma pack(push, 1) - struct int2 { int x, y; }; - struct float2 { float x, y; float & operator [] (int i) { return (&x)[i]; } }; - struct float3 { float x, y, z; float & operator [] (int i) { return (&x)[i]; } }; - struct float4 { float x, y, z, w; float & operator [] (int i) { return (&x)[i]; } }; - struct float3x3 { float3 x, y, z; float & operator () (int i, int j) { return (&x)[j][i]; } }; // column-major - struct pose { float3x3 orientation; float3 position; }; +#pragma pack( push, 1 ) + struct int2 + { + int x, y; + }; + struct float2 + { + float x, y; + float & operator[]( int i ) + { +#ifdef _DEBUG + + assert( i >= 0 ); + assert( i < 2 ); +#endif + return *( &x + i ); + } + }; + struct float3 + { + float x, y, z; + float & operator[]( int i ) + { +#ifdef _DEBUG + + assert( i >= 0 ); + assert( i < 3 ); +#endif + return ( *( &x + i ) ); + } + }; + struct float4 + { + float x, y, z, w; + float & operator[]( int i ) + { +#ifdef _DEBUG + + assert( i >= 0 ); + assert( i < 4 ); +#endif + return ( *( &x + i ) ); + } + }; + struct float3x3 + { + float3 x, y, z; + float & operator()( int i, int j ) + { +#ifdef _DEBUG + + assert( i >= 0 ); + assert( i < 3 ); + assert( j >= 0 ); + assert( j < 3 ); +#endif + return ( *( &x[0] + j * sizeof( float3 ) / sizeof( float ) + i ) ); + } + }; // column-major + struct pose + { + float3x3 orientation; + float3 position; + }; #pragma pack(pop) inline bool operator == (const float3 & a, const float3 & b) { return a.x == b.x && a.y == b.y && a.z == b.z; } inline float3 operator + (const float3 & a, const float3 & b) { return{ a.x + b.x, a.y + b.y, a.z + b.z }; } From 64ded1292803947302533b0d72697babf28bcec6 Mon Sep 17 00:00:00 2001 From: Nir Azkiel <64067618+Nir-Az@users.noreply.github.com> Date: Mon, 19 Apr 2021 18:14:52 +0300 Subject: [PATCH 2/3] Add additional fix --- common/viewer.cpp | 2 +- src/types.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/common/viewer.cpp b/common/viewer.cpp index ba293b000b..3f6f229933 100644 --- a/common/viewer.cpp +++ b/common/viewer.cpp @@ -6,7 +6,7 @@ #define NOMINMAX #endif #endif - + #include #include "viewer.h" diff --git a/src/types.h b/src/types.h index c8bff84379..73b12c6d65 100644 --- a/src/types.h +++ b/src/types.h @@ -395,8 +395,9 @@ namespace librealsense operator T () const { T le_value = 0; - for (unsigned int i = 0; i < sizeof(T); ++i) reinterpret_cast(&le_value)[i] = reinterpret_cast(&be_value)[sizeof(T) - i - 1]; + for (unsigned int i = 0; i < sizeof(T); ++i) *(reinterpret_cast(&le_value) + i) = *(reinterpret_cast(&be_value) + sizeof(T) - i - 1); return le_value; + } }; #pragma pack(pop) From 76e2d857d7bfa10df821caa7b372eba8062a3d01 Mon Sep 17 00:00:00 2001 From: Nir Azkiel <64067618+Nir-Az@users.noreply.github.com> Date: Tue, 20 Apr 2021 12:31:53 +0300 Subject: [PATCH 3/3] remove _DEBUG condition --- common/viewer.cpp | 2 +- src/types.h | 12 ------------ 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/common/viewer.cpp b/common/viewer.cpp index 3f6f229933..ba293b000b 100644 --- a/common/viewer.cpp +++ b/common/viewer.cpp @@ -6,7 +6,7 @@ #define NOMINMAX #endif #endif - + #include #include "viewer.h" diff --git a/src/types.h b/src/types.h index 73b12c6d65..47a6ff6653 100644 --- a/src/types.h +++ b/src/types.h @@ -578,11 +578,8 @@ namespace librealsense float x, y; float & operator[]( int i ) { -#ifdef _DEBUG - assert( i >= 0 ); assert( i < 2 ); -#endif return *( &x + i ); } }; @@ -591,11 +588,8 @@ namespace librealsense float x, y, z; float & operator[]( int i ) { -#ifdef _DEBUG - assert( i >= 0 ); assert( i < 3 ); -#endif return ( *( &x + i ) ); } }; @@ -604,11 +598,8 @@ namespace librealsense float x, y, z, w; float & operator[]( int i ) { -#ifdef _DEBUG - assert( i >= 0 ); assert( i < 4 ); -#endif return ( *( &x + i ) ); } }; @@ -617,13 +608,10 @@ namespace librealsense float3 x, y, z; float & operator()( int i, int j ) { -#ifdef _DEBUG - assert( i >= 0 ); assert( i < 3 ); assert( j >= 0 ); assert( j < 3 ); -#endif return ( *( &x[0] + j * sizeof( float3 ) / sizeof( float ) + i ) ); } }; // column-major