Compare commits

...

3 Commits

Author SHA1 Message Date
trainstopperd 78ef2e7d2a test: unsigned add check 2023-09-06 23:28:33 -04:00
trainstopperd 75f0e66f1f feat: unsigned add checks 2023-09-06 23:25:37 -04:00
trainstopperd a2f2903f77 wip: readme 2023-09-06 23:10:02 -04:00
11 changed files with 203 additions and 4 deletions

29
Makefile.modernunix Normal file
View File

@ -0,0 +1,29 @@
CC=cc
CFLAGS=-Isrc -DCWARE_SAFETY_CHECKS -Wall -Wextra -g
OBJS=
BINS=tests/test tests/assert tests/unsigned_add_check
.SUFFIXES:
all: $(OBJS) $(BINS)
tests/test: tests/test.c src/liberror/public.h
$(CC) -o tests/test $(CFLAGS) $(LDFLAGS) $(OBJS) tests/test.c
tests/assert: tests/assert.c src/liberror/public.h
$(CC) -o tests/assert $(CFLAGS) $(LDFLAGS) $(OBJS) tests/assert.c
tests/unsigned_add_check: tests/unsigned_add_check.c src/liberror/public.h
$(CC) -o tests/unsigned_add_check $(CFLAGS) $(LDFLAGS) $(OBJS) tests/unsigned_add_check.c
clean:
rm -rf $(OBJS)
rm -rf $(BINS)
check:
/bin/echo Running test tests/test
tests/test
/bin/echo Running test tests/assert
tests/assert
/bin/echo Running test tests/unsigned_add_check
tests/unsigned_add_check

29
Makefile.msvc Normal file
View File

@ -0,0 +1,29 @@
CC=cl
CFLAGS=/I src /DCWARE_SAFETY_CHECKS /Zc:__STDC__
OBJS=
BINS=tests\test.exe tests\assert.exe tests\unsigned_add_check.exe
.SUFFIXES:
all: $(OBJS) $(BINS)
tests\test.exe: tests\test.c src\liberror\public.h
$(CC) /Fe:tests\test.exe $(CFLAGS) $(LDFLAGS) $(OBJS) tests\test.c
tests\assert.exe: tests\assert.c src\liberror\public.h
$(CC) /Fe:tests\assert.exe $(CFLAGS) $(LDFLAGS) $(OBJS) tests\assert.c
tests\unsigned_add_check.exe: tests\unsigned_add_check.c src\liberror\public.h
$(CC) /Fe:tests\unsigned_add_check.exe $(CFLAGS) $(LDFLAGS) $(OBJS) tests\unsigned_add_check.c
clean:
del $(OBJS)
del $(BINS)
check:
echo Running test tests\test
tests\test.exe
echo Running test tests\assert
tests\assert.exe
echo Running test tests\unsigned_add_check
tests\unsigned_add_check.exe

29
Makefile.oldunix Normal file
View File

@ -0,0 +1,29 @@
CC=cc
CFLAGS=-Isrc -DCWARE_SAFETY_CHECKS
OBJS=
BINS=tests/test tests/assert tests/unsigned_add_check
.SUFFIXES:
all: $(OBJS) $(BINS)
tests/test: tests/test.c src/liberror/public.h
$(CC) -o tests/test $(CFLAGS) $(LDFLAGS) $(OBJS) tests/test.c
tests/assert: tests/assert.c src/liberror/public.h
$(CC) -o tests/assert $(CFLAGS) $(LDFLAGS) $(OBJS) tests/assert.c
tests/unsigned_add_check: tests/unsigned_add_check.c src/liberror/public.h
$(CC) -o tests/unsigned_add_check $(CFLAGS) $(LDFLAGS) $(OBJS) tests/unsigned_add_check.c
clean:
rm -rf $(OBJS)
rm -rf $(BINS)
check:
/bin/echo Running test tests/test
tests/test
/bin/echo Running test tests/assert
tests/assert
/bin/echo Running test tests/unsigned_add_check
tests/unsigned_add_check

29
Makefile.unix Normal file
View File

@ -0,0 +1,29 @@
CC=cc
CFLAGS=-Isrc -DCWARE_SAFETY_CHECKS
OBJS=
BINS=tests/test tests/assert tests/unsigned_add_check
.SUFFIXES:
all: $(OBJS) $(BINS)
tests/test: tests/test.c src/liberror/public.h
$(CC) -o tests/test $(CFLAGS) $(LDFLAGS) $(OBJS) tests/test.c
tests/assert: tests/assert.c src/liberror/public.h
$(CC) -o tests/assert $(CFLAGS) $(LDFLAGS) $(OBJS) tests/assert.c
tests/unsigned_add_check: tests/unsigned_add_check.c src/liberror/public.h
$(CC) -o tests/unsigned_add_check $(CFLAGS) $(LDFLAGS) $(OBJS) tests/unsigned_add_check.c
clean:
rm -rf $(OBJS)
rm -rf $(BINS)
check:
/bin/echo Running test tests/test
tests/test
/bin/echo Running test tests/assert
tests/assert
/bin/echo Running test tests/unsigned_add_check
tests/unsigned_add_check

