From 1a1189f36377d08e95d898b978aba4bcf7494848 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Mon, 28 Feb 2011 22:01:36 +0200 Subject: [PATCH] fileutil: load_file() fixes - report actually read bytes. Eg: different on win32 due to dos line endings. Reported-by: Rich Schaaf - free buffer on error --- usual/fileutil.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/usual/fileutil.c b/usual/fileutil.c index 23e6ba2..4747e42 100644 --- a/usual/fileutil.c +++ b/usual/fileutil.c @@ -46,19 +46,22 @@ void *load_file(const char *fn, size_t *len_p) return NULL; f = fopen(fn, "r"); - if (!f) + if (!f) { + free(buf); return NULL; + } if ((res = fread(buf, 1, st.st_size, f)) < 0) { + free(buf); fclose(f); return NULL; } fclose(f); - buf[st.st_size] = 0; + buf[res] = 0; if (len_p) - *len_p = st.st_size; + *len_p = res; return buf; } -- 2.39.5