linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Nick Bowler <nbowler@elliptictech.com>
To: "Rémi Denis-Courmont" <remi@remlab.net>
Cc: Mauro Carvalho Chehab <mchehab@redhat.com>,
	mchehab@infradead.org, linux-media@vger.kernel.org,
	linux-kernel@vger.kernel.org, Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Hans Verkuil <hverkuil@xs4all.nl>
Subject: Re: [RFC] [PATCH] v4l2: use unsigned rather than enums in ioctl() structs
Date: Thu, 12 Apr 2012 13:22:45 -0400	[thread overview]
Message-ID: <20120412172245.GA1721@elliptictech.com> (raw)
In-Reply-To: <201204112332.24353.remi@remlab.net>

On 2012-04-11 23:32 +0300, Rémi Denis-Courmont wrote:
> From the perspective of the compiler, this is a feature not a bug. In
> C and C++, loading or storing a value in an enumerated variable
> whereby the value is not a member of the enumeration is undefined.

I'm afraid that this is not the case in C, although it may be in C++
(enums are very different in C++ than they are in C).  In C, enum types
are required to be compatible with some integer type capable of storing
the values of all the enum members (see C11§6.7.2.2#4).  Compatibility
is a very strong condition, and implies that the two types are
interchangable without affecting the meaning of the program (see
C11§6.2.7).  Integer types have a number of specific requirements, one
thing that's relevant here is that they do not have "holes" in their
representable values: there is a minimum and maximum representable
value, and all integers between them are representable (C11§6.2.6.2#1).

Thus, while the choice of integer type used may depend on the values of
the corresponding enum constants, storing any value (regardless of
whether or not its a member of the enumeration) is subject to the same
rules as the implementation-defined compatbile integer type.  This is
always well-defined for values within the range of the type.
(C11§6.3.1.3#1 and C11§6.3.1.4#1).

> In other words, the compiler can assume that this does not happen, and
> optimize it away.

No, a conforming C compiler cannot assume such assignments do not
happen, for the reasons outlined above.

Cheers,
-- 
Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)


  reply	other threads:[~2012-04-12 17:22 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-05 17:52 [RFC] [PATCH] v4l2: use unsigned rather than enums in ioctl() structs Rémi Denis-Courmont
2012-04-11 17:02 ` Mauro Carvalho Chehab
2012-04-11 18:47   ` Rémi Denis-Courmont
2012-04-11 19:53     ` Mauro Carvalho Chehab
2012-04-11 20:32       ` Rémi Denis-Courmont
2012-04-12 17:22         ` Nick Bowler [this message]
2012-04-11 20:08     ` Mauro Carvalho Chehab
2012-04-12  8:04     ` James Courtier-Dutton
2012-04-12 14:55       ` Mauro Carvalho Chehab
2012-04-12 15:41         ` Rémi Denis-Courmont
2012-04-17 17:50           ` Mauro Carvalho Chehab
2012-04-27  8:24             ` [RFC 1/1] v4l: Implement compat handlers for ioctls containing enums Sakari Ailus
2012-04-13  8:25         ` [RFC] [PATCH] v4l2: use unsigned rather than enums in ioctl() structs James Courtier-Dutton

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=20120412172245.GA1721@elliptictech.com \
    --to=nbowler@elliptictech.com \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=hverkuil@xs4all.nl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@infradead.org \
    --cc=mchehab@redhat.com \
    --cc=remi@remlab.net \
    /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 NNTP newsgroup(s).