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 <assert.h>
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;
}

View File

@@ -8,6 +8,7 @@
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
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;
}

View File

@@ -1,5 +1,6 @@
#include "../CgeFile.h"
#include <windows.h>
#include <assert.h>
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;
}