From 2b3fb320251e1604aedd8751b8b4ce5679aea420 Mon Sep 17 00:00:00 2001 From: bzt Date: Mon, 21 Nov 2022 06:28:56 +0100 Subject: [PATCH] Stupid proof fread --- mkbootimg/bin2h.c | 2 +- mkbootimg/main.c | 2 +- mkbootimg/main.h | 2 +- mkbootimg/util.c | 10 ++++++++-- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/mkbootimg/bin2h.c b/mkbootimg/bin2h.c index 70bb826..40a2344 100644 --- a/mkbootimg/bin2h.c +++ b/mkbootimg/bin2h.c @@ -63,7 +63,7 @@ int main(int argc, char **argv) fprintf(stderr, "bin2h: memory allocation error\r\n"); exit(2); } - fread(buff, size, 1, f); + fread(buff, 1, size, f); fclose(f); fn = strrchr(argv[file], '/'); if(!fn) fn = strrchr(argv[file], '\\'); diff --git a/mkbootimg/main.c b/mkbootimg/main.c index fc00f1b..8e3ec13 100644 --- a/mkbootimg/main.c +++ b/mkbootimg/main.c @@ -424,7 +424,7 @@ int flashmapadd(char *file) fseek(f,0L,SEEK_SET); data=(unsigned char*)malloc(size + bs); if(!data) { fprintf(stderr,"mkbootimg: %s\r\n",lang[ERR_MEM]); exit(1); } - data[0] = 0; fread(data,size,1,f); + data[0] = 0; fread(data,1,size,f); fclose(f); if(memcmp(data, "__FMAP__", 8)) { free(data); return 0; } if(!initrd_buf[0] || bs < 1) { fprintf(stderr,"mkbootimg: %s\r\n",lang[ERR_NOINITRD]); exit(1); } diff --git a/mkbootimg/main.h b/mkbootimg/main.h index f2d01da..7e2f228 100644 --- a/mkbootimg/main.h +++ b/mkbootimg/main.h @@ -194,7 +194,7 @@ extern initrd_open rd_open; extern initrd_add rd_add; extern initrd_close rd_close; -extern long int read_size; +extern int64_t read_size; unsigned char* readfileall(char *file); unsigned int gethex(char *ptr, int len); void getguid(char *ptr, guid_t *guid); diff --git a/mkbootimg/util.c b/mkbootimg/util.c index a9c0448..b328076 100644 --- a/mkbootimg/util.c +++ b/mkbootimg/util.c @@ -56,11 +56,13 @@ initrd_close rd_close = NULL; /** * Read a file entirely into memory. Don't use it with partition image files */ -long int read_size; +int64_t read_size; unsigned char* readfileall(char *file) { unsigned char *data=NULL; FILE *f; + int64_t r, t = 0; + read_size=0; if(!file || !*file) return NULL; f=fopen(file,"r"); @@ -71,7 +73,11 @@ unsigned char* readfileall(char *file) data=(unsigned char*)malloc(read_size+1); if(!data) { fprintf(stderr,"mkbootimg: %s\r\n",lang[ERR_MEM]); exit(1); } memset(data,0,read_size+1); - fread(data,1,read_size,f); + do { + r = fread(data + t,1,read_size - t,f); + t += r; + } while(r > 0 && t < read_size); + if(t != read_size) { fprintf(stderr,"mkbootimg: '%s' fread %ld != %ld ???\r\n",file,(long int)t,(long int)read_size); exit(1); } data[read_size] = 0; fclose(f); }