Add asserts
All checks were successful
CI / build-and-analyze (push) Successful in 1m9s

This commit is contained in:
2026-08-08 16:47:24 +03:00
parent ce2f70896e
commit 5a189f6f1d
3 changed files with 110 additions and 14 deletions

11
File.c
View File

@@ -1,8 +1,11 @@
#include "CgeFile.h" #include "CgeFile.h"
#include <assert.h>
int CgeFileError(CgeFile *file) { int CgeFileError(CgeFile *file) {
int flags; int flags;
assert(file != NULL);
CgeFileFlags(file, &flags); CgeFileFlags(file, &flags);
return flags & CGE_FILE_ERROR; return flags & CGE_FILE_ERROR;
} }
@@ -10,6 +13,8 @@ int CgeFileError(CgeFile *file) {
int CgeFileEndOfFile(CgeFile *file) { int CgeFileEndOfFile(CgeFile *file) {
int flags; int flags;
assert(file != NULL);
CgeFileFlags(file, &flags); CgeFileFlags(file, &flags);
return flags & CGE_FILE_EOF; return flags & CGE_FILE_EOF;
} }
@@ -18,6 +23,9 @@ int CgeFilePeek(CgeFile *file, void *buffer, size_t size, size_t *actual) {
int64_t pos; int64_t pos;
size_t n = 0; size_t n = 0;
assert(file != NULL);
assert(buffer != NULL);
if (CgeFileError(file) || CgeFileEndOfFile(file)) if (CgeFileError(file) || CgeFileEndOfFile(file))
return 0; return 0;
@@ -26,6 +34,7 @@ int CgeFilePeek(CgeFile *file, void *buffer, size_t size, size_t *actual) {
!CgeFileSeek(file, pos, CGE_FILE_SET)) !CgeFileSeek(file, pos, CGE_FILE_SET))
return 0; return 0;
if (actual) *actual = n; if (actual)
*actual = n;
return 1; return 1;
} }

View File

@@ -8,6 +8,7 @@
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <assert.h>
static const int64_t off_t_max = static const int64_t off_t_max =
(sizeof(off_t) == 8) ? INT64_MAX : (sizeof(off_t) == 8) ? INT64_MAX :
@@ -77,8 +78,7 @@ static int fileOpenFlags(int mode) {
else else
return -1; return -1;
if (!(mode & CGE_FILE_EXIST)) if (!(mode & CGE_FILE_EXIST)) {
{
flags |= O_CREAT; flags |= O_CREAT;
if (mode & CGE_FILE_CREATE) if (mode & CGE_FILE_CREATE)
flags |= O_EXCL; 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); static const mode_t openMode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
int flags; int flags;
assert(file != NULL);
assert(path != NULL);
if (!path) if (!path)
return CGE_FILE_EINVAL; return CGE_FILE_EINVAL;
@@ -116,17 +119,23 @@ CgeFile *CgeFileNew(const char *path, int mode, int *result) {
CgeFile *file; CgeFile *file;
int code = 0; int code = 0;
assert(path != NULL);
if ((file = malloc(sizeof(*file)))) { if ((file = malloc(sizeof(*file)))) {
if ((code = fileInit(file, path, mode))) { if ((code = fileInit(file, path, mode))) {
free(file); free(file);
file = NULL; file = NULL;
} }
} }
if (result) *result = code; if (result)
*result = code;
return file; return file;
} }
void CgeFileFree(CgeFile *file) { void CgeFileFree(CgeFile *file) {
if (!file)
return;
close(file->handle); close(file->handle);
free(file); free(file);
} }
@@ -134,6 +143,10 @@ void CgeFileFree(CgeFile *file) {
int CgeFileRead(CgeFile *file, void *buffer, size_t size, size_t *actual) { int CgeFileRead(CgeFile *file, void *buffer, size_t size, size_t *actual) {
ssize_t readed; ssize_t readed;
assert(file != NULL);
assert(file->handle != -1);
assert(buffer != NULL);
if (file->flags & CGE_FILE_ERROR) if (file->flags & CGE_FILE_ERROR)
return 0; return 0;
@@ -144,10 +157,13 @@ int CgeFileRead(CgeFile *file, void *buffer, size_t size, size_t *actual) {
if (readed < 0) if (readed < 0)
goto error; goto error;
if (readed > 0) file->flags &= ~CGE_FILE_EOF; if (readed > 0)
else file->flags |= CGE_FILE_EOF; file->flags &= ~CGE_FILE_EOF;
else
file->flags |= CGE_FILE_EOF;
if (actual) *actual = readed; if (actual)
*actual = readed;
return 1; return 1;
error: error:
@@ -160,6 +176,10 @@ int CgeFileWrite(CgeFile *file, const void *buffer, size_t size,
size_t *actual) { size_t *actual) {
ssize_t written; ssize_t written;
assert(file != NULL);
assert(file->handle != -1);
assert(buffer != NULL);
if (file->flags & CGE_FILE_ERROR) if (file->flags & CGE_FILE_ERROR)
return 0; return 0;
@@ -170,7 +190,8 @@ int CgeFileWrite(CgeFile *file, const void *buffer, size_t size,
if (written < 0) if (written < 0)
goto error; goto error;
if (actual) *actual = written; if (actual)
*actual = written;
return 1; return 1;
error: error:
@@ -180,6 +201,10 @@ error:
} }
int CgeFileTell(CgeFile *file, int64_t *offset) { int CgeFileTell(CgeFile *file, int64_t *offset) {
assert(file != NULL);
assert(file->handle != -1);
assert(offset != NULL);
if (file->flags & CGE_FILE_ERROR) if (file->flags & CGE_FILE_ERROR)
return 0; return 0;
@@ -195,6 +220,10 @@ error:
} }
int CgeFileSeek(CgeFile *file, int64_t offset, int whence) { 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) if (file->flags & CGE_FILE_ERROR)
return 0; return 0;
@@ -216,6 +245,9 @@ error:
} }
int CgeFileFlush(CgeFile *file) { int CgeFileFlush(CgeFile *file) {
assert(file != NULL);
assert(file->handle != -1);
if (fsync(file->handle)) if (fsync(file->handle))
goto error; goto error;
@@ -230,6 +262,10 @@ error:
int CgeFileSize(CgeFile *file, int64_t *size) { int CgeFileSize(CgeFile *file, int64_t *size) {
struct stat sb; struct stat sb;
assert(file != NULL);
assert(file->handle != -1);
assert(size != NULL);
if (fstat(file->handle, &sb)) if (fstat(file->handle, &sb))
goto error; goto error;
@@ -243,15 +279,22 @@ error:
} }
int CgeFileFlags(CgeFile *file, int *flags) { int CgeFileFlags(CgeFile *file, int *flags) {
assert(file != NULL);
assert(flags != NULL);
*flags = file->flags; *flags = file->flags;
return 1; return 1;
} }
int CgeFileClear(CgeFile *file) { int CgeFileClear(CgeFile *file) {
assert(file != NULL);
file->flags &= ~CGE_FILE_ERROR; file->flags &= ~CGE_FILE_ERROR;
return 1; return 1;
} }
int CgeFileErrorCode(CgeFile *file) { int CgeFileErrorCode(CgeFile *file) {
assert(file != NULL);
return file->lastError; return file->lastError;
} }

View File

@@ -1,5 +1,6 @@
#include "../CgeFile.h" #include "../CgeFile.h"
#include <windows.h> #include <windows.h>
#include <assert.h>
struct CgeFile { struct CgeFile {
HANDLE handle; HANDLE handle;
@@ -52,6 +53,9 @@ static int errorCodeFromError(void) {
static int fileInit(CgeFile *file, const char *path, int mode) { static int fileInit(CgeFile *file, const char *path, int mode) {
DWORD access = 0, how = 0; DWORD access = 0, how = 0;
assert(file != NULL);
assert(path != NULL);
if (!path) if (!path)
return CGE_FILE_EINVAL; return CGE_FILE_EINVAL;
@@ -96,17 +100,24 @@ CgeFile *CgeFileNew(const char *path, int mode, int *result) {
CgeFile *file; CgeFile *file;
int code = 0; int code = 0;
assert(path != NULL);
if ((file = malloc(sizeof(*file)))) { if ((file = malloc(sizeof(*file)))) {
if ((code = fileInit(file, path, mode))) { if ((code = fileInit(file, path, mode))) {
free(file); free(file);
file = NULL; file = NULL;
} }
} }
if (result) *result = code; if (result)
*result = code;
return file; return file;
} }
void CgeFileFree(CgeFile *file) { void CgeFileFree(CgeFile *file) {
if (!file)
return;
CloseHandle(file->handle); CloseHandle(file->handle);
free(file); free(file);
} }
@@ -114,15 +125,22 @@ void CgeFileFree(CgeFile *file) {
int CgeFileRead(CgeFile *file, void *buffer, size_t size, size_t *actual) { int CgeFileRead(CgeFile *file, void *buffer, size_t size, size_t *actual) {
DWORD readed; DWORD readed;
assert(file != NULL);
assert(file->handle != INVALID_HANDLE_VALUE);
assert(buffer != NULL);
if (file->flags & CGE_FILE_ERROR) if (file->flags & CGE_FILE_ERROR)
return 0; return 0;
if (!ReadFile(file->handle, buffer, (DWORD)size, &readed, NULL)) if (!ReadFile(file->handle, buffer, (DWORD)size, &readed, NULL))
goto error; goto error;
if (!readed) file->flags |= CGE_FILE_EOF; if (!readed)
else file->flags &= ~CGE_FILE_EOF; file->flags |= CGE_FILE_EOF;
if (actual) *actual = readed; else
file->flags &= ~CGE_FILE_EOF;
if (actual)
*actual = readed;
return 1; return 1;
@@ -137,6 +155,10 @@ int CgeFileWrite(CgeFile *file, const void *buffer, size_t size,
DWORD written; DWORD written;
LARGE_INTEGER position; LARGE_INTEGER position;
assert(file != NULL);
assert(file->handle != INVALID_HANDLE_VALUE);
assert(buffer != NULL);
if (file->flags & CGE_FILE_ERROR) if (file->flags & CGE_FILE_ERROR)
return 0; 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)) if (!WriteFile(file->handle, buffer, (DWORD)size, &written, NULL))
goto error; goto error;
if (actual) *actual = written; if (actual)
*actual = written;
return 1; return 1;
@@ -162,6 +185,10 @@ error:
int CgeFileTell(CgeFile *file, int64_t *offset) { int CgeFileTell(CgeFile *file, int64_t *offset) {
LARGE_INTEGER dummy, position; LARGE_INTEGER dummy, position;
assert(file != NULL);
assert(file->handle != INVALID_HANDLE_VALUE);
assert(offset != NULL);
if (file->flags & CGE_FILE_ERROR) if (file->flags & CGE_FILE_ERROR)
return 0; return 0;
@@ -181,6 +208,10 @@ error:
int CgeFileSeek(CgeFile *file, int64_t offset, int whence) { int CgeFileSeek(CgeFile *file, int64_t offset, int whence) {
LARGE_INTEGER position; 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) if (file->flags & CGE_FILE_ERROR)
return 0; return 0;
@@ -198,6 +229,9 @@ error:
} }
int CgeFileFlush(CgeFile *file) { int CgeFileFlush(CgeFile *file) {
assert(file != NULL);
assert(file->handle != INVALID_HANDLE_VALUE);
if (!FlushFileBuffers(file->handle)) if (!FlushFileBuffers(file->handle))
goto error; goto error;
@@ -212,6 +246,9 @@ error:
int CgeFileSize(CgeFile *file, int64_t *size) { int CgeFileSize(CgeFile *file, int64_t *size) {
LARGE_INTEGER dummy; LARGE_INTEGER dummy;
assert(file != NULL);
assert(file->handle != INVALID_HANDLE_VALUE);
if (file->flags & CGE_FILE_ERROR) if (file->flags & CGE_FILE_ERROR)
return 0; return 0;
@@ -228,15 +265,22 @@ error:
} }
int CgeFileFlags(CgeFile *file, int *flags) { int CgeFileFlags(CgeFile *file, int *flags) {
assert(file != NULL);
assert(flags != NULL);
*flags = file->flags; *flags = file->flags;
return 1; return 1;
} }
int CgeFileClear(CgeFile *file) { int CgeFileClear(CgeFile *file) {
assert(file != NULL);
file->flags &= ~CGE_FILE_ERROR; file->flags &= ~CGE_FILE_ERROR;
return 1; return 1;
} }
int CgeFileErrorCode(CgeFile *file) { int CgeFileErrorCode(CgeFile *file) {
assert(file != NULL);
return file->lastError; return file->lastError;
} }