From b665e3629d69991a182e4d0fd0315567b01c724d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Sat, 24 Jun 2023 21:13:00 +0200 Subject: [PATCH] fix: handle filesystem read and write errors --- lib/filesystem.c | 8 ++++++-- lib/filesystem.h | 8 ++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/filesystem.c b/lib/filesystem.c index 6da52df..a246836 100644 --- a/lib/filesystem.c +++ b/lib/filesystem.c @@ -8,7 +8,9 @@ int filesystem_read(char *path, char **file_content, off_t *file_size) { (*file_size) = lseek(file_descriptor, 0, SEEK_END); lseek(file_descriptor, 0, SEEK_SET); (*file_content) = malloc(*file_size); - read(file_descriptor, *file_content, *file_size); + if (read(file_descriptor, *file_content, *file_size) == -1) { + return -1; + } close(file_descriptor); return 0; } @@ -18,7 +20,9 @@ int filesystem_write(char *path, char *file_content, off_t file_size) { if (file_descriptor == -1) { return -1; } - write(file_descriptor, file_content, file_size); + if (write(file_descriptor, file_content, file_size) == -1) { + return -1; + } close(file_descriptor); return 0; } diff --git a/lib/filesystem.h b/lib/filesystem.h index cab4e48..2f697b9 100644 --- a/lib/filesystem.h +++ b/lib/filesystem.h @@ -19,8 +19,8 @@ * @param file_content * @param file_size * @return int - * @retval -1 if the file does not exist - * @retval 0 for success + * @retval -1 if the file does not exist or if there is an error. + * @retval 0 for success. * @since v1.0.0 */ int filesystem_read(char *path, char **file_content, off_t *file_size); @@ -32,8 +32,8 @@ int filesystem_read(char *path, char **file_content, off_t *file_size); * @param file_content * @param file_size * @return int - * @retval -1 if errors - * @retval 0 for success + * @retval -1 if there is an error. + * @retval 0 for success. * @since v1.0.0 */ int filesystem_write(char *path, char *file_content, off_t file_size);