charlietangora / gif-h

Simple C++ one-header library for the creation of animated GIFs from image data.
The Unlicense
497 stars 98 forks source link

Undefined behaviour: Syscall param write(buf) points to uninitialised byte(s) #21

Open mymedia2 opened 2 years ago

mymedia2 commented 2 years ago

The following simple program creates a GIF composed of two completely white frames. But Valgrind's Memcheck complains about an uninitialized memory in GifBegin().

#include "gif.h"

static const int width = 200;
static const int height = 200;

static uint8_t image[width * height * 4];

int main(int argc, char* argv[])
{
  const char* filename = "mytest.gif";
  if (argc > 1) {
    filename = argv[1];
  }

  GifWriter writer;
  GifBegin(&writer, filename, width, height, 100);

  for (int i = 0; i < sizeof image; i += 4) {
    image[i] = image[i + 1] = image[i + 2] = 255;
    image[i + 3] = 0;  // not necessary
  }
  GifWriteFrame(&writer, image, width, height, 100);
  GifWriteFrame(&writer, image, width, height, 100);

  GifEnd(&writer);
  return 0;
}
mymedia@barberry:~/src/gif-h$ valgrind ./a.out 
==98249== Memcheck, a memory error detector
==98249== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==98249== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==98249== Command: ./a.out
==98249== 
==98249== Syscall param write(buf) points to uninitialised byte(s)
==98249==    at 0x49809B7: write (write.c:26)
==98249==    by 0x48F7E6C: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1181)
==98249==    by 0x48F9970: new_do_write (fileops.c:449)
==98249==    by 0x48F9970: _IO_new_do_write (fileops.c:426)
==98249==    by 0x48F9970: _IO_do_write@@GLIBC_2.2.5 (fileops.c:423)
==98249==    by 0x48F8F67: _IO_file_close_it@@GLIBC_2.2.5 (fileops.c:136)
==98249==    by 0x48EBE0E: fclose@@GLIBC_2.2.5 (iofclose.c:53)
==98249==    by 0x10B744: GifEnd(GifWriter*) (gif.h:827)
==98249==    by 0x10B8D1: main (mytest.c:25)
==98249==  Address 0x4abb837 is 1,175 bytes inside a block of size 4,096 alloc'd
==98249==    at 0x4843839: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==98249==    by 0x48EBC23: _IO_file_doallocate (filedoalloc.c:101)
==98249==    by 0x48FAC6F: _IO_doallocbuf (genops.c:347)
==98249==    by 0x48F9EFF: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:745)
==98249==    by 0x48F8694: _IO_new_file_xsputn (fileops.c:1244)
==98249==    by 0x48F8694: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1197)
==98249==    by 0x48ED066: fwrite (iofwrite.c:39)
==98249==    by 0x10B342: GifBegin(GifWriter*, char const*, unsigned int, unsigned int, unsigned int, int, bool) (gif.h:754)
==98249==    by 0x10B7DD: main (mytest.c:16)
==98249== 
==98249== 
==98249== HEAP SUMMARY:
==98249==     in use at exit: 0 bytes in 0 blocks
==98249==   total heap usage: 7 allocs, 7 frees, 4,678,872 bytes allocated
==98249== 
==98249== All heap blocks were freed -- no leaks are possible
==98249== 
==98249== Use --track-origins=yes to see where uninitialised values come from
==98249== For lists of detected and suppressed errors, rerun with: -s
==98249== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)