libslip-c

C SLIP-encoding library
git clone https://git.sinitax.com/sinitax/libslip-c
Log | Files | Refs | LICENSE | sfeed.txt

commit d0a0cbf86a3bb16fe11125df35edaba57bc50d43
parent 98f8cbd013de346af67715175158356e69820f69
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 27 Feb 2023 23:48:01 +0100

Make assert non-optional

Diffstat:
MMakefile | 2+-
Minclude/slip.h | 20--------------------
Msrc/slip.c | 23+++++++++++++++++------
3 files changed, 18 insertions(+), 27 deletions(-)

diff --git a/Makefile b/Makefile @@ -4,7 +4,7 @@ BINDIR ?= /bin CFLAGS = -I include ifeq "$(LIBSLIP_DEBUG)" "1" -CFLAGS += -g -DLIBSLIP_ASSERT_ENABLE=1 +CFLAGS += -g endif all: build/libslip.so build/libslip.a diff --git a/include/slip.h b/include/slip.h @@ -4,26 +4,6 @@ #include <stdbool.h> #include <stdlib.h> -#ifdef LIBSLIP_ASSERT_ENABLE - -#include <stdio.h> - -#define LIBSLIP_ASSERT(x) libslip_assert((x), __FILE__, __LINE__, #x) - -static inline void libslip_assert(int cond, - const char *file, int line, const char *condstr) -{ - if (cond) return; - - fprintf(stderr, "libslip: Assertion failed at %s:%i (%s)\n", - file, line, condstr); - abort(); -} - -#else -#define LIBSLIP_ASSERT(x) -#endif - struct slip { int rx_state; diff --git a/src/slip.c b/src/slip.c @@ -5,6 +5,8 @@ #include <stdio.h> #include <stdint.h> +#define ASSERT(x) assert((x), __FILE__, __LINE__, #x) + enum { SLIPDEC_START_STATE, SLIPDEC_IN_FRAME_STATE, @@ -15,6 +17,16 @@ static void slip_decode_start(struct slip *slip, uint8_t rx_byte); static void slip_decode_inframe(struct slip *slip, uint8_t rx_byte); static void slip_decode_escseq(struct slip *slip, uint8_t rx_byte); +static inline void assert(int cond, + const char *file, int line, const char *condstr) +{ + if (cond) return; + + fprintf(stderr, "libslip: Assertion failed at %s:%i (%s)\n", + file, line, condstr); + abort(); +} + static inline void store(uint8_t **cur, uint8_t *end, uint8_t byte) { @@ -83,11 +95,11 @@ slip_init(struct slip *slip) slip->rx_buflen = 0; slip->rx_buf = NULL; - LIBSLIP_ASSERT(slip->rx_packet != NULL); - LIBSLIP_ASSERT(slip->realloc != NULL); + ASSERT(slip->rx_packet != NULL); + ASSERT(slip->realloc != NULL); - LIBSLIP_ASSERT(slip->esc != slip->start); - LIBSLIP_ASSERT(slip->esc != slip->end); + ASSERT(slip->esc != slip->start); + ASSERT(slip->esc != slip->end); /* build decode table */ for (i = 0; i < 256; i++) @@ -111,7 +123,7 @@ slip_decode_store(struct slip *slip, uint8_t byte) else slip->rx_buflen *= 2; slip->rx_buf = slip->realloc(slip->rx_buf, slip->rx_buflen); - LIBSLIP_ASSERT(slip->rx_buf != NULL); + ASSERT(slip->rx_buf != NULL); } slip->rx_buf[slip->rx_index++] = byte; } @@ -135,7 +147,6 @@ slip_decode(struct slip *slip, uint8_t* data, size_t size) slip_decode_escseq(slip, rx_byte); break; default: - LIBSLIP_ASSERT(false); abort(); } }