Index: aocutils.c ================================================================== --- aocutils.c +++ aocutils.c @@ -42,11 +42,11 @@ int ch; char *tmp = malloc(512); size_t s = 512; size_t r = 0; while ((ch = fgetc(h)) != EOF) { - if (r == s) { + if (r+1 == s) { // grow the array (by golden ratio) char *ttmp = realloc(tmp, (13*s) / 8); if (ttmp) { tmp = ttmp; s = (13*s) / 8; @@ -56,10 +56,11 @@ } } tmp[r++] = ch; } fclose(h); + tmp[r] = 0; *dst = tmp; return r; }