34
Makefile.watcom Normal file
View File

@ -0,0 +1,34 @@
CC=wcc386
LD=wlink
CFLAGS=-i=src -dDCWARE_SAFETY_CHECKS
OBJS=
LINKOBJS=
BINS=tests\test.exe tests\assert.exe tests\unsigned_add_check.exe
.SUFFIXES:
all: $(OBJS) $(BINS)
tests\test.exe: tests\test.c src\liberror\public.h
$(CC) -fo=tests\test.obj $(CFLAGS) tests\test.c
$(LD) $(LDFLAGS) $(LINKOBJS) FILE tests\test.obj NAME tests\test.exe
tests\assert.exe: tests\assert.c src\liberror\public.h
$(CC) -fo=tests\assert.obj $(CFLAGS) tests\assert.c
$(LD) $(LDFLAGS) $(LINKOBJS) FILE tests\assert.obj NAME tests\assert.exe
tests\unsigned_add_check.exe: tests\unsigned_add_check.c src\liberror\public.h
$(CC) -fo=tests\unsigned_add_check.obj $(CFLAGS) tests\unsigned_add_check.c
$(LD) $(LDFLAGS) $(LINKOBJS) FILE tests\unsigned_add_check.obj NAME tests\unsigned_add_check.exe
clean: .SYMBOLIC
del $(OBJS)
del $(BINS)
check: .SYMBOLIC
echo Running test tests\test
tests\test.exe
echo Running test tests\assert
tests\assert.exe
echo Running test tests\unsigned_add_check
tests\unsigned_add_check.exe

10
README Normal file
View File

@ -0,0 +1,10 @@
# liberror
Liberror is a runtime error checking library. It is used to make C programming
a bit safer than usual, providing toggable macros that will result in computer-readable
errors if the checks are failed.
The most important macro is *LIBERROR_ASSERT*, which serves as the building block for
other operations. If the expression evaluates to false, the error checks will be tripped
and a runtime error will be produced. Alongside this general purpose check, there is also
some checked math operations available.

View File

@ -28,9 +28,9 @@ example. Also provided is a basic API for adding additional runtime
.br
error checks such as whether or not a structure has been passed through
.br
an operation that releases it from memory. All of this can be enabled
an operation that releases it from memory. All of this can be disabled
.br
through the definition of the \fBCWARE_SAFETY_CHECKS\fR macro. The manuals for the
through the definition of the \fBLIBERROR_NO_SAFETY_CHECKS\fR macro. The manuals for the
.br
operations can be found below:
.br

View File

@ -1,7 +1,7 @@
#ifndef CWARE_LIBERROR_PUBLIC_H
#define CWARE_LIBERROR_PUBLIC_H
#if defined(CWARE_SAFETY_CHECKS)
#ifndef LIBERROR_NO_SAFETY_CHECKS
#define LIBERROR_ASSERT(expression, type, message, function) \
do { \
if((expression) != 0) { \
@ -15,10 +15,36 @@
#define LIBERROR_TEST(expression, message) \
LIBERROR_ASSERT(expression, "ASSERTION", message, "main")
#define LIBERROR_UNSIGNED_ADD_CHECK(a, b, type, message, function) \
do { \
/* a + b, where either a or b are zero, can never overflow. */ \
if(((a) == 0) || ((b) == 0)) { \
break; \
} \
\
if((a) > (b)) { \
/* a + b is greater or equal to a, so there was no overflow. */ \
if(((a) + (b)) >= (a)) { \
break; \
} \
\
LIBERROR_ASSERT(1 == 0, type, message, function); \
} else if((a) <= (b)) { \
/* a + b is greater or equal to b, so there was no overflow. */ \
if(((a) + (b)) >= (b)) { \
break; \
} \
\
LIBERROR_ASSERT(1 == 0, type, message, function); \
} \
} while(0)
#else
#define LIBERROR_ASSERT(expression, message)
#define LIBERROR_TEST(expression, type, message, function)
#define LIBERROR_UNSIGNED_ADD_CHECk(a, b, type, message, function)
#endif
#endif

13
tests/unsigned_add_check.c Executable file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#include "liberror/public.h"
int main() {
unsigned int x = 0;
unsigned int y = 9;
LIBERROR_UNSIGNED_ADD_CHECK(x, y, "PRECONDITION", "Overflow!", "main");
return 0;
}