nothings / stb

stb single-file public domain libraries for C/C++

Home Page:https://twitter.com/nothings

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

stb_truetype valgrind: invalid read of size 1

scippie75 opened this issue · comments

Describe the bug
When running valgrind over my C application that uses stb_truetype for drawing glyphs on a buffer, I get 'invalid read of size 1' on almost all function calls I do.
Lots of them point to the same ttSHORT call, like this one:

==3427652== Invalid read of size 1
==3427652==    at 0x10C453: ttSHORT (stb_truetype.h:1287)
==3427652==    by 0x111A61: stbtt_ScaleForPixelHeight (stb_truetype.h:2662)

But it is certainly not limited to ttSHORT and stbtt_ScaleForPixelHeight.

Reading 1 byte too many will almost never generate a crash but it can't be good, and it is also very annoying when trying to find my own memory leaks in between.

To Reproduce
This simple test already shows several of those invalid reads in valgrind:

#define STB_TRUETYPE_IMPLEMENTATION
#include "stb_truetype.h"
#include <stdio.h>

int main(int argc, char **argv)
{
  FILE *f = fopen("OpenSans-Bold.ttf", "rb"); /* No error checking, make sure file exists */
  fseek(f, 0, SEEK_END);
  long fs = ftell(f);
  fseek(f, 0, SEEK_SET);
  char *ttf_data = malloc(fs);
  fread(ttf_data, fs, 1, f);
  fclose(f);

  stbtt_fontinfo font;
  stbtt_InitFont(&font, ttf_data, 0);
  free(ttf_data);

  float scale = stbtt_ScaleForPixelHeight(&font, 16);

  return 0;
}

I have tried with a different .ttf file with the same result, so I guess it doesn't have to do anything with the .ttf file.

No special valgrind arguments are needed.

Please remove. Problem was freeing the ttf_data. This should not be done before end of use.