From 477db22cdbacd9caaa654fb91d73f6b00558c9d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Leczkowski?= Date: Mon, 29 Jul 2024 10:57:34 +0200 Subject: [PATCH] unistd/file: fix __safe_read/write return value JIRA: RTOS-882 --- unistd/file-internal.h | 2 +- unistd/file.c | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/unistd/file-internal.h b/unistd/file-internal.h index 6698c1de..bdfc6463 100644 --- a/unistd/file-internal.h +++ b/unistd/file-internal.h @@ -20,7 +20,7 @@ #include -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); diff --git a/unistd/file.c b/unistd/file.c index 44a71ccd..837a558a 100644 --- a/unistd/file.c +++ b/unistd/file.c @@ -48,10 +48,10 @@ 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) { @@ -59,7 +59,6 @@ size_t __safe_write(int fd, const void *buff, size_t size) if (wlen > 0) { todo -= wlen; ptr += wlen; - continue; } else if (wlen < 0) { if ((errno == EINTR) || (errno == EAGAIN)) { @@ -74,6 +73,10 @@ size_t __safe_write(int fd, const void *buff, size_t size) } } + if (wlen < 0) { + return -1; + } + return size - todo; } @@ -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; }