Do not sanitize functions with intentional access to arrays sandbox/vbarinov/ubsan
authorSlava Barinov <v.barinov@samsung.com>
Thu, 10 Nov 2016 07:23:14 +0000 (10:23 +0300)
committerSlava Barinov <v.barinov@samsung.com>
Thu, 10 Nov 2016 07:23:14 +0000 (10:23 +0300)
Preventing
hash.c:50:22: runtime error: signed integer overflow: 44 * 596579247 cannot be represented in type 'int'
findkey.c:122:41: runtime error: index 141 out of bounds for type 'bucket_element [1]'
findkey.c:125:38: runtime error: index 141 out of bounds for type 'bucket_element [1]'
findkey.c:134:42: runtime error: index 142 out of bounds for type 'bucket_element [1]'
gdbmstore.c:138:34: runtime error: index 141 out of bounds for type 'bucket_element [1]'
gdbmstore.c:138:34: runtime error: index 142 out of bounds for type 'bucket_element [1]'
gdbmstore.c:143:27: runtime error: index 146 out of bounds for type 'bucket_element [1]'
gdbmstore.c:144:44: runtime error: index 146 out of bounds for type 'bucket_element [1]'
gdbmstore.c:150:23: runtime error: index 146 out of bounds for type 'bucket_element [1]'
gdbmstore.c:151:23: runtime error: index 146 out of bounds for type 'bucket_element [1]'
gdbmstore.c:152:23: runtime error: index 146 out of bounds for type 'bucket_element [1]'

Signed-off-by: Slava Barinov <v.barinov@samsung.com>
findkey.c
gdbmstore.c
hash.c

index ef559065b446ebd1320e3039b6a3b217ee708944..6a82ed2b92c47ef7cca72a2467de975c736004e2 100644 (file)
--- a/findkey.c
+++ b/findkey.c
@@ -23,7 +23,7 @@
                 Computer Science Department
                 Western Washington University
                 Bellingham, WA 98226
-       
+
 *************************************************************************/
 
 
@@ -55,7 +55,7 @@ _gdbm_read_entry (dbf, elem_loc)
   key_size = dbf->bucket->h_table[elem_loc].key_size;
   data_size = dbf->bucket->h_table[elem_loc].data_size;
   data_ca = &dbf->cache_entry->ca_data;
-  
+
   /* Set up the cache. */
   if (data_ca->dptr != NULL) free (data_ca->dptr);
   data_ca->key_size = key_size;
@@ -76,7 +76,7 @@ _gdbm_read_entry (dbf, elem_loc)
     _gdbm_fatal (dbf, "lseek error");
   num_bytes = read (dbf->desc, data_ca->dptr, key_size+data_size);
   if (num_bytes != key_size+data_size) _gdbm_fatal (dbf, "read error");
-  
+
   return data_ca->dptr;
 }
 
@@ -87,6 +87,7 @@ _gdbm_read_entry (dbf, elem_loc)
    entry.  If it is found, a pointer to the data and the key are returned
    in DPTR.  If it is not found, the value -1 is returned.  Since find
    key computes the hash value of key, that value */
+__attribute__((no_sanitize_undefined))
 int
 _gdbm_findkey (dbf, key, dptr, new_hash_val)
      gdbm_file_info *dbf;
@@ -105,7 +106,7 @@ _gdbm_findkey (dbf, key, dptr, new_hash_val)
   _gdbm_get_bucket (dbf, *new_hash_val>> (31-dbf->header->dir_bits));
 
   /* Is the element the last one found for this bucket? */
-  if (dbf->cache_entry->ca_data.elem_loc != -1 
+  if (dbf->cache_entry->ca_data.elem_loc != -1
       && *new_hash_val == dbf->cache_entry->ca_data.hash_val
       && dbf->cache_entry->ca_data.key_size == key.dsize
       && dbf->cache_entry->ca_data.dptr != NULL
@@ -115,7 +116,7 @@ _gdbm_findkey (dbf, key, dptr, new_hash_val)
       *dptr = dbf->cache_entry->ca_data.dptr+key.dsize;
       return dbf->cache_entry->ca_data.elem_loc;
     }
-      
+
   /* It is not the cached value, search for element in the bucket. */
   elem_loc = *new_hash_val % dbf->header->bucket_elems;
   home_loc = elem_loc;
@@ -126,7 +127,7 @@ _gdbm_findkey (dbf, key, dptr, new_hash_val)
       if (bucket_hash_val != *new_hash_val
         || key_size != key.dsize
         || bcmp (dbf->bucket->h_table[elem_loc].key_start, key.dptr,
-                       (SMALL < key_size ? SMALL : key_size)) != 0) 
+                       (SMALL < key_size ? SMALL : key_size)) != 0)
        {
          /* Current elem_loc is not the item, go to next item. */
          elem_loc = (elem_loc + 1) % dbf->header->bucket_elems;
index 0e21da5b2242464ca41f840d18eed580e06fe778..3cd0f114bc3dfccbad55c0121c494aa5fd7bfd86 100644 (file)
@@ -23,7 +23,7 @@
                 Computer Science Department
                 Western Washington University
                 Bellingham, WA 98226
-       
+
 *************************************************************************/
 
 
@@ -46,6 +46,7 @@
    because the argument FLAGS was GDBM_INSERT and the KEY was already in
    the database. */
 
+__attribute__((no_sanitize_undefined))
 int
 gdbm_store (dbf, key, content, flags)
      gdbm_file_info *dbf;
@@ -132,7 +133,7 @@ gdbm_store (dbf, key, content, flags)
          /* Split the current bucket. */
          _gdbm_split_bucket (dbf, new_hash_val);
        }
-      
+
       /* Find space to insert into bucket and set elem_loc to that place. */
       elem_loc = new_hash_val % dbf->header->bucket_elems;
       while (dbf->bucket->h_table[elem_loc].hash_value != -1)
@@ -166,5 +167,5 @@ gdbm_store (dbf, key, content, flags)
   /* Write everything that is needed to the disk. */
   _gdbm_end_update (dbf);
   return 0;
-  
+
 }
diff --git a/hash.c b/hash.c
index 333ec6405d16842a4c9b2f1d66bc2d4baa7493f4..b33e48a78b3336c7c0f7cfa01751aba945d0bb82 100644 (file)
--- a/hash.c
+++ b/hash.c
@@ -23,7 +23,7 @@
                 Computer Science Department
                 Western Washington University
                 Bellingham, WA 98226
-       
+
 *************************************************************************/
 
 
@@ -38,6 +38,7 @@
    to find the home position of the element by taking the value modulo the
    bucket hash table size. */
 
+__attribute__((no_sanitize_undefined))
 int
 _gdbm_hash (key)
      datum key;
@@ -51,7 +52,7 @@ _gdbm_hash (key)
   for (index = 0; index < key.dsize; index++)
     value = (value + (key.dptr[index] << (index*5 % 24))) & 0x7FFFFFFF;
 
-  value = (1103515243 * value + 12345) & 0x7FFFFFFF;  
+  value = (1103515243 * value + 12345) & 0x7FFFFFFF;
 
   /* Return the value. */
   return((int) value);