From 9522a0b7c16414fce2103cf58bfdd63aaf0cb01b Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Thu, 3 Nov 2022 14:58:32 -0400 Subject: DRTVWR-575: Fix llcommon assumptions that size_t fits in 4 bytes. It's a little distressing how often we have historically coded S32 or U32 to pass a length or index. There are more such assumptions in other viewer subdirectories, but this is a start. --- indra/llcommon/lldefs.h | 54 ++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'indra/llcommon/lldefs.h') diff --git a/indra/llcommon/lldefs.h b/indra/llcommon/lldefs.h index 5a4b8325f4..d9b0cbf71b 100644 --- a/indra/llcommon/lldefs.h +++ b/indra/llcommon/lldefs.h @@ -28,6 +28,7 @@ #define LL_LLDEFS_H #include "stdtypes.h" +#include // Often used array indices const U32 VX = 0; @@ -168,50 +169,51 @@ const U32 MAXADDRSTR = 17; // 123.567.901.345 = 15 chars + \0 + 1 for good luc // llclampb(a) // clamps a to [0 .. 255] // -template -inline LLDATATYPE llmax(const LLDATATYPE& d1, const LLDATATYPE& d2) +template +inline auto llmax(T1 d1, T2 d2) { return (d1 > d2) ? d1 : d2; } -template -inline LLDATATYPE llmax(const LLDATATYPE& d1, const LLDATATYPE& d2, const LLDATATYPE& d3) +template +inline auto llmax(T1 d1, T2 d2, T3 d3) { - LLDATATYPE r = llmax(d1,d2); + auto r = llmax(d1,d2); return llmax(r, d3); } -template -inline LLDATATYPE llmax(const LLDATATYPE& d1, const LLDATATYPE& d2, const LLDATATYPE& d3, const LLDATATYPE& d4) +template +inline auto llmax(T1 d1, T2 d2, T3 d3, T4 d4) { - LLDATATYPE r1 = llmax(d1,d2); - LLDATATYPE r2 = llmax(d3,d4); + auto r1 = llmax(d1,d2); + auto r2 = llmax(d3,d4); return llmax(r1, r2); } -template -inline LLDATATYPE llmin(const LLDATATYPE& d1, const LLDATATYPE& d2) +template +inline auto llmin(T1 d1, T2 d2) { return (d1 < d2) ? d1 : d2; } -template -inline LLDATATYPE llmin(const LLDATATYPE& d1, const LLDATATYPE& d2, const LLDATATYPE& d3) +template +inline auto llmin(T1 d1, T2 d2, T3 d3) { - LLDATATYPE r = llmin(d1,d2); + auto r = llmin(d1,d2); return (r < d3 ? r : d3); } -template -inline LLDATATYPE llmin(const LLDATATYPE& d1, const LLDATATYPE& d2, const LLDATATYPE& d3, const LLDATATYPE& d4) +template +inline auto llmin(T1 d1, T2 d2, T3 d3, T4 d4) { - LLDATATYPE r1 = llmin(d1,d2); - LLDATATYPE r2 = llmin(d3,d4); + auto r1 = llmin(d1,d2); + auto r2 = llmin(d3,d4); return llmin(r1, r2); } -template -inline LLDATATYPE llclamp(const LLDATATYPE& a, const LLDATATYPE& minval, const LLDATATYPE& maxval) +template +inline typename std::common_type::type +llclamp(A a, MIN minval, MAX maxval) { if ( a < minval ) { @@ -225,23 +227,21 @@ inline LLDATATYPE llclamp(const LLDATATYPE& a, const LLDATATYPE& minval, const L } template -inline LLDATATYPE llclampf(const LLDATATYPE& a) +inline LLDATATYPE llclampf(LLDATATYPE a) { - return llmin(llmax(a, (LLDATATYPE)0), (LLDATATYPE)1); + return llmin(llmax(a, LLDATATYPE(0)), LLDATATYPE(1)); } template -inline LLDATATYPE llclampb(const LLDATATYPE& a) +inline LLDATATYPE llclampb(LLDATATYPE a) { - return llmin(llmax(a, (LLDATATYPE)0), (LLDATATYPE)255); + return llmin(llmax(a, LLDATATYPE(0)), LLDATATYPE(255)); } template inline void llswap(LLDATATYPE& lhs, LLDATATYPE& rhs) { - LLDATATYPE tmp = lhs; - lhs = rhs; - rhs = tmp; + std::swap(lhs, rhs); } #endif // LL_LLDEFS_H -- cgit v1.2.3 From 4349cb6165e983ff6bdd45ad1b82bb98bfc0436f Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Sat, 12 Nov 2022 18:59:21 -1000 Subject: DRTVWR-575: Address review comments on Xcode 14.1 type tweaks. Introduce LLSD template constructors and assignment operators to disambiguate construction or assignment from any integer type to Integer, likewise any floating point type to Real. Use new narrow() function to validate conversions. For LLSD method parameters converted from LLSD::Integer to size_t, where the method previously checked for a negative argument, make it now check for size_t converted from negative: in other words, more than S32_MAX. The risk of having a parameter forced from negative to unsigned exceeds the risk of a valid length or index over that max. In lltracerecording.cpp's PeriodicRecording, now that mCurPeriod and mNumRecordedPeriods are size_t instead of S32, defend against subtracting 1 from 0. Use narrow() to validate newly-introduced narrowing conversions. Make llclamp() return the type of the raw input value, even if the types of the boundary values differ. std::ostream::tellp() no longer returns a value we can directly report as a number. Cast to U64. --- indra/llcommon/lldefs.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'indra/llcommon/lldefs.h') diff --git a/indra/llcommon/lldefs.h b/indra/llcommon/lldefs.h index d9b0cbf71b..a567fd7c12 100644 --- a/indra/llcommon/lldefs.h +++ b/indra/llcommon/lldefs.h @@ -212,16 +212,15 @@ inline auto llmin(T1 d1, T2 d2, T3 d3, T4 d4) } template -inline typename std::common_type::type -llclamp(A a, MIN minval, MAX maxval) +inline A llclamp(A a, MIN minval, MAX maxval) { if ( a < minval ) { - return minval; + return static_cast(minval); } else if ( a > maxval ) { - return maxval; + return static_cast(maxval); } return a; } -- cgit v1.2.3 From 7d9249d180f7bc228cad3d6f5ed4d5fb13296451 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Tue, 6 Dec 2022 17:16:56 -0500 Subject: DRTVWR-575: Try to avoid comparison warnings in llclamp() --- indra/llcommon/lldefs.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'indra/llcommon/lldefs.h') diff --git a/indra/llcommon/lldefs.h b/indra/llcommon/lldefs.h index a567fd7c12..5c46f6a796 100644 --- a/indra/llcommon/lldefs.h +++ b/indra/llcommon/lldefs.h @@ -214,13 +214,14 @@ inline auto llmin(T1 d1, T2 d2, T3 d3, T4 d4) template inline A llclamp(A a, MIN minval, MAX maxval) { - if ( a < minval ) + A aminval{ static_cast(minval) }, amaxval{ static_cast(maxval) }; + if ( a < aminval ) { - return static_cast(minval); + return aminval; } - else if ( a > maxval ) + else if ( a > amaxval ) { - return static_cast(maxval); + return amaxval; } return a; } -- cgit v1.2.3