]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Fail if nd_push_buffer() or nd_push_snaplen() fails.
authorGuy Harris <[email protected]>
Fri, 1 Apr 2022 22:28:33 +0000 (15:28 -0700)
committerGuy Harris <[email protected]>
Sat, 2 Apr 2022 00:23:23 +0000 (17:23 -0700)
Always call ndo->ndo_error with a memory-allocation error if they fail.

Add WARN_UNUSED_RESULT for compilers that support it, and use it for
those routines, so that any future code that doesn't check for failure
gets a warning.

(cherry picked from commit e84f58700fe17ee5c4680247fcdcf506ff38587d)

funcattrs.h
netdissect.h
print-esp.c
print-ether.c
print-ip.c
print-ip6.c

index dfe366299feb35eb8772e71ccd682ba856cdc646..12c8cb12bcc0363b4a96b8647a5ab83ad26bca92 100644 (file)
   #define NORETURN_FUNCPTR
 #endif
 
+/*
+ * WARN_UNUSED_RESULT, before a function declaration, means "the caller
+ * should use the result of this function" (even if it's just a success/
+ * failure indication).
+ */
+#if __has_attribute(warn_unused_result) \
+    || ND_IS_AT_LEAST_GNUC_VERSION(3,4) \
+    || ND_IS_AT_LEAST_HP_C_VERSION(6,25)
+  #define WARN_UNUSED_RESULT __attribute((warn_unused_result))
+#else
+  #define WARN_UNUSED_RESULT
+#endif
+
 /*
  * PRINTFLIKE(x,y), after a function declaration, means "this function
  * does printf-style formatting, with the xth argument being the format
index 8aae13f6219418a46c163fd3d684e93db4865a83..db69d47ca1a7b14fe39543d625d6ba913f301990 100644 (file)
@@ -260,9 +260,10 @@ struct netdissect_options {
                      PRINTFLIKE_FUNCPTR(2, 3);
 };
 
-extern int nd_push_buffer(netdissect_options *, u_char *, const u_char *,
-    u_int);
-extern int nd_push_snaplen(netdissect_options *, const u_char *, u_int);
+extern WARN_UNUSED_RESULT int nd_push_buffer(netdissect_options *, u_char *,
+    const u_char *, u_int);
+extern WARN_UNUSED_RESULT int nd_push_snaplen(netdissect_options *,
+    const u_char *, u_int);
 extern void nd_change_snaplen(netdissect_options *, const u_char *, u_int);
 extern void nd_pop_packet_info(netdissect_options *);
 extern void nd_pop_all_packet_info(netdissect_options *);
index 31f93db09b34d1b05d4342c1417552a86bd1d5cb..a9ffad55717000e2633d01ea5dfefaa02e5f4003 100644 (file)
@@ -331,7 +331,8 @@ int esp_decrypt_buffer_by_ikev2_print(netdissect_options *ndo,
         */
        if (!nd_push_buffer(ndo, pt, pt, ctlen)) {
                free(pt);
-               return 0;
+               (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
+                       "%s: can't push buffer on buffer stack", __func__);
        }
 
        return 1;
@@ -907,7 +908,10 @@ esp_print(netdissect_options *ndo,
         * Don't put padding + padding length(1 byte) + next header(1 byte)
         * in the buffer because they are not part of the plaintext to decode.
         */
-       nd_push_snaplen(ndo, pt, payloadlen - (padlen + 2));
+       if (!nd_push_snaplen(ndo, pt, payloadlen - (padlen + 2))) {
+               (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
+                       "%s: can't push snaplen on buffer stack", __func__);
+       }
 
        /* Now dissect the plaintext. */
        ip_demux_print(ndo, pt, payloadlen - (padlen + 2), ver, fragmented,
index 2f2b150316a67f8e442857bb9e5d768f114f3a47..cd623f9025687a95bb7e166bdf98e516cb0ea30d 100644 (file)
@@ -306,7 +306,10 @@ recurse:
                 * Cut off the snapshot length to the end of the
                 * payload.
                 */
-               nd_push_snaplen(ndo, p, length);
+               if (!nd_push_snaplen(ndo, p, length)) {
+                       (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
+                               "%s: can't push snaplen on buffer stack", __func__);
+               }
 
                if (ndo->ndo_eflag) {
                        ND_PRINT("802.3");
index 4f9617a3c6a8b205225ffffda44131c5c3afef04..23ba99c970075099b9e5a80a598967e2c4081b6e 100644 (file)
@@ -377,7 +377,10 @@ ip_print(netdissect_options *ndo,
        /*
         * Cut off the snapshot length to the end of the IP payload.
         */
-       nd_push_snaplen(ndo, bp, len);
+       if (!nd_push_snaplen(ndo, bp, len)) {
+               (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
+                       "%s: can't push snaplen on buffer stack", __func__);
+       }
 
        len -= hlen;
 
index 15b30630fb2418330246f3c6770439b540cce168..829483345925c6d20c8b24ff789048aaa0b66873 100644 (file)
@@ -305,7 +305,10 @@ ip6_print(netdissect_options *ndo, const u_char *bp, u_int length)
        /*
         * Cut off the snapshot length to the end of the IP payload.
         */
-       nd_push_snaplen(ndo, bp, len);
+       if (!nd_push_snaplen(ndo, bp, len)) {
+               (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
+                       "%s: can't push snaplen on buffer stack", __func__);
+       }
 
        cp = (const u_char *)ip6;
        advance = sizeof(struct ip6_hdr);