Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
camgunz committed Mar 29, 2022
2 parents 88ed040 + a8b526e commit c039b41
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 46 deletions.
58 changes: 39 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
CC ?= gcc
CLANG ?= clang

CFLAGS ?= -Werror -Wall -Wextra -funsigned-char -fwrapv -Wconversion -Wno-c99-extensions -Wno-sign-conversion -Wmissing-format-attribute -Wpointer-arith -Wformat-nonliteral -Winit-self -Wwrite-strings -Wshadow -Wenum-compare -Wempty-body -Wparentheses -Wcast-align -Wstrict-aliasing --pedantic-errors
EXTRA_CFLAGS ?= -Werror -Wall -Wextra -funsigned-char -fwrapv -Wconversion \
-Wno-sign-conversion -Wmissing-format-attribute \
-Wpointer-arith -Wformat-nonliteral -Winit-self \
-Wwrite-strings -Wshadow \ -Wenum-compare -Wempty-body \
-Wparentheses -Wcast-align -Wstrict-aliasing --pedantic-errors
CMPCFLAGS ?= -std=c89 -Wno-c99-extensions
TESTCFLAGS ?= -std=c99 -Wno-error=deprecated-declarations -Wno-deprecated-declarations -O0
TESTCFLAGS ?= -std=c99 -Wno-error=deprecated-declarations \
-Wno-deprecated-declarations -O0
NOFPUTESTCFLAGS ?= $(TESTCFLAGS) -DCMP_NO_FLOAT

ADDRCFLAGS ?= -fsanitize=address
MEMCFLAGS ?= -fsanitize=memory -fno-omit-frame-pointer -fno-optimize-sibling-calls
MEMCFLAGS ?= -fsanitize=memory -fno-omit-frame-pointer \
-fno-optimize-sibling-calls
UBCFLAGS ?= -fsanitize=undefined

.PHONY: all clean test coverage
Expand Down Expand Up @@ -40,43 +46,57 @@ unittest: cmpunittest
@./cmpunittest

cmp.o: cmp.c
$(CC) $(CFLAGS) $(CMPCFLAGS) -fprofile-arcs -ftest-coverage -g -I. -c cmp.c
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CMPCFLAGS) \
-fprofile-arcs -ftest-coverage -g -I. -c cmp.c

cmpprof: cmp.o
$(CC) $(CFLAGS) $(TESTCFLAGS) -fprofile-arcs -I. \
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(TESTCFLAGS) $(LDFLAGS) \
-fprofile-arcs -I. \
-o cmpprof cmp.o test/profile.c test/tests.c test/buf.c test/utils.c \
-lcmocka -lprofiler

cmpunittest: cmp.o
$(CC) $(CFLAGS) $(TESTCFLAGS) -fprofile-arcs -ftest-coverage -g -I. \
-o cmpunittest cmp.o test/test.c test/tests.c test/buf.c test/utils.c -lcmocka
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(TESTCFLAGS) $(LDFLAGS) \
-fprofile-arcs -ftest-coverage -g -I. \
-o cmptest cmp.o test/test.c test/tests.c test/buf.c test/utils.c \
-lcmocka

cmpnofloattest: cmp.o
$(CC) $(CFLAGS) $(NOFPUTESTCFLAGS) -fprofile-arcs -ftest-coverage -g -I. \
-o cmpnofloattest cmp.o test/test.c test/tests.c test/buf.c test/utils.c \
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(NOFPUTESTCFLAGS) $(LDFLAGS) \
-fprofile-arcs -ftest-coverage -g -I. \
-o cmpnofloattest cmp.o test/test.c test/tests.c test/buf.c \
test/utils.c \
-lcmocka

clangcmp.o: cmp.c
$(CLANG) $(CFLAGS) $(CMPCFLAGS) -fprofile-arcs -ftest-coverage -g -I. \
-c cmp.c -o clangcmp.o
$(CLANG) $(CFLAGS) $(EXTRA_CFLAGS) $(CMPCFLAGS) \
-fprofile-arcs -ftest-coverage -g -I. -c cmp.c -o clangcmp.o

