Skip to content

Commit

Permalink
Merge branch 'develop' into 20190908_lint
Browse files Browse the repository at this point in the history
  • Loading branch information
camgunz authored Sep 7, 2020
2 parents 437f29e + 775f7bb commit 7b1d4d3
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
# Coverage files
*.gcda
*.gcno
coverage/

# Testing executable
test/test-cmp
cmptest
cmptest2
cmpaddrtest
cmpmemtest
cmpnofloattest
cmpubtest

# Example executables
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Edit following two lines to set component requirements (see docs)
set(COMPONENT_SRCS "cmp.c" )
set(COMPONENT_ADD_INCLUDEDIRS ".")
register_component()
35 changes: 25 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ CC ?= gcc
CLANG ?= clang

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
CMPCFLAGS ?= -std=c89 -Wno-c99-extensions
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
Expand All @@ -13,37 +14,50 @@ UBCFLAGS ?= -fsanitize=undefined

all: cmptest example1 example2

test: addrtest memtest ubtest unittest

unittest: cmptest
@./cmptest
test: addrtest memtest nofloattest ubtest unittest

addrtest: cmpaddrtest
@./cmpaddrtest

memtest: cmpmemtest
@./cmpmemtest

nofloattest: cmpnofloattest
@./cmpnofloattest
@rm -f *.gcno *.gcda *.info

ubtest: cmpubtest
@./cmpubtest

unittest: cmptest
@./cmptest

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

cmptest: cmp.o
$(CC) $(CFLAGS) $(TESTCFLAGS) -fprofile-arcs -ftest-coverage -g -I. -o cmptest cmp.o test/test.c test/buf.c test/utils.c -lcmocka
$(CC) $(CFLAGS) $(TESTCFLAGS) -fprofile-arcs -ftest-coverage -g -I. \
-o cmptest cmp.o test/test.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/buf.c test/utils.c -lcmocka

