From cc06c2b21f0ac74677d0bddfa4d4d18814de92bc Mon Sep 17 00:00:00 2001 From: Karthikeyan Krishnasamy Date: Tue, 16 Apr 2024 10:47:53 +0530 Subject: [PATCH] lib: posix: fs: fix errno handling ftruncate should return -1 on errors and it needs to set appropriate error values in errno Signed-off-by: Karthikeyan Krishnasamy --- lib/posix/options/fs.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/posix/options/fs.c b/lib/posix/options/fs.c index 8285ef1648b..091a3ddccda 100644 --- a/lib/posix/options/fs.c +++ b/lib/posix/options/fs.c @@ -423,11 +423,18 @@ int mkdir(const char *path, mode_t mode) */ int ftruncate(int fd, off_t length) { + int rc; struct posix_fs_desc *ptr = NULL; ptr = z_get_fd_obj(fd, NULL, EBADF); if (!ptr) return -1; - return fs_truncate(&ptr->file, length); + rc = fs_truncate(&ptr->file, length); + if (rc < 0) { + errno = -rc; + return -1; + } + + return 0; }