cmpaddrtest: clangcmp.o clean
$(CLANG) $(CFLAGS) $(TESTCFLAGS) $(ADDRCFLAGS) -I. -o cmpaddrtest cmp.c \
test/test.c test/tests.c test/buf.c test/utils.c -lcmocka
$(CLANG) $(CFLAGS) $(EXTRA_CFLAGS) $(TESTCFLAGS) $(ADDRCFLAGS) $(LDFLAGS) \
-I. -o cmpaddrtest \
cmp.c test/test.c test/tests.c test/buf.c test/utils.c \
-lcmocka

cmpmemtest: clangcmp.o clean
$(CLANG) $(CFLAGS) $(TESTCFLAGS) $(MEMCFLAGS) -I. -o cmpmemtest cmp.c \
test/test.c test/tests.c test/buf.c test/utils.c -lcmocka
$(CLANG) $(CFLAGS) $(EXTRA_CFLAGS) $(TESTCFLAGS) $(MEMCFLAGS) $(LDFLAGS) \
-I. -o cmpmemtest \
cmp.c test/test.c test/tests.c test/buf.c test/utils.c \
-lcmocka

cmpubtest: clangcmp.o clean
$(CLANG) $(CFLAGS) $(TESTCFLAGS) $(UBCFLAGS) -I. -o cmpubtest cmp.c \
test/test.c test/tests.c test/buf.c test/utils.c -lcmocka
$(CLANG) $(CFLAGS) $(EXTRA_CFLAGS) $(TESTCFLAGS) $(UBCFLAGS) $(LDFLAGS) \
-I. -o cmpubtest \
cmp.c test/test.c test/tests.c test/buf.c test/utils.c \
-lcmocka

example1:
$(CC) $(CFLAGS) --std=c89 -O3 -I. -o example1 cmp.c examples/example1.c
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) --std=c89 -O3 -I. -o example1 \
cmp.c examples/example1.c

example2:
$(CC) $(CFLAGS) --std=c89 -O3 -I. -o example2 cmp.c examples/example2.c
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) --std=c89 -O3 -I. -o example2 \
cmp.c examples/example2.c

coverage:
@rm -f base_coverage.info test_coverage.info total_coverage.info
Expand Down
66 changes: 39 additions & 27 deletions cmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ static uint16_t be16(uint16_t x) {
return x;
}

static int16_t sbe16(int16_t x) {
return (int16_t)be16((uint16_t)x);
}

