Skip to content

Valgrind reports uninitialized bytes originating in src/cache.h #354

@dancingmirrors

Description

@dancingmirrors

==1275795== Syscall param write(buf) points to uninitialised byte(s)
==1275795== at 0x89549EE: __syscall_cancel_arch (syscall_cancel.S:56)
==1275795== by 0x8949667: __internal_syscall_cancel (cancellation.c:49)
==1275795== by 0x89496AC: __syscall_cancel (cancellation.c:75)
==1275795== by 0x89BE935: write (write.c:26)
==1275795== by 0x89455F4: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1182)
==1275795== by 0x89438D1: new_do_write (fileops.c:450)
==1275795== by 0x89457F8: _IO_new_file_xsputn (fileops.c:1256)
==1275795== by 0x89457F8: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1198)
==1275795== by 0x893937B: fwrite (iofwrite.c:39)
==1275795== by 0xB066F9: cache_save_obj (video/out/vo_default.c:1649)
==1275795== by 0x83C4A9D: pl_cache_try_set (cache.c:189)
==1275795== by 0x83C4A9D: pl_cache_try_set (cache.c:173)
==1275795== by 0x83C4AF8: pl_cache_set (cache.c:195)
==1275795== by 0x83AF6B0: sh_lut (lut.c:600)
==1275795== Address 0x3e01a066 is 38 bytes inside a block of size 2,097,184 alloc'd

This is how I fixed it but I dunno if it's correct:

--- a/src/cache.h
+++ b/src/cache.h
@@ -48,11 +48,14 @@ static inline void pl_cache_obj_resize(void *alloc, pl_cache_obj *obj, size_t si
     if (obj->free != pl_free) {
         if (obj->free)
             obj->free(obj->data);
-        obj->data = pl_alloc(alloc, size);
+        obj->data = pl_zalloc(alloc, size);
         obj->free = pl_free;
     } else if (pl_get_size(obj->data) < size) {
+        size_t old_size = pl_get_size(obj->data);
         obj->data = pl_steal(alloc, obj->data);
         obj->data = pl_realloc(alloc, obj->data, size);
+        // Zero-initialize the newly allocated portion
+        memset((char *)obj->data + old_size, 0, size - old_size);
     }
     obj->size = size;
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions