]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Add ND_LCHECKMSG_U, ND_LCHECK_U, ND_LCHECKMSG_ZU and ND_LCHECK_ZU macros
authorFrancois-Xavier Le Bail <[email protected]>
Sat, 16 Jan 2021 13:27:21 +0000 (14:27 +0100)
committerFrancois-Xavier Le Bail <[email protected]>
Sat, 16 Jan 2021 13:33:10 +0000 (14:33 +0100)
They check length < minimum for invalid packet with or without a custom
message, format %u or %zu.

%zu (ND_LCHECKMSG_ZU and ND_LCHECK_ZU) is useful when minimum is a
sizeof(...).

[skip ci]

netdissect.h

index 96b6c0e74657e8b38d7c638f49c7209ae2b35738..9541740b8e39c7066f9eedef83adc4cbde709887 100644 (file)
@@ -382,6 +382,28 @@ extern void nd_pop_all_packet_info(netdissect_options *);
  */
 #define ND_BYTES_AVAILABLE_AFTER(p) ND_BYTES_BETWEEN(ndo->ndo_snapend, (p))
 
+/* Check length < minimum for invalid packet with a custom message, format %u */
+#define ND_LCHECKMSG_U(length, minimum, what) \
+if ((length) < (minimum)) { \
+ND_PRINT(" [%s %u < %u]", (what), (length), (minimum)); \
+goto invalid; \
+}
+
+/* Check length < minimum for invalid packet with #length message, format %u */
+#define ND_LCHECK_U(length, minimum) \
+ND_LCHECKMSG_U((length), (minimum), (#length))
+
+/* Check length < minimum for invalid packet with a custom message, format %zu */
+#define ND_LCHECKMSG_ZU(length, minimum, what) \
+if ((length) < (minimum)) { \
+ND_PRINT(" [%s %u < %zu]", (what), (length), (minimum)); \
+goto invalid; \
+}
+
+/* Check length < minimum for invalid packet with #length message, format %zu */
+#define ND_LCHECK_ZU(length, minimum) \
+ND_LCHECKMSG_U((length), (minimum), (#length))
+
 #define ND_PRINT(...) (ndo->ndo_printf)(ndo, __VA_ARGS__)
 #define ND_DEFAULTPRINT(ap, length) (*ndo->ndo_default_print)(ndo, ap, length)