From: Jean-Philippe Orsini Date: Sun, 5 Feb 2017 17:49:51 +0000 (+0100) Subject: fixed case of pre-allocated files (reported size of the file might be greater than... X-Git-Url: http://git.wpitchoune.net/gitweb/?p=psensor.git;a=commitdiff_plain;h=790c76c1fb187bdc358e0a34e4edcf428d6c8970 fixed case of pre-allocated files (reported size of the file might be greater than the bytes than can be reader) --- diff --git a/src/lib/pio.c b/src/lib/pio.c index 7bb3d7c..1942348 100644 --- a/src/lib/pio.c +++ b/src/lib/pio.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2016 jeanfi@gmail.com + * Copyright (C) 2010-2017 jeanfi@gmail.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -150,11 +150,13 @@ void paths_free(char **paths) char *file_get_content(const char *fpath) { - long size; - + long size, n; char *page; + log_fct_enter(); + size = file_get_size(fpath); + if (size == -1) { page = NULL; @@ -167,19 +169,27 @@ char *file_get_content(const char *fpath) if (fp) { page = malloc(size + 1); - if (!page || size != fread(page, 1, size, fp)) { - free(page); - page = NULL; - } else { - *(page + size) = '\0'; + + if (page) { + clearerr(fp); + n = fread(page, 1, size, fp); + if (n != size && ferror(fp)) { + free(page); + page = NULL; + } else { + *(page + n) = '\0'; + } } fclose(fp); } else { + log_debug("failed to open %s", fpath); page = NULL; } } + log_fct_exit(); + return page; } diff --git a/src/lib/pio.h b/src/lib/pio.h index b7b0652..018706b 100644 --- a/src/lib/pio.h +++ b/src/lib/pio.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2016 jeanfi@gmail.com + * Copyright (C) 2010-2017 jeanfi@gmail.com * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,7 +20,7 @@ #ifndef _P_IO_H #define _P_IO_H -#define P_IO_VER 6 +#define P_IO_VER 7 #include