Fix: Avoid integer overflow in inversion op
- closes https://github.com/assimp/assimp/issues/3424pull/5068/head
parent
074af85734
commit
d51f033e20
|
@ -39,7 +39,7 @@
|
||||||
|
|
||||||
namespace Assimp {
|
namespace Assimp {
|
||||||
|
|
||||||
const double fast_atof_table[16] = { // we write [16] here instead of [] to work around a swig bug
|
constexpr double fast_atof_table[16] = { // we write [16] here instead of [] to work around a swig bug
|
||||||
0.0,
|
0.0,
|
||||||
0.1,
|
0.1,
|
||||||
0.01,
|
0.01,
|
||||||
|
@ -58,12 +58,10 @@ const double fast_atof_table[16] = { // we write [16] here instead of [] to wo
|
||||||
0.000000000000001
|
0.000000000000001
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------
|
||||||
// Convert a string in decimal format to a number
|
// Convert a string in decimal format to a number
|
||||||
// ------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------
|
||||||
inline
|
inline unsigned int strtoul10( const char* in, const char** out=0) {
|
||||||
unsigned int strtoul10( const char* in, const char** out=0) {
|
|
||||||
unsigned int value = 0;
|
unsigned int value = 0;
|
||||||
|
|
||||||
for ( ;; ) {
|
for ( ;; ) {
|
||||||
|
@ -83,8 +81,7 @@ unsigned int strtoul10( const char* in, const char** out=0) {
|
||||||
// ------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------
|
||||||
// Convert a string in octal format to a number
|
// Convert a string in octal format to a number
|
||||||
// ------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------
|
||||||
inline
|
inline unsigned int strtoul8( const char* in, const char** out=0) {
|
||||||
unsigned int strtoul8( const char* in, const char** out=0) {
|
|
||||||
unsigned int value( 0 );
|
unsigned int value( 0 );
|
||||||
for ( ;; ) {
|
for ( ;; ) {
|
||||||
if ( *in < '0' || *in > '7' ) {
|
if ( *in < '0' || *in > '7' ) {
|
||||||
|
@ -103,8 +100,7 @@ unsigned int strtoul8( const char* in, const char** out=0) {
|
||||||
// ------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------
|
||||||
// Convert a string in hex format to a number
|
// Convert a string in hex format to a number
|
||||||
// ------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------
|
||||||
inline
|
inline unsigned int strtoul16( const char* in, const char** out=0) {
|
||||||
unsigned int strtoul16( const char* in, const char** out=0) {
|
|
||||||
unsigned int value( 0 );
|
unsigned int value( 0 );
|
||||||
for ( ;; ) {
|
for ( ;; ) {
|
||||||
if ( *in >= '0' && *in <= '9' ) {
|
if ( *in >= '0' && *in <= '9' ) {
|
||||||
|
@ -128,8 +124,7 @@ unsigned int strtoul16( const char* in, const char** out=0) {
|
||||||
// Convert just one hex digit
|
// Convert just one hex digit
|
||||||
// Return value is UINT_MAX if the input character is not a hex digit.
|
// Return value is UINT_MAX if the input character is not a hex digit.
|
||||||
// ------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------
|
||||||
inline
|
inline unsigned int HexDigitToDecimal(char in) {
|
||||||
unsigned int HexDigitToDecimal(char in) {
|
|
||||||
unsigned int out( UINT_MAX );
|
unsigned int out( UINT_MAX );
|
||||||
if ( in >= '0' && in <= '9' ) {
|
if ( in >= '0' && in <= '9' ) {
|
||||||
out = in - '0';
|
out = in - '0';
|
||||||
|
@ -146,16 +141,14 @@ unsigned int HexDigitToDecimal(char in) {
|
||||||
// ------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------
|
||||||
// Convert a hex-encoded octet (2 characters, i.e. df or 1a).
|
// Convert a hex-encoded octet (2 characters, i.e. df or 1a).
|
||||||
// ------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------
|
||||||
inline
|
inline uint8_t HexOctetToDecimal(const char* in) {
|
||||||
uint8_t HexOctetToDecimal(const char* in) {
|
|
||||||
return ((uint8_t)HexDigitToDecimal(in[0])<<4)+(uint8_t)HexDigitToDecimal(in[1]);
|
return ((uint8_t)HexDigitToDecimal(in[0])<<4)+(uint8_t)HexDigitToDecimal(in[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------
|
||||||
// signed variant of strtoul10
|
// signed variant of strtoul10
|
||||||
// ------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------
|
||||||
inline
|
inline int strtol10( const char* in, const char** out=0) {
|
||||||
int strtol10( const char* in, const char** out=0) {
|
|
||||||
bool inv = (*in=='-');
|
bool inv = (*in=='-');
|
||||||
if ( inv || *in == '+' ) {
|
if ( inv || *in == '+' ) {
|
||||||
++in;
|
++in;
|
||||||
|
@ -163,7 +156,11 @@ int strtol10( const char* in, const char** out=0) {
|
||||||
|
|
||||||
int value = strtoul10(in,out);
|
int value = strtoul10(in,out);
|
||||||
if (inv) {
|
if (inv) {
|
||||||
value = -value;
|
if (value < INT_MAX) {
|
||||||
|
value = -value;
|
||||||
|
} else {
|
||||||
|
ASSIMP_LOG_WARN( "Converting the string \"", in, "\" into an inverted value resulted in overflow." );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
@ -174,8 +171,7 @@ int strtol10( const char* in, const char** out=0) {
|
||||||
// 0NNN - oct
|
// 0NNN - oct
|
||||||
// NNN - dec
|
// NNN - dec
|
||||||
// ------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------
|
||||||
inline
|
inline unsigned int strtoul_cppstyle( const char* in, const char** out=0) {
|
||||||
unsigned int strtoul_cppstyle( const char* in, const char** out=0) {
|
|
||||||
if ('0' == in[0]) {
|
if ('0' == in[0]) {
|
||||||
return 'x' == in[1] ? strtoul16(in+2,out) : strtoul8(in+1,out);
|
return 'x' == in[1] ? strtoul16(in+2,out) : strtoul8(in+1,out);
|
||||||
}
|
}
|
||||||
|
@ -187,8 +183,7 @@ unsigned int strtoul_cppstyle( const char* in, const char** out=0) {
|
||||||
// It is mainly used by fast_atof to prevent ugly and unwanted integer overflows.
|
// It is mainly used by fast_atof to prevent ugly and unwanted integer overflows.
|
||||||
// ------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------
|
||||||
template<typename ExceptionType = DeadlyImportError>
|
template<typename ExceptionType = DeadlyImportError>
|
||||||
inline
|
inline uint64_t strtoul10_64( const char* in, const char** out=0, unsigned int* max_inout=0) {
|
||||||
uint64_t strtoul10_64( const char* in, const char** out=0, unsigned int* max_inout=0) {
|
|
||||||
unsigned int cur = 0;
|
unsigned int cur = 0;
|
||||||
uint64_t value = 0;
|
uint64_t value = 0;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue