]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Suppress "casting away const" warnings in missing/getopt_long.c.
authorGuy Harris <[email protected]>
Sat, 24 Jul 2021 08:51:02 +0000 (01:51 -0700)
committerGuy Harris <[email protected]>
Sat, 24 Jul 2021 08:51:02 +0000 (01:51 -0700)
It's explicitly cheating in some places; just cast away the warnings.

Add a Clang version testing macro to compiler-tests.h.

Move the diagnostic control #defines from netdissect-stdinc.h to
diag-control.h, libpcap-style, so that they can be used by code that
doesn't use (or need) netdissect-stdinc.h.  This also means that we can
limit the inclusion of diag-control.h, and the definition of those

Do them more libpcap-style, with separate DIAG_OFF/DIAG_ON pairs for
particular issues, rather than having DIAG_OFF() and DIAG_ON() macros
that take -W option names as arguments; that way, if we need to define
them for compilers that don't have the GCC/Clang syntax for those
pragmas, e.g. MSVC, we can do so.

Makefile.in
compiler-tests.h
diag-control.h [new file with mode: 0644]
missing/getopt_long.c
netdissect-stdinc.h
print-esp.c
print-rx.c
print-tcp.c
signature.c
tcpdump.c

index f6b287bda4c01d707eb8786c9629d6564eaa1f68..b5c78ee2eb4dcd1fc1443b8dc4409da964dcd450 100644 (file)
@@ -275,6 +275,7 @@ HDR = \
        chdlc.h \
        compiler-tests.h \
        cpack.h \
+       diag-control.h \
        ethertype.h \
        extract.h \
        fptype.h \
