public inbox for ccan@ozlabs.org
 help / color / mirror / Atom feed
* why add the result of check_type in container_of
@ 2018-03-12  3:19 Yubin Ruan
  2018-03-14  1:18 ` David Gibson
  0 siblings, 1 reply; 2+ messages in thread
From: Yubin Ruan @ 2018-03-12  3:19 UTC (permalink / raw)
  To: ccan

Hi,

I notice that in the implementation of container_of(), the resulting pointer
will be advanced by 1 if types mismatch:

#define container_of(member_ptr, containing_type, member)   \
	 ((containing_type *)                                   \
	  ((char *)(member_ptr)                                 \
	   - container_off(containing_type, member))            \
	  + check_types_match(*(member_ptr), ((containing_type *)0)->member))

where check_types_match() is defined as 

#define check_types_match(expr1, expr2)		\
	((typeof(expr1) *)0 != (typeof(expr2) *)0)

which will return 1 if types mismatch.

IMO, advancing the wrong pointer (resulting from mismatch types) by 1 will
only make things worse, and users usually only get garbage results and will not
be aware of the type-mismatch bug. Wouldn't it be better to throw an building
error/warning for that?

Yubin
_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-03-14  1:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-12  3:19 why add the result of check_type in container_of Yubin Ruan
2018-03-14  1:18 ` David Gibson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox