flatfile: fix handling of zero-length files.

This commit is contained in:
Daniel Beer 2015-07-16 13:14:54 +12:00
parent cb765f639d
commit 235a4fce45
1 changed files with 13 additions and 7 deletions

View File

@ -61,6 +61,10 @@ static int read_flatfile(const char *path, uint8_t **buf, address_t *len)
*len = ftell(in);
rewind(in);
if (!*len) {
*buf = malloc(1);
count = 1;
} else {
*buf = malloc(*len);
if (!*buf) {
printc_err("flatfile: can't allocate memory\n");
@ -69,6 +73,8 @@ static int read_flatfile(const char *path, uint8_t **buf, address_t *len)
}
count = fread(*buf, *len, 1, in);
}
fclose(in);
if (count != 1) {
@ -97,7 +103,7 @@ static int write_flatfile(const char *path, uint8_t *buf, address_t len)
return -1;
}
if (fwrite(buf, len, 1, out) != 1)
if (len && (fwrite(buf, len, 1, out) != 1))
errmsg = last_error();
if (fclose(out) != 0 && !errmsg)