static uint32_t be32(uint32_t x) {
char *b = (char *)&x;

Expand All @@ -153,6 +157,10 @@ static uint32_t be32(uint32_t x) {
return x;
}

static int32_t sbe32(int32_t x) {
return (int32_t)be32((uint32_t)x);
}

static uint64_t be64(uint64_t x) {
char *b = (char *)&x;

Expand All @@ -179,6 +187,10 @@ static uint64_t be64(uint64_t x) {
return x;
}

static int64_t sbe64(int64_t x) {
return (int64_t)be64((uint64_t)x);
}

#ifndef CMP_NO_FLOAT
static float decode_befloat(const char *b) {
float f = 0.;
Expand Down Expand Up @@ -583,7 +595,7 @@ static bool read_obj_data(cmp_ctx_t *ctx, uint8_t type_marker,
obj->as.u8 = type_marker;
return true;
case CMP_TYPE_NEGATIVE_FIXNUM:
obj->as.s8 = type_marker;
obj->as.s8 = (int8_t)type_marker;
return true;
case CMP_TYPE_NIL:
obj->as.u8 = 0;
Expand Down Expand Up @@ -639,21 +651,21 @@ static bool read_obj_data(cmp_ctx_t *ctx, uint8_t type_marker,
ctx->error = DATA_READING_ERROR;
return false;
}
obj->as.s16 = be16(obj->as.s16);
obj->as.s16 = sbe16(obj->as.s16);
return true;
case CMP_TYPE_SINT32:
if (!ctx->read(ctx, &obj->as.s32, sizeof(int32_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
obj->as.s32 = be32(obj->as.s32);
obj->as.s32 = sbe32(obj->as.s32);
return true;
case CMP_TYPE_SINT64:
if (!ctx->read(ctx, &obj->as.s64, sizeof(int64_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
obj->as.s64 = be64(obj->as.s64);
obj->as.s64 = sbe64(obj->as.s64);
return true;
case CMP_TYPE_FLOAT:
{
Expand Down Expand Up @@ -809,15 +821,15 @@ bool cmp_write_pfix(cmp_ctx_t *ctx, uint8_t c) {

bool cmp_write_nfix(cmp_ctx_t *ctx, int8_t c) {
if (c >= -32 && c <= -1)
return write_fixed_value(ctx, c);
return write_fixed_value(ctx, (uint8_t)c);

ctx->error = INPUT_VALUE_TOO_LARGE_ERROR;
return false;
}

bool cmp_write_sfix(cmp_ctx_t *ctx, int8_t c) {
if (c >= 0)
return cmp_write_pfix(ctx, c);
return cmp_write_pfix(ctx, (uint8_t)c);
if (c >= -32 && c <= -1)
return cmp_write_nfix(ctx, c);

Expand All @@ -836,7 +848,7 @@ bool cmp_write_s16(cmp_ctx_t *ctx, int16_t s) {
if (!write_type_marker(ctx, S16_MARKER))
return false;

s = be16(s);
s = sbe16(s);

return ctx->write(ctx, &s, sizeof(int16_t));
}
Expand All @@ -845,7 +857,7 @@ bool cmp_write_s32(cmp_ctx_t *ctx, int32_t i) {
if (!write_type_marker(ctx, S32_MARKER))
return false;

i = be32(i);
i = sbe32(i);

return ctx->write(ctx, &i, sizeof(int32_t));
}
Expand All @@ -854,14 +866,14 @@ bool cmp_write_s64(cmp_ctx_t *ctx, int64_t l) {
if (!write_type_marker(ctx, S64_MARKER))
return false;

l = be64(l);
l = sbe64(l);

return ctx->write(ctx, &l, sizeof(int64_t));
}

bool cmp_write_integer(cmp_ctx_t *ctx, int64_t d) {
if (d >= 0)
return cmp_write_uinteger(ctx, d);
return cmp_write_uinteger(ctx, (uint64_t)d);
if (d >= -32)
return cmp_write_nfix(ctx, (int8_t)d);
if (d >= -128)
Expand Down Expand Up @@ -1861,7 +1873,7 @@ bool cmp_read_char(cmp_ctx_t *ctx, int8_t *c) {
return true;
case CMP_TYPE_UINT8:
if (obj.as.u8 <= 127) {
*c = obj.as.u8;
*c = (int8_t)obj.as.u8;
return true;
}
break;
Expand Down Expand Up @@ -1893,7 +1905,7 @@ bool cmp_read_short(cmp_ctx_t *ctx, int16_t *s) {
return true;
case CMP_TYPE_UINT16:
if (obj.as.u16 <= 32767) {
*s = obj.as.u16;
*s = (int16_t)obj.as.u16;
return true;
}
break;
Expand Down Expand Up @@ -1931,7 +1943,7 @@ bool cmp_read_int(cmp_ctx_t *ctx, int32_t *i) {
return true;
case CMP_TYPE_UINT32:
if (obj.as.u32 <= 2147483647) {
*i = obj.as.u32;
*i = (int32_t)obj.as.u32;
return true;
}
break;
Expand Down Expand Up @@ -1975,7 +1987,7 @@ bool cmp_read_long(cmp_ctx_t *ctx, int64_t *d) {
return true;
case CMP_TYPE_UINT64:
if (obj.as.u64 <= 9223372036854775807) {
*d = obj.as.u64;
*d = (int64_t)obj.as.u64;
return true;
}
break;
Expand Down Expand Up @@ -2069,7 +2081,7 @@ bool cmp_read_uchar(cmp_ctx_t *ctx, uint8_t *c) {
case CMP_TYPE_NEGATIVE_FIXNUM:
case CMP_TYPE_SINT8:
if (obj.as.s8 >= 0) {
*c = obj.as.s8;
*c = (uint8_t)obj.as.s8;
return true;
}
break;
Expand Down Expand Up @@ -2098,13 +2110,13 @@ bool cmp_read_ushort(cmp_ctx_t *ctx, uint16_t *s) {
case CMP_TYPE_NEGATIVE_FIXNUM:
case CMP_TYPE_SINT8:
if (obj.as.s8 >= 0) {
*s = obj.as.s8;
*s = (uint8_t)obj.as.s8;
return true;
}
break;
case CMP_TYPE_SINT16:
if (obj.as.s16 >= 0) {
*s = obj.as.s16;
*s = (uint16_t)obj.as.s16;
return true;
}
break;
Expand Down Expand Up @@ -2136,19 +2148,19 @@ bool cmp_read_uint(cmp_ctx_t *ctx, uint32_t *i) {
case CMP_TYPE_NEGATIVE_FIXNUM:
case CMP_TYPE_SINT8:
if (obj.as.s8 >= 0) {
*i = obj.as.s8;
*i = (uint8_t)obj.as.s8;
return true;
}
break;
case CMP_TYPE_SINT16:
if (obj.as.s16 >= 0) {
*i = obj.as.s16;
*i = (uint16_t)obj.as.s16;
return true;
}
break;
case CMP_TYPE_SINT32:
if (obj.as.s32 >= 0) {
*i = obj.as.s32;
*i = (uint32_t)obj.as.s32;
return true;
}
break;
Expand Down Expand Up @@ -2183,25 +2195,25 @@ bool cmp_read_ulong(cmp_ctx_t *ctx, uint64_t *u) {
case CMP_TYPE_NEGATIVE_FIXNUM:
case CMP_TYPE_SINT8:
if (obj.as.s8 >= 0) {
*u = obj.as.s8;
*u = (uint8_t)obj.as.s8;
return true;
}
break;
case CMP_TYPE_SINT16:
if (obj.as.s16 >= 0) {
*u = obj.as.s16;
*u = (uint16_t)obj.as.s16;
return true;
}
break;
case CMP_TYPE_SINT32:
if (obj.as.s32 >= 0) {
*u = obj.as.s32;
*u = (uint32_t)obj.as.s32;
return true;
}
break;
case CMP_TYPE_SINT64:
if (obj.as.s64 >= 0) {
*u = obj.as.s64;
*u = (uint64_t)obj.as.s64;
return true;
}
break;
Expand Down Expand Up @@ -3243,7 +3255,7 @@ bool cmp_object_as_short(const cmp_object_t *obj, int16_t *s) {
return true;
case CMP_TYPE_UINT16:
if (obj->as.u16 <= 32767) {
*s = obj->as.u16;
*s = (int16_t)obj->as.u16;
return true;
}
else {
Expand Down Expand Up @@ -3275,7 +3287,7 @@ bool cmp_object_as_int(const cmp_object_t *obj, int32_t *i) {
return true;
case CMP_TYPE_UINT32:
if (obj->as.u32 <= 2147483647) {
*i = obj->as.u32;
*i = (int32_t)obj->as.u32;
return true;
}
else {
Expand Down Expand Up @@ -3313,7 +3325,7 @@ bool cmp_object_as_long(const cmp_object_t *obj, int64_t *d) {
return true;
case CMP_TYPE_UINT64:
if (obj->as.u64 <= 9223372036854775807) {
*d = obj->as.u64;
*d = (int64_t)obj->as.u64;
return true;
}
else {
Expand Down

0 comments on commit c039b41

Please sign in to comment.