Browse Source

Merge pull request #4532 from assimp/kimkulling/fix_undefined_behaviour_in_superfasthast_issue4531

Add unittest to reproduce undefined behaviour
Kim Kulling 3 years ago
parent
commit
4d451fe437
4 changed files with 61 additions and 11 deletions
  1. 4 8
      include/assimp/Hash.h
  2. 1 1
      test/CMakeLists.txt
  3. 56 0
      test/unit/Common/utHash.cpp
  4. 0 2
      test/unit/MathTest.cpp

+ 4 - 8
include/assimp/Hash.h

@@ -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);

+ 1 - 1
test/CMakeLists.txt

@@ -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

+ 56 - 0
test/unit/Common/utHash.cpp

@@ -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);
+}

+ 0 - 2
test/unit/MathTest.cpp

@@ -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,