[Bridge] [PATCH 04/26] net: add a new sockptr_t type

Jan Engelhardt jengelh at inai.de
Thu Jul 23 15:40:49 UTC 2020

On Thursday 2020-07-23 08:08, Christoph Hellwig wrote:
>+typedef struct {
>+	union {
>+		void		*kernel;
>+		void __user	*user;
>+	};
>+	bool		is_kernel : 1;
>+} sockptr_t;
>+static inline bool sockptr_is_null(sockptr_t sockptr)
>+	return !sockptr.user && !sockptr.kernel;

"""If the member used to access the contents of a union is not the same as the
member last used to store a value, the object representation of the value that
was stored is reinterpreted as an object representation of the new type (this
is known as type punning). If the size of the new type is larger than the size
of the last-written type, the contents of the excess bytes are unspecified (and
may be a trap representation)"""

As I am not too versed with the consequences of trap representations, I will
just point out that a future revision of the C standard may introduce (proposal
N2362) stronger C++-like requirements; as for union, that would imply a simple:

"""It's undefined behavior to read from the member of the union that wasn't
most recently written.""" [cppreference.com]

So, in the spirit of copy_from/to_sockptr, the is_null function should read

	return sockptr.is_kernel ? !sockptr.user : !sockptr.kernel;

More information about the Bridge mailing list