Skip to content

Commit

Permalink
unistd/file: fix __safe_read/write return value
Browse files Browse the repository at this point in the history
JIRA: RTOS-882
  • Loading branch information
lukileczo committed Jul 29, 2024
1 parent faa8b35 commit 477db22
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion unistd/file-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <sys/types.h>


size_t __safe_write(int fd, const void *buff, size_t size);
ssize_t __safe_write(int fd, const void *buff, size_t size);


ssize_t __safe_read(int fd, void *buf, size_t size);
Expand Down
13 changes: 10 additions & 3 deletions unistd/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,17 @@ WRAP_ERRNO_DEF(int, dup2, (int fildes, int fildes2), (fildes, fildes2))
WRAP_ERRNO_DEF(int, fsync, (int fildes), (fildes))


size_t __safe_write(int fd, const void *buff, size_t size)
ssize_t __safe_write(int fd, const void *buff, size_t size)
{
size_t todo = size;
ssize_t wlen;
ssize_t wlen = 0;
const char *ptr = buff;

while (todo != 0) {
wlen = write(fd, ptr, todo);
if (wlen > 0) {
todo -= wlen;
ptr += wlen;
continue;
}
else if (wlen < 0) {
if ((errno == EINTR) || (errno == EAGAIN)) {
Expand All @@ -74,6 +73,10 @@ size_t __safe_write(int fd, const void *buff, size_t size)
}
}

if (wlen < 0) {
return -1;
}

return size - todo;
}

Expand Down Expand Up @@ -102,6 +105,10 @@ ssize_t __safe_read(int fd, void *buf, size_t size)
}
}

if ((size != 0) && (size == todo)) {
return -1;
}

return size - todo;
}

Expand Down

0 comments on commit 477db22

Please sign in to comment.