Yap, it is real. And it hasn't even discovered the entire problem.
When one dynamically allocates memory using the malloc()
family of functions, which calloc()
is the member of, for one element, the code often goes like so:
T *ptr = malloc(sizeof(T));
That's fine as long as you don't change the type of the pointer. When you do, you will forget to change the type in the sizeof()
, and possibly you end up allocating less memory than your T
type takes up, effectively making your program invoke undefined behavior.
And you just run into this issue. (Alhough, technically, here you allocate more memory than needed, no UB, but conceptually it's still wrong.)
char *out = calloc(lineLength, sizeof(data));
should really be
char *out = calloc(lineLength + 1, sizeof(*data));
instead. You want to allocate space for lineLength + 1
pieces of char
, whereas data
is a pointer-to-char.
Oh, and do not cast the return value of mmap()
!