Skip to content

Commit

Permalink
Merge pull request sysprog21#303 from visitorckw/fix-syscall-error-ha…
Browse files Browse the repository at this point in the history
…ndling

Fix syscall error handling
  • Loading branch information
jserv authored Dec 22, 2023
2 parents 81912c1 + 331e7c4 commit c32861a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ static void syscall_write(riscv_t *rv)
memory_read(s->mem, tmp, buffer + total_write, PREALLOC_SIZE);
if (!map_at_end(s->fd_map, &it)) {
/* write out the data */
size_t written =
fwrite(tmp, 1, PREALLOC_SIZE, map_iter_value(&it, FILE *));
FILE *handle = map_iter_value(&it, FILE *);
size_t written = fwrite(tmp, 1, PREALLOC_SIZE, handle);
if (written != PREALLOC_SIZE && ferror(handle))
goto error_handler;
total_write += written;
count -= PREALLOC_SIZE;
} else
Expand All @@ -106,7 +108,10 @@ static void syscall_write(riscv_t *rv)
memory_read(s->mem, tmp, buffer + total_write, count);
if (!map_at_end(s->fd_map, &it)) {
/* write out the data */
size_t written = fwrite(tmp, 1, count, map_iter_value(&it, FILE *));
FILE *handle = map_iter_value(&it, FILE *);
size_t written = fwrite(tmp, 1, count, handle);
if (written != count && ferror(handle))
goto error_handler;
total_write += written;
} else
goto error_handler;
Expand Down Expand Up @@ -211,7 +216,11 @@ static void syscall_close(riscv_t *rv)
map_iter_t it;
map_find(s->fd_map, &it, &fd);
if (!map_at_end(s->fd_map, &it)) {
fclose(map_iter_value(&it, FILE *));
if (fclose(map_iter_value(&it, FILE *))) {
/* error */
rv_set_reg(rv, rv_reg_a0, -1);
return;
}
map_erase(s->fd_map, &it);

/* success */
Expand Down

0 comments on commit c32861a

Please sign in to comment.