aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Johnston <markj@FreeBSD.org>2024-02-03 19:16:50 +0000
committerMark Johnston <markj@FreeBSD.org>2024-02-03 19:16:50 +0000
commit9ed713d97b3d0376747c9ad1842864bebf08f823 (patch)
tree571f5fe8d242733e6e35a9becff2ce6bb74388fd
parentf73124b077d867990cbcb4d903b48be2ca55e4ca (diff)
cdefs: Introduce __result_use_or_ignore_check
Try to paper over inconsistent semantics for __warn_unused_result__ between clang and gcc. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 for a spirited discussion of these semantics. Introduce __result_use_or_ignore_check, which allows callers to explicitly ignore the return value with a cast to void. Use that to restore some checking for copyout() and friends, previously removed in commit d07acc58d898 ("systm: Relax __result_use_check annotations"). Reviewed by: olce, rpokala, kib, emaste MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D43426
-rw-r--r--sys/sys/cdefs.h11
-rw-r--r--sys/sys/systm.h17
2 files changed, 20 insertions, 8 deletions
diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h
index 206cc569c55a..e47a7072e1f1 100644
--- a/sys/sys/cdefs.h
+++ b/sys/sys/cdefs.h
@@ -326,6 +326,17 @@
#if __GNUC_PREREQ__(3, 4)
#define __fastcall __attribute__((__fastcall__))
#define __result_use_check __attribute__((__warn_unused_result__))
+#ifdef __clang__
+/*
+ * clang and gcc have different semantics for __warn_unused_result__: the latter
+ * does not permit the use of a void cast to suppress the warning. Use
+ * __result_use_or_ignore_check in places where a void cast is acceptable.
+ * This can be implemented by [[nodiscard]] from C23.
+ */
+#define __result_use_or_ignore_check __result_use_check
+#else
+#define __result_use_or_ignore_check
+#endif /* !__clang__ */
#else
#define __fastcall
#define __result_use_check
diff --git a/sys/sys/systm.h b/sys/sys/systm.h
index 29c8bfc3c768..376a35e7e8e2 100644
--- a/sys/sys/systm.h
+++ b/sys/sys/systm.h
@@ -299,10 +299,11 @@ int __result_use_check copyin(const void * __restrict udaddr,
void * _Nonnull __restrict kaddr, size_t len);
int __result_use_check copyin_nofault(const void * __restrict udaddr,
void * _Nonnull __restrict kaddr, size_t len);
-int copyout(const void * _Nonnull __restrict kaddr,
- void * __restrict udaddr, size_t len);
-int copyout_nofault(const void * _Nonnull __restrict kaddr,
+int __result_use_or_ignore_check copyout(const void * _Nonnull __restrict kaddr,
void * __restrict udaddr, size_t len);
+int __result_use_or_ignore_check copyout_nofault(
+ const void * _Nonnull __restrict kaddr, void * __restrict udaddr,
+ size_t len);
#ifdef SAN_NEEDS_INTERCEPTORS
int SAN_INTERCEPTOR(copyin)(const void *, void *, size_t);
@@ -323,11 +324,11 @@ int64_t fuword64(volatile const void *base);
int __result_use_check fueword(volatile const void *base, long *val);
int __result_use_check fueword32(volatile const void *base, int32_t *val);
int __result_use_check fueword64(volatile const void *base, int64_t *val);
-int subyte(volatile void *base, int byte);
-int suword(volatile void *base, long word);
-int suword16(volatile void *base, int word);
-int suword32(volatile void *base, int32_t word);
-int suword64(volatile void *base, int64_t word);
+int __result_use_or_ignore_check subyte(volatile void *base, int byte);
+int __result_use_or_ignore_check suword(volatile void *base, long word);
+int __result_use_or_ignore_check suword16(volatile void *base, int word);
+int __result_use_or_ignore_check suword32(volatile void *base, int32_t word);
+int __result_use_or_ignore_check suword64(volatile void *base, int64_t word);
uint32_t casuword32(volatile uint32_t *base, uint32_t oldval, uint32_t newval);
u_long casuword(volatile u_long *p, u_long oldval, u_long newval);
int casueword32(volatile uint32_t *base, uint32_t oldval, uint32_t *oldvalp,