Merge pull request #4532 from assimp/kimkulling/fix_undefined_behaviour_in_superfasthast_issue4531

Add unittest to reproduce undefined behaviour
pull/4537/head
Kim Kulling 2022-05-17 22:08:54 +02:00 committed by GitHub
commit 4d451fe437
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 11 deletions

View File

@ -48,6 +48,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdint.h>
#include <string.h>
#include <cmath>
// ------------------------------------------------------------------------------------------------
// Hashing function taken from
@ -73,9 +74,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ------------------------------------------------------------------------------------------------
inline uint32_t SuperFastHash (const char * data, uint32_t len = 0, uint32_t hash = 0) {
uint32_t tmp;
int rem;
size_t offset;
uint32_t tmp;
int rem;
if (!data) return 0;
if (!len)len = (uint32_t)::strlen(data);
@ -96,11 +96,7 @@ size_t offset;
switch (rem) {
case 3: hash += get16bits (data);
hash ^= hash << 16;
offset = static_cast<size_t>(sizeof(uint16_t));
if (offset < 0) {
return 0;
}
hash ^= data[offset] << 18;
hash ^= abs(data[sizeof(uint16_t)]) << 18;
hash += hash >> 11;
break;
case 2: hash += get16bits (data);

View File

@ -1,6 +1,5 @@
# Open Asset Import Library (assimp)
# ----------------------------------------------------------------------
#
# Copyright (c) 2006-2022, assimp team
#
# All rights reserved.
@ -72,6 +71,7 @@ SET( COMMON
unit/AssimpAPITest_aiQuaternion.cpp
unit/AssimpAPITest_aiVector2D.cpp
unit/AssimpAPITest_aiVector3D.cpp
unit/Common/utHash.cpp
unit/MathTest.cpp
unit/MathTest.h
unit/RandomNumberGeneration.h

View File

@ -0,0 +1,56 @@
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2022, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
#include "UnitTestPCH.h"
#include <assimp/Hash.h>
using namespace Assimp;
class utHash : public ::testing::Test {
// empty
};
TEST_F( utHash, SuperFastHashTest ) {
const char *Data = "-21416115v";
auto result = SuperFastHash(Data, 10);
EXPECT_NE(0, result);
}

View File

@ -5,8 +5,6 @@ Open Asset Import Library (assimp)
Copyright (c) 2006-2022, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,