index 4793b7199740917999e8990f137f112495294a10..8b1a2331ed7a88ac0dfc272ce20c63ff3baff690 100644 (file)
         (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
 #endif
 
+/*
+ * Check whether this is Clang major.minor or a later release.
+ */
+
+#if !defined(__clang__)
+#define ND_IS_AT_LEAST_CLANG_VERSION(major, minor) 0
+#else
+#define ND_IS_AT_LEAST_CLANG_VERSION(major, minor) \
+       (__clang_major__ > (major) || \
+        (__clang_major__ == (major) && __clang_minor__ >= (minor)))
+#endif
+
 /*
  * Check whether this is Sun C/SunPro C/Oracle Studio major.minor
  * or a later release.
diff --git a/diag-control.h b/diag-control.h
new file mode 100644 (file)
index 0000000..8c9ca72
--- /dev/null
@@ -0,0 +1,128 @@
+/* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
+/*
+ * Copyright (c) 1993, 1994, 1995, 1996, 1997
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the Computer Systems
+ *     Engineering Group at Lawrence Berkeley Laboratory.
+ * 4. Neither the name of the University nor of the Laboratory may be used
+ *    to endorse or promote products derived from this software without
+ *    specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _diag_control_h
+#define _diag_control_h
+
+#include "compiler-tests.h"
+
+#ifndef _MSC_VER
+  /*
+   * Clang and GCC both support this way of putting pragmas into #defines.
+   * We don't use it unless we have a compiler that supports it; the
+   * warning-suppressing pragmas differ between Clang and GCC, so we test
+   * for both of those separately.
+   */
+  #define DIAG_DO_PRAGMA(x) _Pragma (#x)
+#endif
+
+/*
+ * The current clang compilers also define __GNUC__ and __GNUC_MINOR__
+ * thus we need to test the clang case before the GCC one
+ */
+#if ND_IS_AT_LEAST_CLANG_VERSION(2,8)
+  /*
+   * Clang complains if you OR together multiple enum values of a
+   * given enum type and them pass it as an argument of that enum
+   * type.  Some libcap-ng routines use enums to define bit flags;
+   * we want to squelch the warnings that produces.
+   */
+  #define DIAG_OFF_ASSIGN_ENUM \
+    DIAG_DO_PRAGMA(clang diagnostic push) \
+    DIAG_DO_PRAGMA(clang diagnostic ignored "-Wassign-enum")
+  #define DIAG_ON_ASSIGN_ENUM \
+    DIAG_DO_PRAGMA(clang diagnostic pop)
+
+  /*
+   * It also legitimately complains about some code in the BSD
+   * getopt_long() - that code explicitly and deliberately
+   * violates the contract by permuting the argument vector
+   * (declared as char const *argv[], meaning "I won't change
+   * the vector by changing any of its elements), as do the
+   * GNU and Solaris getopt_long().  This is documented in the
+   * man pages for all versions; it can be suppressed by setting
+   * the environment variable POSIXLY_CORRECT or by putting a "+"
+   * at the beginning of the option string.
+   *
+   * We suppress the warning.
+   */
+  #define DIAG_OFF_CAST_QUAL \
+    DIAG_DO_PRAGMA(clang diagnostic push) \
+    DIAG_DO_PRAGMA(clang diagnostic ignored "-Wcast-qual")
+  #define DIAG_ON_CAST_QUAL \
+    DIAG_DO_PRAGMA(clang diagnostic pop)
+
+  /*
+   * Suppress deprecation warnings.
+   */
+  #define DIAG_OFF_DEPRECATION \
+    DIAG_DO_PRAGMA(clang diagnostic push) \
+    DIAG_DO_PRAGMA(clang diagnostic ignored "-Wdeprecated-declarations")
+  #define DIAG_ON_DEPRECATION \
+    DIAG_DO_PRAGMA(clang diagnostic pop)
+  #define DIAG_OFF_FORMAT_TRUNCATION
+  #define DIAG_ON_FORMAT_TRUNCATION
+#elif ND_IS_AT_LEAST_GNUC_VERSION(4,2)
+  /* GCC apparently doesn't complain about ORing enums together. */
+  #define DIAG_OFF_ASSIGN_ENUM
+  #define DIAG_ON_ASSIGN_ENUM
+
+  /*
+   * It does, however, complain about casting away constness in
+   * missing/getopt_long.c.
+   */
+  #define DIAG_OFF_CAST_QUAL \
+    DIAG_DO_PRAGMA(GCC diagnostic push) \
+    DIAG_DO_PRAGMA(GCC diagnostic ignored "-Wcast-qual")
+  #define DIAG_ON_CAST_QUAL \
+    DIAG_DO_PRAGMA(GCC diagnostic pop)
+
+  /*
+   * Suppress deprecation warnings.
+   */
+  #define DIAG_OFF_DEPRECATION \
+    DIAG_DO_PRAGMA(GCC diagnostic push) \
+    DIAG_DO_PRAGMA(GCC diagnostic ignored "-Wdeprecated-declarations")
+  #define DIAG_ON_DEPRECATION \
+    DIAG_DO_PRAGMA(GCC diagnostic pop)
+#else
+  #define DIAG_OFF_ASSIGN_ENUM
+  #define DIAG_ON_ASSIGN_ENUM
+  #define DIAG_OFF_CAST_QUAL
+  #define DIAG_ON_CAST_QUAL
+  #define DIAG_OFF_DEPRECATION
+  #define DIAG_ON_DEPRECATION
+#endif
+
+#endif /* _diag_control_h */
index 80857369804dbe21aa4c27ad50db68f743d8656e..5018400ed0e41d5c09b42323f8958b08ba56d0de 100644 (file)
@@ -57,6 +57,8 @@
 #include <string.h>
 #include <stdarg.h>
 
+#include "diag-control.h"
+
 #define GNU_COMPATIBLE         /* Be more compatible, configure's use us! */
 
 #define PRINT_ERROR    ((opterr) && (*options != ':'))
@@ -158,11 +160,29 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end,
                                pos -= nnonopts;
                        else
                                pos += nopts;
+                       /*
+                        * This is annoying - I guess the
+                        * "char * const argv[]" in the declaration
+                        * of getopt() - and thus getopt_long() -
+                        * means that it makes a promise not to
+                        * shuffle the arguments, but here we are,
+                        * shuffling the arguments.
+                        *
+                        * (No, it's not a promise that it won't
+                        * modify any of the argument strings.
+                        * It's a promise that it won't modify
+                        * the array of pointers to the argument
+                        * strings.)
+                        *
+                        * So squelch the cast warnings.
+                        */
                        swap = nargv[pos];
+DIAG_OFF_CAST_QUAL
                        /* LINTED const cast */
                        ((char **) nargv)[pos] = nargv[cstart];
                        /* LINTED const cast */
                        ((char **)nargv)[cstart] = swap;
+DIAG_ON_CAST_QUAL
                }
        }
 }
