CCAN Archive mirror
 help / color / mirror / Atom feed
From: Yubin Ruan <ablacktshirt@gmail.com>
To: ccan@lists.ozlabs.org
Subject: why add the result of check_type in container_of
Date: Mon, 12 Mar 2018 11:19:39 +0800	[thread overview]
Message-ID: <20180312031939.GA29780@HP> (raw)

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

             reply	other threads:[~2018-03-12  3:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-12  3:19 Yubin Ruan [this message]
2018-03-14  1:18 ` why add the result of check_type in container_of David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180312031939.GA29780@HP \
    --to=ablacktshirt@gmail.com \
    --cc=ccan@lists.ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).