2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-09-18 13:31:23 +02:00
The values that can be represented by a signed bit field with a length of one bit may not meet developer expectations. For example, according to the C99 Standard, a single-bit signed bit-field has a single (one) sign bit and no (zero) value bits.
2021-04-28 16:49:39 +02:00
This rule does not apply to unnamed bit fields, as their values cannot be accessed.
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
signed int f:1; // Noncompliant; there's only room here for the sign
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
unsigned int f:1;
----
or
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
signed int:1; // unnamed
----
or
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
signed int f:2;
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
== Resources
2021-04-28 16:49:39 +02:00
* MISRA C:2004, 6.5 - Bit fields of type signed int shall be at least 2 bits long
* MISRA C:2012, 6.2 - Single-bit named bit fields shall not be of a signed type
* MISRA {cpp}:2008, 9-6-4 - Named bit-fields with signed integer type shall have a length of more than one bit
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Review "X" declaration sign and size.
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== is duplicated by: S1010
=== on 9 Dec 2014, 20:58:20 Evgeny Mandrikov wrote:
\[~ann.campbell.2] could you please verify this description, which was built as a combination of MISRA C:2012 and MISRA {cpp}2008?
=== on 14 Dec 2014, 22:51:08 Evgeny Mandrikov wrote:
\[~ann.campbell.2] I was expecting that you'll also specify SQALE model and other parameters, but ok...
=== on 15 Dec 2014, 14:58:47 Ann Campbell wrote:
Sorry [~evgeny.mandrikov], I didn't notice the lack of SQALE &etc. I'll pay more attention next time.
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]