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>
This commit is contained in:
Kevin Cox 2014-07-18 11:25:53 -04:00 committed by Evan Huus
parent 4a8b6882c3
commit 01359be9e4
1 changed files with 45 additions and 0 deletions

View File

@ -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) \