Page MenuHomeFreeBSD

D61401.1795041100.diff
No OneTemporary

Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None

D61401.1795041100.diff

diff --git a/share/man/man9/cdefs.9 b/share/man/man9/cdefs.9
--- a/share/man/man9/cdefs.9
+++ b/share/man/man9/cdefs.9
@@ -3,7 +3,7 @@
.\"
.\" SPDX-License-Identifier: BSD-2-Clause
.\"
-.Dd August 13, 2026
+.Dd November 11, 2026
.Dt CDEFS 9
.Os
.Sh NAME
@@ -249,6 +249,24 @@
.It Sy __guarded_by(x) Ta
.It Sy __pt_guarded_by(x) Ta
.El
+.Ss Bounds Safety Macros
+Pointer annotations for Clang's
+.Fl fbounds-safety .
+Without that flag the annotations expand to nothing and the forge macros to a
+plain cast.
+.Bl -column "---------------"
+.It Sy Macro Ta Sy Description
+.It Sy __null_terminated Ta Pointer to an array that ends at its first zero
+element, such as a C string
+.It Sy __unsafe_forge_null_terminated(type,p) Ta Convert
+.Fa p
+to a
+.Sy __null_terminated
+pointer of
+.Fa type
+without a check.
+The caller guarantees that the terminator is there.
+.El
.Ss Emulated Keywords
As C evolves, many of the old macros we once used have been incorporated into
the standard language.
diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h
--- a/sys/sys/cdefs.h
+++ b/sys/sys/cdefs.h
@@ -389,9 +389,13 @@
#define __sized_by_or_null(N) __attribute__((__sized_by_or_null__(N)))
/* Pointer bounded by the sibling pointer E (one past the end). */
#define __ended_by(E) __attribute__((__ended_by__(E)))
+/* Pointer to an array ending at the first zero element. */
+#define __null_terminated __attribute__((__terminated_by__(0)))
/* Escape hatches: produce a checked pointer from an address (and size). */
#define __unsafe_forge_single(T, P) \
((T __single)__builtin_unsafe_forge_single((P)))
+#define __unsafe_forge_null_terminated(T, P) \
+ ((T __null_terminated)__builtin_unsafe_forge_terminated_by((P), 0))
/* Set the default ABI for subsequent unannotated pointers in this file. */
#define __ptrcheck_abi_assume_single() \
_Pragma("clang abi_ptr_attr set(single)")
@@ -428,7 +432,9 @@
#define __sized_by(N)
#define __sized_by_or_null(N)
#define __ended_by(E)
+#define __null_terminated
#define __unsafe_forge_single(T, P) ((T)(P))
+#define __unsafe_forge_null_terminated(T, P) ((T)(P))
#define __unsafe_forge_bidi_indexable(T, P, S) ((void)(S), (T)(P))
#define __ptrcheck_abi_assume_single()
#define __ptrcheck_abi_assume_unsafe_indexable()
diff --git a/sys/sys/hash.h b/sys/sys/hash.h
--- a/sys/sys/hash.h
+++ b/sys/sys/hash.h
@@ -43,11 +43,12 @@
* the previous hash.
*/
static __inline uint32_t
-hash32_buf(const void *buf, size_t len, uint32_t hash)
+hash32_buf(const void * __sized_by(len) buf, size_t len, uint32_t hash)
{
const unsigned char *p = buf;
+ size_t n = len;
- while (len--)
+ while (n--)
hash = HASHSTEP(hash, *p++);
return hash;
@@ -59,7 +60,8 @@
static __inline uint32_t
hash32_str(const void *buf, uint32_t hash)
{
- const unsigned char *p = buf;
+ const unsigned char * __null_terminated p =
+ __unsafe_forge_null_terminated(const unsigned char *, buf);
while (*p)
hash = HASHSTEP(hash, *p++);
@@ -73,7 +75,8 @@
static __inline uint32_t
hash32_strn(const void *buf, size_t len, uint32_t hash)
{
- const unsigned char *p = buf;
+ const unsigned char * __null_terminated p =
+ __unsafe_forge_null_terminated(const unsigned char *, buf);
while (*p && len--)
hash = HASHSTEP(hash, *p++);
@@ -89,13 +92,14 @@
static __inline uint32_t
hash32_stre(const void *buf, int end, const char **ep, uint32_t hash)
{
- const unsigned char *p = buf;
+ const unsigned char * __null_terminated p =
+ __unsafe_forge_null_terminated(const unsigned char *, buf);
while (*p && (*p != end))
hash = HASHSTEP(hash, *p++);
if (ep)
- *ep = p;
+ *ep = (const char * __null_terminated)p;
return hash;
}
@@ -109,13 +113,14 @@
hash32_strne(const void *buf, size_t len, int end, const char **ep,
uint32_t hash)
{
- const unsigned char *p = buf;
+ const unsigned char * __null_terminated p =
+ __unsafe_forge_null_terminated(const unsigned char *, buf);
while (*p && (*p != end) && len--)
hash = HASHSTEP(hash, *p++);
if (ep)
- *ep = p;
+ *ep = (const char * __null_terminated)p;
return hash;
}

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 18, 10:31 PM (4 h, 6 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41804048
Default Alt Text
D61401.1795041100.diff (4 KB)

Event Timeline