Stupid proof fread

This commit is contained in:
bzt 2022-11-21 06:28:56 +01:00
parent be253ad7a7
commit 2b3fb32025
4 changed files with 11 additions and 5 deletions

View File

@ -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], '\\');

View File

@ -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); }

View File

@ -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);

View File

@ -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);
}