From 5a189f6f1de2b7e3370a0291d3e695a1f4b771fd Mon Sep 17 00:00:00 2001 From: Mikhail Romanko Date: Sat, 8 Aug 2026 16:47:24 +0300 Subject: [PATCH] Add asserts --- File.c | 11 +++++++++- posix/File.c | 59 +++++++++++++++++++++++++++++++++++++++++++++------- win32/File.c | 54 ++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 110 insertions(+), 14 deletions(-) diff --git a/File.c b/File.c index 901cb27..3aa8b52 100644 --- a/File.c +++ b/File.c @@ -1,8 +1,11 @@ #include "CgeFile.h" +#include int CgeFileError(CgeFile *file) { int flags; + assert(file != NULL); + CgeFileFlags(file, &flags); return flags & CGE_FILE_ERROR; } @@ -10,6 +13,8 @@ int CgeFileError(CgeFile *file) { int CgeFileEndOfFile(CgeFile *file) { int flags; + assert(file != NULL); + CgeFileFlags(file, &flags); return flags & CGE_FILE_EOF; } @@ -18,6 +23,9 @@ int CgeFilePeek(CgeFile *file, void *buffer, size_t size, size_t *actual) { int64_t pos; size_t n = 0; + assert(file != NULL); + assert(buffer != NULL); + if (CgeFileError(file) || CgeFileEndOfFile(file)) return 0; @@ -26,6 +34,7 @@ int CgeFilePeek(CgeFile *file, void *buffer, size_t size, size_t *actual) { !CgeFileSeek(file, pos, CGE_FILE_SET)) return 0; - if (actual) *actual = n; + if (actual) + *actual = n; return 1; } diff --git a/posix/File.c b/posix/File.c index 21e79b5..c71d007 100644 --- a/posix/File.c +++ b/posix/File.c @@ -8,6 +8,7 @@ #include #include #include +#include static const int64_t off_t_max = (sizeof(off_t) == 8) ? INT64_MAX : @@ -77,8 +78,7 @@ static int fileOpenFlags(int mode) { else return -1; - if (!(mode & CGE_FILE_EXIST)) - { + if (!(mode & CGE_FILE_EXIST)) { flags |= O_CREAT; if (mode & CGE_FILE_CREATE) flags |= O_EXCL; @@ -97,6 +97,9 @@ static int fileInit(CgeFile *file, const char *path, int mode) { static const mode_t openMode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); int flags; + assert(file != NULL); + assert(path != NULL); + if (!path) return CGE_FILE_EINVAL; @@ -116,17 +119,23 @@ CgeFile *CgeFileNew(const char *path, int mode, int *result) { CgeFile *file; int code = 0; + assert(path != NULL); + if ((file = malloc(sizeof(*file)))) { if ((code = fileInit(file, path, mode))) { free(file); file = NULL; } } - if (result) *result = code; + if (result) + *result = code; return file; } void CgeFileFree(CgeFile *file) { + if (!file) + return; + close(file->handle); free(file); } @@ -134,20 +143,27 @@ void CgeFileFree(CgeFile *file) { int CgeFileRead(CgeFile *file, void *buffer, size_t size, size_t *actual) { ssize_t readed; + assert(file != NULL); + assert(file->handle != -1); + assert(buffer != NULL); + if (file->flags & CGE_FILE_ERROR) return 0; do { - readed = read(file->handle, buffer, size); + readed = read(file->handle, buffer, size); } while (readed < 0 && errno == EINTR); if (readed < 0) goto error; - if (readed > 0) file->flags &= ~CGE_FILE_EOF; - else file->flags |= CGE_FILE_EOF; + if (readed > 0) + file->flags &= ~CGE_FILE_EOF; + else + file->flags |= CGE_FILE_EOF; - if (actual) *actual = readed; + if (actual) + *actual = readed; return 1; error: @@ -160,6 +176,10 @@ int CgeFileWrite(CgeFile *file, const void *buffer, size_t size, size_t *actual) { ssize_t written; + assert(file != NULL); + assert(file->handle != -1); + assert(buffer != NULL); + if (file->flags & CGE_FILE_ERROR) return 0; @@ -170,7 +190,8 @@ int CgeFileWrite(CgeFile *file, const void *buffer, size_t size, if (written < 0) goto error; - if (actual) *actual = written; + if (actual) + *actual = written; return 1; error: @@ -180,6 +201,10 @@ error: } int CgeFileTell(CgeFile *file, int64_t *offset) { + assert(file != NULL); + assert(file->handle != -1); + assert(offset != NULL); + if (file->flags & CGE_FILE_ERROR) return 0; @@ -195,6 +220,10 @@ error: } int CgeFileSeek(CgeFile *file, int64_t offset, int whence) { + assert(file != NULL); + assert(file->handle != -1); + assert(whence == CGE_FILE_SET || whence == CGE_FILE_CUR || whence == CGE_FILE_END); + if (file->flags & CGE_FILE_ERROR) return 0; @@ -216,6 +245,9 @@ error: } int CgeFileFlush(CgeFile *file) { + assert(file != NULL); + assert(file->handle != -1); + if (fsync(file->handle)) goto error; @@ -230,6 +262,10 @@ error: int CgeFileSize(CgeFile *file, int64_t *size) { struct stat sb; + assert(file != NULL); + assert(file->handle != -1); + assert(size != NULL); + if (fstat(file->handle, &sb)) goto error; @@ -243,15 +279,22 @@ error: } int CgeFileFlags(CgeFile *file, int *flags) { + assert(file != NULL); + assert(flags != NULL); + *flags = file->flags; return 1; } int CgeFileClear(CgeFile *file) { + assert(file != NULL); + file->flags &= ~CGE_FILE_ERROR; return 1; } int CgeFileErrorCode(CgeFile *file) { + assert(file != NULL); + return file->lastError; } diff --git a/win32/File.c b/win32/File.c index f9d325b..70baf2d 100644 --- a/win32/File.c +++ b/win32/File.c @@ -1,5 +1,6 @@ #include "../CgeFile.h" #include +#include struct CgeFile { HANDLE handle; @@ -52,6 +53,9 @@ static int errorCodeFromError(void) { static int fileInit(CgeFile *file, const char *path, int mode) { DWORD access = 0, how = 0; + assert(file != NULL); + assert(path != NULL); + if (!path) return CGE_FILE_EINVAL; @@ -96,17 +100,24 @@ CgeFile *CgeFileNew(const char *path, int mode, int *result) { CgeFile *file; int code = 0; + assert(path != NULL); + if ((file = malloc(sizeof(*file)))) { if ((code = fileInit(file, path, mode))) { free(file); file = NULL; } } - if (result) *result = code; + if (result) + *result = code; + return file; } void CgeFileFree(CgeFile *file) { + if (!file) + return; + CloseHandle(file->handle); free(file); } @@ -114,15 +125,22 @@ void CgeFileFree(CgeFile *file) { int CgeFileRead(CgeFile *file, void *buffer, size_t size, size_t *actual) { DWORD readed; + assert(file != NULL); + assert(file->handle != INVALID_HANDLE_VALUE); + assert(buffer != NULL); + if (file->flags & CGE_FILE_ERROR) return 0; if (!ReadFile(file->handle, buffer, (DWORD)size, &readed, NULL)) goto error; - if (!readed) file->flags |= CGE_FILE_EOF; - else file->flags &= ~CGE_FILE_EOF; - if (actual) *actual = readed; + if (!readed) + file->flags |= CGE_FILE_EOF; + else + file->flags &= ~CGE_FILE_EOF; + if (actual) + *actual = readed; return 1; @@ -137,6 +155,10 @@ int CgeFileWrite(CgeFile *file, const void *buffer, size_t size, DWORD written; LARGE_INTEGER position; + assert(file != NULL); + assert(file->handle != INVALID_HANDLE_VALUE); + assert(buffer != NULL); + if (file->flags & CGE_FILE_ERROR) return 0; @@ -149,7 +171,8 @@ int CgeFileWrite(CgeFile *file, const void *buffer, size_t size, if (!WriteFile(file->handle, buffer, (DWORD)size, &written, NULL)) goto error; - if (actual) *actual = written; + if (actual) + *actual = written; return 1; @@ -162,6 +185,10 @@ error: int CgeFileTell(CgeFile *file, int64_t *offset) { LARGE_INTEGER dummy, position; + assert(file != NULL); + assert(file->handle != INVALID_HANDLE_VALUE); + assert(offset != NULL); + if (file->flags & CGE_FILE_ERROR) return 0; @@ -181,6 +208,10 @@ error: int CgeFileSeek(CgeFile *file, int64_t offset, int whence) { LARGE_INTEGER position; + assert(file != NULL); + assert(file->handle != INVALID_HANDLE_VALUE); + assert(whence == CGE_FILE_SET || whence == CGE_FILE_CUR || whence == CGE_FILE_END); + if (file->flags & CGE_FILE_ERROR) return 0; @@ -198,6 +229,9 @@ error: } int CgeFileFlush(CgeFile *file) { + assert(file != NULL); + assert(file->handle != INVALID_HANDLE_VALUE); + if (!FlushFileBuffers(file->handle)) goto error; @@ -212,6 +246,9 @@ error: int CgeFileSize(CgeFile *file, int64_t *size) { LARGE_INTEGER dummy; + assert(file != NULL); + assert(file->handle != INVALID_HANDLE_VALUE); + if (file->flags & CGE_FILE_ERROR) return 0; @@ -228,15 +265,22 @@ error: } int CgeFileFlags(CgeFile *file, int *flags) { + assert(file != NULL); + assert(flags != NULL); + *flags = file->flags; return 1; } int CgeFileClear(CgeFile *file) { + assert(file != NULL); + file->flags &= ~CGE_FILE_ERROR; return 1; } int CgeFileErrorCode(CgeFile *file) { + assert(file != NULL); + return file->lastError; }