Compare commits
3 Commits
1220c1139d
...
78ef2e7d2a
Author | SHA1 | Date |
---|---|---|
|
78ef2e7d2a | |
|
75f0e66f1f | |
|
a2f2903f77 |
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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.
|
|
@ -28,9 +28,9 @@ example. Also provided is a basic API for adding additional runtime
|
||||||
.br
|
.br
|
||||||
error checks such as whether or not a structure has been passed through
|
error checks such as whether or not a structure has been passed through
|
||||||
.br
|
.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
|
.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
|
.br
|
||||||
operations can be found below:
|
operations can be found below:
|
||||||
.br
|
.br
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#ifndef CWARE_LIBERROR_PUBLIC_H
|
#ifndef CWARE_LIBERROR_PUBLIC_H
|
||||||
#define 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) \
|
#define LIBERROR_ASSERT(expression, type, message, function) \
|
||||||
do { \
|
do { \
|
||||||
if((expression) != 0) { \
|
if((expression) != 0) { \
|
||||||
|
@ -15,10 +15,36 @@
|
||||||
#define LIBERROR_TEST(expression, message) \
|
#define LIBERROR_TEST(expression, message) \
|
||||||
LIBERROR_ASSERT(expression, "ASSERTION", message, "main")
|
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
|
#else
|
||||||
|
|
||||||
#define LIBERROR_ASSERT(expression, message)
|
#define LIBERROR_ASSERT(expression, message)
|
||||||
#define LIBERROR_TEST(expression, type, message, function)
|
#define LIBERROR_TEST(expression, type, message, function)
|
||||||
|
#define LIBERROR_UNSIGNED_ADD_CHECk(a, b, type, message, function)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
Loading…
Reference in New Issue