flatfile: fix handling of zero-length files.
This commit is contained in:
parent
cb765f639d
commit
235a4fce45
|
@ -61,14 +61,20 @@ static int read_flatfile(const char *path, uint8_t **buf, address_t *len)
|
||||||
*len = ftell(in);
|
*len = ftell(in);
|
||||||
rewind(in);
|
rewind(in);
|
||||||
|
|
||||||
*buf = malloc(*len);
|
if (!*len) {
|
||||||
if (!*buf) {
|
*buf = malloc(1);
|
||||||
printc_err("flatfile: can't allocate memory\n");
|
count = 1;
|
||||||
fclose(in);
|
} else {
|
||||||
return -1;
|
*buf = malloc(*len);
|
||||||
|
if (!*buf) {
|
||||||
|
printc_err("flatfile: can't allocate memory\n");
|
||||||
|
fclose(in);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
count = fread(*buf, *len, 1, in);
|
||||||
}
|
}
|
||||||
|
|
||||||
count = fread(*buf, *len, 1, in);
|
|
||||||
fclose(in);
|
fclose(in);
|
||||||
|
|
||||||
if (count != 1) {
|
if (count != 1) {
|
||||||
|
@ -97,7 +103,7 @@ static int write_flatfile(const char *path, uint8_t *buf, address_t len)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fwrite(buf, len, 1, out) != 1)
|
if (len && (fwrite(buf, len, 1, out) != 1))
|
||||||
errmsg = last_error();
|
errmsg = last_error();
|
||||||
|
|
||||||
if (fclose(out) != 0 && !errmsg)
|
if (fclose(out) != 0 && !errmsg)
|
||||||
|
|
Loading…
Reference in New Issue