diff options
author | Kevin Cox <kevincox@kevincox.ca> | 2014-07-18 11:25:53 -0400 |
---|---|---|
committer | Evan Huus <eapache@gmail.com> | 2014-07-18 20:05:27 +0000 |
commit | 01359be9e40ab144d45d1b6e8bdae41beddca288 (patch) | |
tree | f013cbe48e1e74d07f1c99573e8b03156781b252 /epan | |
parent | 4a8b6882c3ade328a3b2e88ef8ae43bd735b23f6 (diff) |
Add better assertions for comparing integers.
Adds DISSECTOR_ASSERT_CMPINT() family of assertions which show the value
of each side of the comparison upon failure.
Change-Id: Ie6f2ba46d62fc864c08eb432c403fab8f1932d91
Reviewed-on: https://code.wireshark.org/review/3103
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Reviewed-by: Evan Huus <eapache@gmail.com>
Diffstat (limited to 'epan')
-rw-r--r-- | epan/proto.h | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/epan/proto.h b/epan/proto.h index 79e1420a99..a525cca9f5 100644 --- a/epan/proto.h +++ b/epan/proto.h @@ -174,6 +174,51 @@ WS_DLL_PUBLIC WS_MSVC_NORETURN void proto_report_dissector_bug(const char *messa ep_strdup_printf("%s:%u: failed assertion \"DISSECTOR_ASSERT_NOT_REACHED\"", \ __FILE__, __LINE__))) +/** Compare two integers. + * + * This is functionally the same as `DISSECTOR_ASSERT(a op b)` except that it + * will display the values of a and b upon failure. + * + * DISSECTOR_ASSERT_CMPINT(a, ==, b); + * DISSECTOR_ASSERT_CMPINT(min, <=, max); + * + * This function can currently compare values that fit inside a gint64. + * + * WARNING: The number of times the arguments are evaluated is undefined. Do + * not use expressions with side effects as arguments. + * + * @param a The first integer. + * @param op Any binary operator. + * @param b The second integer. + */ +#define DISSECTOR_ASSERT_CMPINT(a, op, b) \ + ((void) ((a op b) ? (void)0 : \ + __DISSECTOR_ASSERT_CMPINT (a, op, b, gint64, "%"G_GINT64_MODIFIER"d"))) \ + __DISSECTOR_ASSERT_STATIC_ANALYSIS_HINT(expression) + +/** Like DISSECTOR_ASSERT_CMPINT() except the arguments are treated as + * unsigned values. + * + * This function can currently compare values that fit inside a guint64. + */ +#define DISSECTOR_ASSERT_CMPUINT(a, op, b) \ + ((void) ((a op b) ? (void)0 : \ + __DISSECTOR_ASSERT_CMPINT (a, op, b, guint64, "%"G_GINT64_MODIFIER"u"))) \ + __DISSECTOR_ASSERT_STATIC_ANALYSIS_HINT(expression) + +/** Like DISSECTOR_ASSERT_CMPUINT() except the values are displayed in + * hexadecimal upon assertion failure. + */ +#define DISSECTOR_ASSERT_CMPUINTHEX(a, op, b) \ + ((void) ((a op b) ? (void)0 : \ + __DISSECTOR_ASSERT_CMPINT (a, op, b, guint64, "0x%"G_GINT64_MODIFIER"X"))) \ + __DISSECTOR_ASSERT_STATIC_ANALYSIS_HINT(expression) + +#define __DISSECTOR_ASSERT_CMPINT(a, op, b, type, fmt) \ + (REPORT_DISSECTOR_BUG( \ + ep_strdup_printf("%s:%u: failed assertion "#a" "#op" "#b" ("fmt" "#op" "fmt")", \ + __FILE__, __LINE__, (type)a, (type)b))) + #define __DISSECTOR_ASSERT_STRINGIFY(s) # s #define __DISSECTOR_ASSERT(expression, file, lineno) \ |