clangcmp.o:
$(CLANG) $(CFLAGS) $(CMPCFLAGS) -fprofile-arcs -ftest-coverage -g -I. -c cmp.c -o clangcmp.o
$(CLANG) $(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/buf.c test/utils.c -lcmocka
$(CLANG) $(CFLAGS) $(TESTCFLAGS) $(ADDRCFLAGS) -I. -o cmpaddrtest cmp.c \
test/test.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/buf.c test/utils.c -lcmocka
$(CLANG) $(CFLAGS) $(TESTCFLAGS) $(MEMCFLAGS) -I. -o cmpmemtest cmp.c \
test/test.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/buf.c test/utils.c -lcmocka
$(CLANG) $(CFLAGS) $(TESTCFLAGS) $(UBCFLAGS) -I. -o cmpubtest cmp.c \
test/test.c test/buf.c test/utils.c -lcmocka

example1:
$(CC) $(CFLAGS) --std=c89 -O3 -I. -o example1 cmp.c examples/example1.c
Expand All @@ -66,6 +80,7 @@ clean:
@rm -f cmpaddrtest
@rm -f cmpmemtest
@rm -f cmpubtest
@rm -f cmpnofloattest
@rm -f example1
@rm -f example2
@rm -f *.o
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,8 @@ Use these functions in lieu of their v5 counterparts:
- `cmp_write_str_v4` instead of `cmp_write_str`
- `cmp_write_object_v4` instead of `cmp_write_object`
## Disabling Floating Point Operations
Thanks to [tdragon](https://github.com/tdragon) it's possible to disable
floating point operations in CMP by defining `CMP_NO_FLOAT`. No floating point
functionality will be included. Fair warning: this changes the ABI.
40 changes: 40 additions & 0 deletions cmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ enum {
LENGTH_WRITING_ERROR,
SKIP_DEPTH_LIMIT_EXCEEDED_ERROR,
INTERNAL_ERROR,
DISABLED_FLOATING_POINT_ERROR,
ERROR_MAX
};

Expand All @@ -117,6 +118,7 @@ const char * const cmp_error_messages[ERROR_MAX + 1] = {
"Error writing size",
"Depth limit exceeded while skipping",
"Internal error",
"Floating point operations disabled",
"Max Error"
};

Expand Down Expand Up @@ -181,6 +183,7 @@ static uint64_t be64(uint64_t x) {
return x;
}

#ifndef CMP_NO_FLOAT
static float decode_befloat(const char *b) {
float f = 0.;
char *fb = (char *)&f;
Expand Down Expand Up @@ -212,6 +215,7 @@ static double decode_bedouble(const char *b) {

return d;
}
#endif /* CMP_NO_FLOAT */

static bool read_byte(cmp_ctx_t *ctx, uint8_t *x) {
return ctx->read(ctx, x, sizeof(uint8_t));
Expand Down Expand Up @@ -641,6 +645,7 @@ static bool read_obj_data(cmp_ctx_t *ctx, uint8_t type_marker,
return true;
case CMP_TYPE_FLOAT:
{
#ifndef CMP_NO_FLOAT
char bytes[4];

if (!ctx->read(ctx, bytes, 4)) {
Expand All @@ -649,9 +654,14 @@ static bool read_obj_data(cmp_ctx_t *ctx, uint8_t type_marker,
}
obj->as.flt = decode_befloat(bytes);
return true;
#else /* CMP_NO_FLOAT */
ctx->error = DISABLED_FLOATING_POINT_ERROR;
return false;
#endif /* CMP_NO_FLOAT */
}
case CMP_TYPE_DOUBLE:
{
#ifndef CMP_NO_FLOAT
char bytes[8];

if (!ctx->read(ctx, bytes, 8)) {
Expand All @@ -660,6 +670,10 @@ static bool read_obj_data(cmp_ctx_t *ctx, uint8_t type_marker,
}
obj->as.dbl = decode_bedouble(bytes);
return true;
#else /* CMP_NO_FLOAT */
ctx->error = DISABLED_FLOATING_POINT_ERROR;
return false;
#endif /* CMP_NO_FLOAT */
}
case CMP_TYPE_BIN8:
case CMP_TYPE_BIN16:
Expand Down Expand Up @@ -901,6 +915,7 @@ bool cmp_write_uinteger(cmp_ctx_t *ctx, uint64_t u) {
return cmp_write_u64(ctx, u);
}

#ifndef CMP_NO_FLOAT
bool cmp_write_float(cmp_ctx_t *ctx, float f) {
if (!write_type_marker(ctx, FLOAT_MARKER))
return false;
Expand Down Expand Up @@ -952,6 +967,7 @@ bool cmp_write_decimal(cmp_ctx_t *ctx, double d) {
else
return cmp_write_double(ctx, d);
}
#endif /* CMP_NO_FLOAT */

bool cmp_write_nil(cmp_ctx_t *ctx) {
return write_type_marker(ctx, NIL_MARKER);
Expand Down Expand Up @@ -1569,9 +1585,19 @@ bool cmp_write_object(cmp_ctx_t *ctx, const cmp_object_t *obj) {
case CMP_TYPE_EXT32:
return cmp_write_ext32_marker(ctx, obj->as.ext.type, obj->as.ext.size);
case CMP_TYPE_FLOAT:
#ifndef CMP_NO_FLOAT
return cmp_write_float(ctx, obj->as.flt);
#else /* CMP_NO_FLOAT */
ctx->error = DISABLED_FLOATING_POINT_ERROR;
return false;
#endif /* CMP_NO_FLOAT */
case CMP_TYPE_DOUBLE:
#ifndef CMP_NO_FLOAT
return cmp_write_double(ctx, obj->as.dbl);
#else /* CMP_NO_FLOAT */
ctx->error = DISABLED_FLOATING_POINT_ERROR;
return false;
#endif
case CMP_TYPE_UINT8:
return cmp_write_u8(ctx, obj->as.u8);
case CMP_TYPE_UINT16:
Expand Down Expand Up @@ -1645,9 +1671,19 @@ bool cmp_write_object_v4(cmp_ctx_t *ctx, const cmp_object_t *obj) {
case CMP_TYPE_EXT32:
return cmp_write_ext32_marker(ctx, obj->as.ext.type, obj->as.ext.size);
case CMP_TYPE_FLOAT:
#ifndef CMP_NO_FLOAT
return cmp_write_float(ctx, obj->as.flt);
#else /* CMP_NO_FLOAT */
ctx->error = DISABLED_FLOATING_POINT_ERROR;
return false;
#endif
case CMP_TYPE_DOUBLE:
#ifndef CMP_NO_FLOAT
return cmp_write_double(ctx, obj->as.dbl);
#else
ctx->error = DISABLED_FLOATING_POINT_ERROR;
return false;
#endif
case CMP_TYPE_UINT8:
return cmp_write_u8(ctx, obj->as.u8);
case CMP_TYPE_UINT16:
Expand Down Expand Up @@ -2171,6 +2207,7 @@ bool cmp_read_uinteger(cmp_ctx_t *ctx, uint64_t *d) {
return cmp_read_ulong(ctx, d);
}

#ifndef CMP_NO_FLOAT
bool cmp_read_float(cmp_ctx_t *ctx, float *f) {
cmp_object_t obj;

Expand Down Expand Up @@ -2221,6 +2258,7 @@ bool cmp_read_decimal(cmp_ctx_t *ctx, double *d) {
return false;
}
}
#endif /* CMP_NO_FLOAT */

bool cmp_read_nil(cmp_ctx_t *ctx) {
cmp_object_t obj;
Expand Down Expand Up @@ -3342,6 +3380,7 @@ bool cmp_object_as_uinteger(const cmp_object_t *obj, uint64_t *d) {
return cmp_object_as_ulong(obj, d);
}

#ifndef CMP_NO_FLOAT
bool cmp_object_as_float(const cmp_object_t *obj, float *f) {
if (obj->type == CMP_TYPE_FLOAT) {
*f = obj->as.flt;
Expand All @@ -3359,6 +3398,7 @@ bool cmp_object_as_double(const cmp_object_t *obj, double *d) {

return false;
}
#endif /* CMP_NO_FLOAT */

bool cmp_object_as_bool(const cmp_object_t *obj, bool *b) {
if (obj->type == CMP_TYPE_BOOLEAN) {
Expand Down
14 changes: 12 additions & 2 deletions cmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ union cmp_object_data_u {
int16_t s16;
int32_t s32;
int64_t s64;
#ifndef CMP_NO_FLOAT
float flt;
double dbl;
#endif /* CMP_NO_FLOAT */
uint32_t array_size;
uint32_t map_size;
uint32_t str_size;
Expand Down Expand Up @@ -151,7 +153,9 @@ bool cmp_write_uinteger(cmp_ctx_t *ctx, uint64_t u);
* Writes a floating-point value (either single or double-precision) to the
* backend
*/
#ifndef CMP_NO_FLOAT
bool cmp_write_decimal(cmp_ctx_t *ctx, double d);
#endif /* CMP_NO_FLOAT */

/* Writes NULL to the backend */
bool cmp_write_nil(cmp_ctx_t *ctx);
Expand Down Expand Up @@ -269,7 +273,9 @@ bool cmp_read_uinteger(cmp_ctx_t *ctx, uint64_t *u);
* Reads a floating point value (either single or double-precision) from the
* backend
*/
#ifndef CMP_NO_FLOAT
bool cmp_read_decimal(cmp_ctx_t *ctx, double *d);
#endif /* CMP_NO_FLOAT */

/* "Reads" (more like "skips") a NULL value from the backend */
bool cmp_read_nil(cmp_ctx_t *ctx);
Expand Down Expand Up @@ -326,8 +332,8 @@ bool cmp_read_object(cmp_ctx_t *ctx, cmp_object_t *obj);
bool cmp_skip_object(cmp_ctx_t *ctx, cmp_object_t *obj);

/*
* This is similar to `cmp_skip_object_flat`, except it tolerates flat arrays
* and maps. If when skipping such an array or map this function encounteres
* This is similar to `cmp_skip_object`, except it tolerates flat arrays and
* maps. If when skipping such an array or map this function encounters
* another array/map, it will:
* - If `obj` is not `NULL`, fill in `obj` with that (nested) object
* - Set `ctx->error` to `SKIP_DEPTH_LIMIT_EXCEEDED_ERROR`
Expand Down Expand Up @@ -401,8 +407,10 @@ bool cmp_write_u16(cmp_ctx_t *ctx, uint16_t s);
bool cmp_write_u32(cmp_ctx_t *ctx, uint32_t i);
bool cmp_write_u64(cmp_ctx_t *ctx, uint64_t l);

#ifndef CMP_NO_FLOAT
bool cmp_write_float(cmp_ctx_t *ctx, float f);
bool cmp_write_double(cmp_ctx_t *ctx, double d);
#endif /* CMP_NO_FLOAT */

bool cmp_write_fixstr_marker(cmp_ctx_t *ctx, uint8_t size);
bool cmp_write_fixstr(cmp_ctx_t *ctx, const char *data, uint8_t size);
Expand Down Expand Up @@ -464,8 +472,10 @@ bool cmp_read_u16(cmp_ctx_t *ctx, uint16_t *s);
bool cmp_read_u32(cmp_ctx_t *ctx, uint32_t *i);
bool cmp_read_u64(cmp_ctx_t *ctx, uint64_t *l);

#ifndef CMP_NO_FLOAT
bool cmp_read_float(cmp_ctx_t *ctx, float *f);
bool cmp_read_double(cmp_ctx_t *ctx, double *d);
#endif /* CMP_NO_FLOAT */

bool cmp_read_fixext1_marker(cmp_ctx_t *ctx, int8_t *type);
bool cmp_read_fixext1(cmp_ctx_t *ctx, int8_t *type, void *data);
Expand Down
4 changes: 4 additions & 0 deletions test/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ void M_BufferWriteULongs(buf_t *buf, const uint64_t *ulongs, size_t count) {
M_BufferWriteChars(buf, (char *)ulongs, count * sizeof(uint64_t));
}

#ifndef CMP_NO_FLOAT
void M_BufferWriteFloat(buf_t *buf, float f) {
M_BufferWriteFloats(buf, &f, 1);
}
Expand All @@ -319,6 +320,7 @@ void M_BufferWriteDoubles(buf_t *buf, const double *doubles, size_t count) {
M_BufferEnsureCapacity(buf, count * sizeof(double));
M_BufferWriteChars(buf, (char *)doubles, count * sizeof(doubles));
}
#endif

void M_BufferWriteString(buf_t *buf, const char *string, size_t length) {
M_BufferEnsureCapacity(buf, length + 1);
Expand Down Expand Up @@ -440,6 +442,7 @@ bool M_BufferReadULongs(buf_t *buf, uint64_t *l, size_t count) {
return M_BufferRead(buf, l, count * sizeof(uint64_t));
}

#ifndef CMP_NO_FLOAT
bool M_BufferReadFloat(buf_t *buf, float *f) {
return M_BufferReadFloats(buf, f, 1);
}
Expand All @@ -455,6 +458,7 @@ bool M_BufferReadDouble(buf_t *buf, double *d) {
bool M_BufferReadDoubles(buf_t *buf, double *d, size_t count) {
return M_BufferRead(buf, d, count * sizeof(double));
}
#endif

bool M_BufferReadString(buf_t *buf, char *s, size_t length) {
return M_BufferRead(buf, s, length);
Expand Down
4 changes: 4 additions & 0 deletions test/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ void M_BufferWriteLong(buf_t *buf, int64_t l);
void M_BufferWriteLongs(buf_t *buf, const int64_t *longs, size_t count);
void M_BufferWriteULong(buf_t *buf, uint64_t l);
void M_BufferWriteULongs(buf_t *buf, const uint64_t *longs, size_t count);
#ifndef CMP_NO_FLOAT
void M_BufferWriteFloat(buf_t *buf, float f);
void M_BufferWriteFloats(buf_t *buf, const float *floats, size_t count);
void M_BufferWriteDouble(buf_t *buf, double d);
void M_BufferWriteDoubles(buf_t *buf, const double *doubles, size_t count);
#endif
void M_BufferWriteString(buf_t *buf, const char *string, size_t length);
void M_BufferWriteZeros(buf_t *buf, size_t count);

Expand All @@ -111,10 +113,12 @@ bool M_BufferReadLong(buf_t *buf, int64_t *l);
bool M_BufferReadLongs(buf_t *buf, int64_t *l, size_t count);
bool M_BufferReadULong(buf_t *buf, uint64_t *l);
bool M_BufferReadULongs(buf_t *buf, uint64_t *l, size_t count);
#ifndef CMP_NO_FLOAT
bool M_BufferReadFloat(buf_t *buf, float *f);
bool M_BufferReadFloats(buf_t *buf, float *f, size_t count);
bool M_BufferReadDouble(buf_t *buf, double *d);
bool M_BufferReadDoubles(buf_t *buf, double *d, size_t count);
#endif
bool M_BufferReadString(buf_t *buf, char *s, size_t length);
bool M_BufferReadStringDup(buf_t *buf, char **s);
bool M_BufferCopyString(buf_t *dst, buf_t *src);
Expand Down
Loading

0 comments on commit 7b1d4d3

Please sign in to comment.