Unintended side effect of DR 230?

Unintended side effect of DR 230?

Post by Keith Thom » Mon, 09 Mar 2009 03:03:47


This issue came up in comp.lang.c in the thread "Question About
Sequence Points and Interrupt/Thread Safety". Mark Gooding noticed
the change from C99 to N1256.

In the original C99 standard, 6.3.1.1p2 says:

The following may be used in an expression wherever an int or
unsigned int may be used:

-- An object or expression with an integer type whose integer
conversion rank is less than or equal to the rank of int and
unsigned int.

-- A bit-field of type _Bool, int, signed int, or unsigned int.

N1256 changes this to (emphasis added):

The following may be used in an expression wherever an int or
unsigned int may be used:

-- An object or expression with an integer type whose integer
conversion rank is less than *or equal to* the rank of int and
unsigned int.

-- A bit-field of type _Bool, int, signed int, or unsigned int.

The change appeared in TC 2, in response to DR #230
< http://www.yqcomputer.com/ ;. It was
intended to deal with enumerated types with a rank equal to that of
int. It appears that the effect on unsigned int (on systems where the
range of int includes all values of type unsigned int) was unintended.

Later in paragraph 2, we see:

If an int can represent all values of the original type, the value
is converted to an int; otherwise, it is converted to an unsigned
int. These are called the _integer promotions_.

But consider a system where int covers the entire range of unsigned
int (implying that unsigned int has at least one padding bit). With
the revised wording, an expression of type unsigned int would be
promoted to signed int, which could introduce undefined behavior in
some cases.

Presumably this was not the intent.

One possible fix would be to change the wording so that the "or equal
to" clause applies only to enumerated types:

The following may be used in an expression wherever an int or
unsigned int may be used:

-- An object or expression with an integer type whose integer
conversion rank is less than the rank of int and unsigned int.

-- An object or expression with an enumerated type whose integer
conversion rank is less than or equal to the rank of int and
unsigned int.

-- A bit-field of type _Bool, int, signed int, or unsigned int.

The first and second clauses cover some of the same cases, but that
shouldn't be a problem (if it is, the first clause can be re-worded to
excluded enumerated types).

--
Keith Thompson (The_Other_Keith) XXXX@XXXXX.COM < http://www.yqcomputer.com/ ~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
 
 

Unintended side effect of DR 230?

Post by Nate Eldre » Mon, 09 Mar 2009 03:47:39

Keith Thompson < XXXX@XXXXX.COM > writes:


I think you meant

"is less than the rank of int and unsigned int"

 
 
 

Unintended side effect of DR 230?

Post by Keith Thom » Mon, 09 Mar 2009 05:00:54

Nate Eldredge < XXXX@XXXXX.COM > writes:
[...]

Argh, you're right; I copy-and-pasted from the wrong document.

--
Keith Thompson (The_Other_Keith) XXXX@XXXXX.COM < http://www.yqcomputer.com/ ~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"