summaryrefslogtreecommitdiffhomepage
path: root/test/bitbuf.test.c
diff options
context:
space:
mode:
authorMichael Smith <mikesmiffy128@gmail.com>2021-11-20 03:10:50 +0000
committerMichael Smith <mikesmiffy128@gmail.com>2021-11-20 03:18:08 +0000
commitda6f343032cb01597dc7866e66f091adf3243a62 (patch)
tree870f8cb8e82bb42202ab92bea03fc6ab35ada7ca /test/bitbuf.test.c
Initial public snapshot
With code from Bill. Thanks Bill!
Diffstat (limited to 'test/bitbuf.test.c')
-rw-r--r--test/bitbuf.test.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/test/bitbuf.test.c b/test/bitbuf.test.c
new file mode 100644
index 0000000..58d1c4d
--- /dev/null
+++ b/test/bitbuf.test.c
@@ -0,0 +1,34 @@
+/* This file is dedicated to the public domain. */
+
+{.desc = "the bit buffer implementation"};
+
+#include "../src/bitbuf.h"
+#include "../src/intdefs.h"
+
+#include <stdio.h>
+#include <string.h>
+
+static union {
+ char buf[512];
+ bitbuf_cell buf_align[512 / sizeof(bitbuf_cell)];
+} bb_buf;
+static struct bitbuf bb = {bb_buf.buf, 512, 512 * 8, 0, false, false, "test"};
+
+TEST("The possible UB in bitbuf_appendbuf shouldn't trigger horrible bugs", 0) {
+ char unalign[3] = {'X', 'X', 'X'};
+ char _buf[32 + sizeof(bitbuf_cell)];
+ char *buf = _buf;
+ if (bitbuf_align <= 1) {
+ // *shouldn't* happen
+ fputs("what's going on with the alignment???\n", stderr);
+ return false;
+ }
+ // make sure the pointer is definitely misaligned
+ while (!((usize)buf % bitbuf_align)) ++buf;
+
+ memcpy(buf, "Misaligned test buffer contents!", 32);
+ bitbuf_appendbuf(&bb, buf, 32);
+ return !memcmp(bb.buf, buf, 32);
+}
+
+// vi: sw=4 ts=4 noet tw=80 cc=80