util/blob: use memcpy in read functions
authorPierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Mon, 23 May 2022 08:33:25 +0000 (10:33 +0200)
committerMarge Bot <emma+marge@anholt.net>
Thu, 7 Jul 2022 12:25:05 +0000 (12:25 +0000)
Type casting may require specific alignment on some platforms;
since the input data can be provided by the application we can't
require any alignment.

Switch to using memcpy like the write functions do, and drop the
asserts.

Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/6493
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16662>

src/util/blob.c

index 67f6bb2..b293df9 100644 (file)
@@ -85,7 +85,6 @@ grow_to_fit(struct blob *blob, size_t additional)
 bool
 blob_align(struct blob *blob, size_t alignment)
 {
-   assert(align64((uintptr_t)blob->data, alignment) == (uintptr_t)blob->data);
    const size_t new_size = align64(blob->size, alignment);
 
    if (blob->size < new_size) {
@@ -103,7 +102,6 @@ blob_align(struct blob *blob, size_t alignment)
 void
 blob_reader_align(struct blob_reader *blob, size_t alignment)
 {
-   assert(align64((uintptr_t)blob->data, alignment) == (uintptr_t)blob->data);
    blob->current = blob->data + align64(blob->current - blob->data, alignment);
 }
 
@@ -310,22 +308,14 @@ blob_skip_bytes(struct blob_reader *blob, size_t size)
       blob->current += size;
 }
 
-/* These next three read functions have identical form. If we add any beyond
- * these first three we should probably switch to generating these with a
- * preprocessor macro.
-*/
-
 #define BLOB_READ_TYPE(name, type)         \
 type                                       \
 name(struct blob_reader *blob)             \
 {                                          \
-   type ret;                               \
+   type ret = 0;                           \
    int size = sizeof(ret);                 \
    blob_reader_align(blob, size);          \
-   if (! ensure_can_read(blob, size))      \
-      return 0;                            \
-   ret = *((type*) blob->current);         \
-   blob->current += size;                  \
+   blob_copy_bytes(blob, &ret, size);      \
    return ret;                             \
 }