Skip to content

Commit dd821c4

Browse files
committed
Have a #define to squelch -Wunused-result warnings.
In one place, we really *do* legitimately ignore the return value of pcap_set_datalink(). We cast its return value to void, which appears to squelch warnings from Clang, but not from at least some versions of GCC, so provide DIAG_OFF_WARN_UNUSED_RESULT/DIAG_ON_WARN_UNUSED_RESULT and use them to squelch the warning.
1 parent 6193b88 commit dd821c4

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

diag-control.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@
132132
#define DIAG_ON_CAST_QUAL \
133133
DIAG_DO_PRAGMA(GCC diagnostic pop)
134134

135+
#if ND_IS_AT_LEAST_GNUC_VERSION(4,5)
136+
/*
137+
* GCC warns about unused return values if a function is marked as
138+
* "warn about ignoring this function's return value".
139+
*
140+
* Clang appears to let you ignore a result without a warning by
141+
* casting the function result to void, so we don't appear to
142+
* need this for Clang.
143+
*/
144+
#define DIAG_OFF_WARN_UNUSED_RESULT \
145+
DIAG_DO_PRAGMA(GCC diagnostic push) \
146+
DIAG_DO_PRAGMA(GCC diagnostic ignored "-Wunused-result")
147+
#define DIAG_ON_WARN_UNUSED_RESULT \
148+
DIAG_DO_PRAGMA(GCC diagnostic pop)
149+
#endif
150+
135151
/*
136152
* Suppress deprecation warnings.
137153
*/
@@ -172,6 +188,12 @@
172188
#ifndef DIAG_ON_CAST_QUAL
173189
#define DIAG_ON_CAST_QUAL
174190
#endif
191+
#ifndef DIAG_OFF_WARN_UNUSED_RESULT
192+
#define DIAG_OFF_WARN_UNUSED_RESULT
193+
#endif
194+
#ifndef DIAG_ON_WARN_UNUSED_RESULT
195+
#define DIAG_ON_WARN_UNUSED_RESULT
196+
#endif
175197
#ifndef DIAG_OFF_DEPRECATION
176198
#define DIAG_OFF_DEPRECATION
177199
#endif

tcpdump.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2326,8 +2326,11 @@ main(int argc, char **argv)
23262326
* on; this may be a non-Linux "any" device
23272327
* that doesn't support DLT_LINUX_SLL2.
23282328
*/
2329-
if (strcmp(device, "any") == 0)
2329+
if (strcmp(device, "any") == 0) {
2330+
DIAG_OFF_WARN_UNUSED_RESULT
23302331
(void) pcap_set_datalink(pd, DLT_LINUX_SLL2);
2332+
DIAG_ON_WARN_UNUSED_RESULT
2333+
}
23312334
}
23322335
#endif
23332336
i = pcap_snapshot(pd);

0 commit comments

Comments
 (0)