forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
structeq() is too dangerous: if a structure has padding, it can fail silently. The new ccan/structeq instead provides a macro to define foo_eq(), which does the right thing in case of padding (which none of our structures currently have anyway). Upgrade ccan, and use it everywhere. Except run-peer-wire.c, which is only testing code and can use raw memcmp(): valgrind will tell us if padding exists. Interestingly, we still declared short_channel_id_eq, even though we didn't define it any more! Signed-off-by: Rusty Russell <[email protected]>
- Loading branch information
1 parent
4a1ca0f
commit fed5a11
Showing
44 changed files
with
172 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
CCAN imported from http://ccodearchive.net. | ||
|
||
CCAN version: init-2434-gac8694de | ||
CCAN version: init-2435-g92be2eff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
../../licenses/CC0 | ||
../../licenses/BSD-MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,45 @@ | ||
/* CC0 (Public domain) - see LICENSE file for details */ | ||
/* MIT (BSD) license - see LICENSE file for details */ | ||
#ifndef CCAN_STRUCTEQ_H | ||
#define CCAN_STRUCTEQ_H | ||
#include <ccan/build_assert/build_assert.h> | ||
#include <ccan/cppmagic/cppmagic.h> | ||
#include <string.h> | ||
#include <stdbool.h> | ||
|
||
/** | ||
* structeq - are two structures bitwise equal (including padding!) | ||
* @a: a pointer to a structure | ||
* @b: a pointer to a structure of the same type. | ||
* STRUCTEQ_DEF - define an ..._eq function to compare two structures. | ||
* @sname: name of the structure, and function (<sname>_eq) to define. | ||
* @padbytes: number of bytes of expected padding, or -1 if unknown. | ||
* @...: name of every member of the structure. | ||
* | ||
* If you *know* a structure has no padding, you can memcmp them. At | ||
* least this way, the compiler will issue a warning if the structs are | ||
* different types! | ||
* This generates a single memcmp() call in the common case where the | ||
* structure contains no padding. Since it can't tell the difference between | ||
* padding and a missing member, @padbytes can be used to assert that | ||
* there isn't any, or how many we expect. -1 means "expect some", since | ||
* it can be platform dependent. | ||
*/ | ||
#define structeq(a, b) \ | ||
(memcmp((a), (b), sizeof(*(a)) + 0 * sizeof((a) == (b))) == 0) | ||
#define STRUCTEQ_DEF(sname, padbytes, ...) \ | ||
static inline bool CPPMAGIC_GLUE2(sname, _eq)(const struct sname *_a, \ | ||
const struct sname *_b) \ | ||
{ \ | ||
BUILD_ASSERT(((padbytes) < 0 && \ | ||
CPPMAGIC_JOIN(+, CPPMAGIC_MAP(STRUCTEQ_MEMBER_SIZE_, \ | ||
__VA_ARGS__)) \ | ||
> sizeof(*_a)) \ | ||
|| CPPMAGIC_JOIN(+, CPPMAGIC_MAP(STRUCTEQ_MEMBER_SIZE_, \ | ||
__VA_ARGS__)) \ | ||
+ (padbytes) == sizeof(*_a)); \ | ||
if (CPPMAGIC_JOIN(+, CPPMAGIC_MAP(STRUCTEQ_MEMBER_SIZE_, __VA_ARGS__)) \ | ||
== sizeof(*_a)) \ | ||
return memcmp(_a, _b, sizeof(*_a)) == 0; \ | ||
else \ | ||
return CPPMAGIC_JOIN(&&, \ | ||
CPPMAGIC_MAP(STRUCTEQ_MEMBER_CMP_, \ | ||
__VA_ARGS__)); \ | ||
} | ||
|
||
/* Helpers */ | ||
#define STRUCTEQ_MEMBER_SIZE_(m) sizeof((_a)->m) | ||
#define STRUCTEQ_MEMBER_CMP_(m) memcmp(&_a->m, &_b->m, sizeof(_a->m)) == 0 | ||
|
||
#endif /* CCAN_STRUCTEQ_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.