ccan/structeq: rewrite for safety.
From 92be2eff52133138e4975308f6e731c46b53ace1 Mon Sep 17 00:00:00 2001
From: Rusty Russell <[email protected]>
Date: Wed, 4 Jul 2018 13:37:28 +0930
Subject: [PATCH] ccan/structeq: make it safe when there's padding.
ccan/cppmagic FTW!
The only issue is that we can't tell if there's padding or they've missed a member, so we add a padding bytes count, so they'll get an error if it (for example) the structure adds a new member later.
Signed-off-by Rusty Russell <[email protected]>
--- ccan/crypto/shachain/shachain.h | 4 +- ccan/structeq/LICENSE | 2 +- ccan/structeq/_info | 20 ++++---- ccan/structeq/structeq.h | 46 +++++++++++++++---- ccan/structeq/test/compile_fail-different.c | 4 +- .../test/compile_fail-expect-any-padding.c | 19 ++++++++ .../test/compile_fail-expect-padding.c | 19 ++++++++ .../test/compile_fail-unexpceted-padding.c | 20 ++++++++ ccan/structeq/test/run-with-padding.c | 32 +++++++++++++ ccan/structeq/test/run.c | 8 ++-- 10 files changed, 149 insertions(+), 25 deletions(-) create mode 100644 ccan/structeq/test/compile_fail-expect-any-padding.c create mode 100644 ccan/structeq/test/compile_fail-expect-padding.c create mode 100644 ccan/structeq/test/compile_fail-unexpceted-padding.c create mode 100644 ccan/structeq/test/run-with-padding.c
ccan/crypto/shachain/shachain.h +3 -1
@@ -116,6 +116,8 @@ bool shachain_add_hash(struct shachain *chain,* * Example: * #include <ccan/structeq/structeq.h> +* // Defines sha256_eq +* STRUCTEQ_DEF(sha256, 0, u); * * static void next_hash(const struct sha256 *hash) * {
@@ -127,7 +129,7 @@ bool shachain_add_hash(struct shachain *chain,* else { * struct sha256 check; * assert(shachain_get_hash(&chain, index+1, &check)); -* assert(structeq(&check, hash)); +* assert(sha256_eq(&check, hash)); * } * } */
ccan/structeq/LICENSE +1 -1
@@ -1 +1 @@ -../../licenses/CC0 +../../licenses/BSD-MIT
ccan/structeq/_info +10 -10
@@ -6,9 +6,11 @@ * structeq - bitwise comparison of structs. * * This is a replacement for memcmp, which checks the argument types are the - * same. + * same, and takes into account padding in the structure. When there is no + * padding, it becomes a memcmp at compile time (assuming a + * constant-optimizing compiler). * - * License: CC0 (Public domain) + * License: BSD-MIT * Author: Rusty Russell <[email protected]> * * Example:
@@ -19,26 +21,22 @@ * struct mydata { * int start, end; * }; + * // Defines mydata_eq(a, b) + * STRUCTEQ_DEF(mydata, 0, start, end); * * int main(void) * { * struct mydata a, b; * - * // No padding in struct, otherwise this doesn't work! - * BUILD_ASSERT(sizeof(a) == sizeof(a.start) + sizeof(a.end)); - * * a.start = 100; * a.end = 101; * - * b.start = 100; - * b.end = 101; - * * // They are equal. - * assert(structeq(&a, &b)); + * assert(mydata_eq(&a, &b)); * * b.end++; * // Now they are not. - * assert(!structeq(&a, &b)); + * assert(!mydata_eq(&a, &b)); * * return 0; * }
@@ -50,6 +48,8 @@ int main(int argc, char *argv[]) return 1; if (strcmp(argv[1], "depends") == 0) { + printf("ccan/build_assert\n"); + printf("ccan/cppmagic\n"); return 0; }
ccan/structeq/structeq.h +37 -9
54 lines not on this page · raw message
ccan/structeq/test/compile_fail-different.c +3 -1
@@ -8,6 +8,8 @@ struct mydata2 {int start, end; }; +STRUCTEQ_DEF (mydata1,0 , start, end); +int main (void ) {struct mydata1 a = {0 ,100 };
@@ -18,5 +20,5 @@ int main(void)#endif b = { 0, 100 }; -return structeq (&a, &b); +return mydata1_eq (&a, &b); }
ccan/structeq/test/compile_fail-expect-any-padding.c new file +19 -0
@@ -0,0 +1,19 @@ +#include <ccan/structeq/structeq.h> + +struct mydata { +int start, end; +}; +#ifdef FAIL +#define PADDING -1 +#else +#define PADDING 0 +#endif + +STRUCTEQ_DEF (mydata,PADDING , start, end); + +int main (void ) +{ +struct mydata a = {0 ,100 }; + +return mydata_eq (&a, &a); +}
ccan/structeq/test/compile_fail-expect-padding.c new file +19 -0
@@ -0,0 +1,19 @@ +#include <ccan/structeq/structeq.h> + +struct mydata { +int start, end; +}; +#ifdef FAIL +#define PADDING 1 +#else +#define PADDING 0 +#endif + +STRUCTEQ_DEF (mydata,PADDING , start, end); + +int main (void ) +{ +struct mydata a = {0 ,100 }; + +return mydata_eq (&a, &a); +}
ccan/structeq/test/compile_fail-unexpceted-padding.c new file +20 -0
@@ -0,0 +1,20 @@ +#include <ccan/structeq/structeq.h> + +struct mydata { +int start, end; +int pad; +}; +#ifdef FAIL +#define PADDING 0 +#else +#define PADDING sizeof(int) +#endif + +STRUCTEQ_DEF (mydata,PADDING , start, end); + +int main (void ) +{ +struct mydata a = {0 ,100 }; + +return mydata_eq (&a, &a); +}
ccan/structeq/test/run-with-padding.c new file +32 -0
@@ -0,0 +1,32 @@ +#include <ccan/structeq/structeq.h> +#include <ccan/tap/tap.h> + +/* In theory, this could be generated without padding, if alignof(int) were 0, +* and test would fail. Call me when that happens. */ +struct mydata { +char start; +int end; +}; + +STRUCTEQ_DEF (mydata,sizeof (int ) -sizeof (char ), start, end); + +int main (void ) +{ +struct mydata a, b; + +/* This is how many tests you plan to run */ +plan_tests (3 ); + + a.start =0 ; + a.end =100 ; +ok1 (mydata_eq (&a, &a)); + + b = a; +ok1 (mydata_eq (&a, &b)); + + b.end++; +ok1 (!mydata_eq (&a, &b)); + +/* This exits depending on whether all tests passed */ +return exit_status (); +}
ccan/structeq/test/run.c +5 -3
@@ -5,6 +5,8 @@ struct mydata {int start, end; }; +STRUCTEQ_DEF (mydata,0 , start, end); +int main (void ) {struct mydata a, b;
@@ -14,13 +16,13 @@ int main(void) a.start =0 ; a.end =100 ; -ok1 (structeq (&a, &a)); +ok1 (mydata_eq (&a, &a)); b = a; -ok1 (structeq (&a, &b)); +ok1 (mydata_eq (&a, &b)); b.end++; -ok1 (!structeq (&a, &b)); +ok1 (!mydata_eq (&a, &b));/* This exits depending on whether all tests passed */ return exit_status ();
Signature · 1 line
2.17.1