@@ -291,6 +311,7 @@ parse_long_options(char * const *nargv, const char *options,
                }
                if (long_options[match].has_arg == required_argument ||
                    long_options[match].has_arg == optional_argument) {
+DIAG_OFF_CAST_QUAL
                        if (has_equal)
                                optarg = (char *)has_equal;
                        else if (long_options[match].has_arg ==
@@ -300,6 +321,7 @@ parse_long_options(char * const *nargv, const char *options,
                                 */
                                optarg = nargv[optind++];
                        }
+DIAG_ON_CAST_QUAL
                }
                if ((long_options[match].has_arg == required_argument)
                    && (optarg == NULL)) {
@@ -543,8 +565,10 @@ start:
                        ++optind;
        } else {                                /* takes (optional) argument */
                optarg = NULL;
+DIAG_OFF_CAST_QUAL
                if (*place)                     /* no white space */
                        optarg = (char *)place;
+DIAG_ON_CAST_QUAL
                else if (oli[1] != ':') {       /* arg not optional */
                        if (++optind >= nargc) {        /* no arg */
                                place = EMSG;
index 0523620f3908ceae14154bfa198afd0f73284f9b..cd273371483282e80fbe8a2390da69d2871aec5a 100644 (file)
@@ -357,70 +357,6 @@ struct in6_addr {
 #define FALSE 0
 #endif
 
-/*
- * The Apple deprecation workaround macros below were adopted from the
- * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
- */
-
-#define XSTRINGIFY(x) #x
-
-/*
- *     Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
- */
-#define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
-#define DIAG_DO_PRAGMA(x) _Pragma (#x)
-
-/*
- * The current clang compilers also define __GNUC__ and __GNUC_MINOR__
- * thus we need to test the clang case before the GCC one
- */
-#if defined(__clang__)
-#  if (__clang_major__ * 100) + __clang_minor__ >= 208
-#    define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
-#    define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
-#    define DIAG_ON(x) DIAG_PRAGMA(pop)
-#  else
-#    define DIAG_OFF(x)
-#    define DIAG_ON(x)
-#  endif
-#elif defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
-#  define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
-#  if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
-#    define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
-#    define DIAG_ON(x) DIAG_PRAGMA(pop)
-#  else
-#    define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
-#    define DIAG_ON(x)  DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
-#  endif
-#else
-#  define DIAG_OFF(x)
-#  define DIAG_ON(x)
-#endif
-
-/* Use for clang specific warnings */
-#ifdef __clang__
-#  define DIAG_OFF_CLANG(x) DIAG_OFF(x)
-#  define DIAG_ON_CLANG(x)  DIAG_ON(x)
-#else
-#  define DIAG_OFF_CLANG(x)
-#  define DIAG_ON_CLANG(x)
-#endif
-
-/*
- *     For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
- */
-#ifdef __APPLE__
-#  define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
-#  define USES_APPLE_RST DIAG_ON(deprecated-declarations)
-#else
-#  define USES_APPLE_DEPRECATED_API
-#  define USES_APPLE_RST
-#endif
-
-/*
- * end of Apple deprecation workaround macros
- */
-
 /*
  * Statement attributes, for various compilers.
  *
index 61c3e13bffdae2da778ca29f5732f5929a22aaff..d1ea1005d5d47145a89d3125409217f962ff88e1 100644 (file)
@@ -47,6 +47,8 @@
 #include "netdissect.h"
 #include "extract.h"
 
+#include "diag-control.h"
+
 #ifdef HAVE_LIBCRYPTO
 #include "strtoaddr.h"
 #include "ascii_strcasecmp.h"
@@ -278,7 +280,7 @@ do_decrypt(netdissect_options *ndo, const char *caller, struct sa_list *sa,
  * dissecting anything in it and before it does any dissection of
  * anything in the old buffer.  That will free the new buffer.
  */
-USES_APPLE_DEPRECATED_API
+DIAG_OFF_DEPRECATION
 int esp_decrypt_buffer_by_ikev2_print(netdissect_options *ndo,
                                      int initiator,
                                      const u_char spii[8],
@@ -334,7 +336,7 @@ int esp_decrypt_buffer_by_ikev2_print(netdissect_options *ndo,
 
        return 1;
 }
-USES_APPLE_RST
+DIAG_ON_DEPRECATION
 
 static void esp_print_addsa(netdissect_options *ndo,
                            struct sa_list *sa, int sa_def)
@@ -413,7 +415,7 @@ int espprint_decode_hex(netdissect_options *ndo,
  * decode the form:    SPINUM@IP <tab> ALGONAME:0xsecret
  */
 
-USES_APPLE_DEPRECATED_API
+DIAG_OFF_DEPRECATION
 static int
 espprint_decode_encalgo(netdissect_options *ndo,
                        char *decode, struct sa_list *sa)
@@ -478,7 +480,7 @@ espprint_decode_encalgo(netdissect_options *ndo,
 
        return 1;
 }
-USES_APPLE_RST
+DIAG_ON_DEPRECATION
 
 /*
  * for the moment, ignore the auth algorithm, just hard code the authenticator
@@ -670,7 +672,7 @@ static void esp_print_decode_onesecret(netdissect_options *ndo, char *line,
        esp_print_addsa(ndo, &sa1, sa_def);
 }
 
-USES_APPLE_DEPRECATED_API
+DIAG_OFF_DEPRECATION
 static void esp_init(netdissect_options *ndo _U_)
 {
        /*
@@ -683,7 +685,7 @@ static void esp_init(netdissect_options *ndo _U_)
 #endif
        EVP_add_cipher_alias(SN_des_ede3_cbc, "3des");
 }
-USES_APPLE_RST
+DIAG_ON_DEPRECATION
 
 void esp_decodesecret_print(netdissect_options *ndo)
 {
@@ -720,7 +722,7 @@ void esp_decodesecret_print(netdissect_options *ndo)
 #endif
 
 #ifdef HAVE_LIBCRYPTO
-USES_APPLE_DEPRECATED_API
+DIAG_OFF_DEPRECATION
 #endif
 void
 esp_print(netdissect_options *ndo,
@@ -920,5 +922,5 @@ esp_print(netdissect_options *ndo,
 #endif
 }
 #ifdef HAVE_LIBCRYPTO
-USES_APPLE_RST
+DIAG_ON_DEPRECATION
 #endif
index b8ee5a839b79e4d77791cda9918c88dc0fe6154c..a9eccb914eb58b417d8aec5ed39d31f4902241e0 100644 (file)
@@ -1140,6 +1140,7 @@ trunc:
  * representing a logical OR of all the ACL permission bits
  */
 
+#define XSTRINGIFY(x) #x
 #define NUMSTRINGIFY(x)        XSTRINGIFY(x)
 
 static void
index 142a2ba9ff31cffa5938034f6fe1bcb23f3a747f..0c250a07491d86fa62042a9ef49b480ab3c6b323 100644 (file)
@@ -44,6 +44,8 @@ __RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
 #include "addrtoname.h"
 #include "extract.h"
 
+#include "diag-control.h"
+
 #include "tcp.h"
 
 #include "ip.h"
@@ -883,7 +885,7 @@ print_tcp_fastopen_option(netdissect_options *ndo, const u_char *cp,
 }
 
 #ifdef HAVE_LIBCRYPTO
-USES_APPLE_DEPRECATED_API
+DIAG_OFF_DEPRECATION
 static int
 tcp_verify_signature(netdissect_options *ndo,
                      const struct ip *ip, const struct tcphdr *tp,
@@ -963,5 +965,5 @@ tcp_verify_signature(netdissect_options *ndo,
         else
                 return (SIGNATURE_INVALID);
 }
-USES_APPLE_RST
+DIAG_ON_DEPRECATION
 #endif /* HAVE_LIBCRYPTO */
index ca3aec9f45976100ee89a4eefac970d1831172e9..77d6e81ac3ab2043d1d801ed031dbbcda45dd5fc 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "netdissect.h"
 #include "signature.h"
+#include "diag-control.h"
 
 #ifdef HAVE_LIBCRYPTO
 #include <openssl/md5.h>
@@ -45,7 +46,7 @@ const struct tok signature_check_values[] = {
  * Compute a HMAC MD5 sum.
  * Taken from rfc2104, Appendix.
  */
-USES_APPLE_DEPRECATED_API
+DIAG_OFF_DEPRECATION
 static void
 signature_compute_hmac_md5(const uint8_t *text, int text_len, unsigned char *key,
                            unsigned int key_len, uint8_t *digest)
@@ -108,7 +109,7 @@ signature_compute_hmac_md5(const uint8_t *text, int text_len, unsigned char *key
     MD5_Update(&context, digest, 16);     /* then results of 1st hash */
     MD5_Final(digest, &context);          /* finish up 2nd pass */
 }
-USES_APPLE_RST
+DIAG_ON_DEPRECATION
 
 /*
  * Verify a cryptographic signature of the packet.
index 95162ecc18165f9a4518354684698e783b94ebfd..1e105eba60570d45874deb2b7a5308481dd9dca3 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -161,6 +161,8 @@ The Regents of the University of California.  All rights reserved.\n";
 
 #include "print.h"
 
+#include "diag-control.h"
+
 #include "fptype.h"
 
 #ifndef PATH_MAX
@@ -795,7 +797,7 @@ droproot(const char *username, const char *chroot_dir)
                error("Couldn't find user '%.32s'", username);
 #ifdef HAVE_LIBCAP_NG
        /* We don't need CAP_SETUID, CAP_SETGID and CAP_SYS_CHROOT any more. */
-DIAG_OFF_CLANG(assign-enum)
+DIAG_OFF_ASSIGN_ENUM
        capng_updatev(
                CAPNG_DROP,
                CAPNG_EFFECTIVE | CAPNG_PERMITTED,
@@ -803,7 +805,7 @@ DIAG_OFF_CLANG(assign-enum)
                CAP_SETGID,
                CAP_SYS_CHROOT,
                -1);
-DIAG_ON_CLANG(assign-enum)
+DIAG_ON_ASSIGN_ENUM
        capng_apply(CAPNG_SELECT_BOTH);
 #endif /* HAVE_LIBCAP_NG */
 
@@ -2337,33 +2339,33 @@ main(int argc, char **argv)
                /* Initialize capng */
                capng_clear(CAPNG_SELECT_BOTH);
                if (username) {
-DIAG_OFF_CLANG(assign-enum)
+DIAG_OFF_ASSIGN_ENUM
                        capng_updatev(
                                CAPNG_ADD,
                                CAPNG_PERMITTED | CAPNG_EFFECTIVE,
                                CAP_SETUID,
                                CAP_SETGID,
                                -1);
-DIAG_ON_CLANG(assign-enum)
+DIAG_ON_ASSIGN_ENUM
                }
                if (chroot_dir) {
-DIAG_OFF_CLANG(assign-enum)
+DIAG_OFF_ASSIGN_ENUM
                        capng_update(
                                CAPNG_ADD,
                                CAPNG_PERMITTED | CAPNG_EFFECTIVE,
                                CAP_SYS_CHROOT
                                );
-DIAG_ON_CLANG(assign-enum)
+DIAG_ON_ASSIGN_ENUM
                }
 
                if (WFileName) {
-DIAG_OFF_CLANG(assign-enum)
+DIAG_OFF_ASSIGN_ENUM
                        capng_update(
                                CAPNG_ADD,
                                CAPNG_PERMITTED | CAPNG_EFFECTIVE,
                                CAP_DAC_OVERRIDE
                                );
-DIAG_ON_CLANG(assign-enum)
+DIAG_ON_ASSIGN_ENUM
                }
                capng_apply(CAPNG_SELECT_BOTH);
 #endif /* HAVE_LIBCAP_NG */
@@ -3174,7 +3176,7 @@ static void verbose_stats_dump(int sig _U_)
 }
 #endif /* _WIN32 */
 
-USES_APPLE_DEPRECATED_API
+DIAG_OFF_DEPRECATION
 static void
 print_version(FILE *f)
 {
@@ -3212,7 +3214,7 @@ print_version(FILE *f)
 #  endif
 #endif /* __SANITIZE_ADDRESS__ or __has_feature */
 }
-USES_APPLE_RST
+DIAG_ON_DEPRECATION
 
 static void
 print_usage(FILE *f)