Welcome, Guest
You have to register before you can post on our site.

Username
  

Password
  





  Is below code is compliant to rule 10.1?
Posted by: Pundlik Oulkar - 11-11-2009, 05:15 AM - Forum: 6.10 Arithmetic Type Conversions - Replies (1)

//Example 1:

struct xyz {
unsigned char bit:1;
}abc={0} ; // Is this assignment to bit-field is compliant to MISRA rule10.1 and Why?

Print this item

  Rule 15.0 - Required or Adviosry?
Posted by: William Forbes - 24-10-2009, 06:32 PM - Forum: 6.15 Switch Statements - Replies (4)

TC1 introduced the concept of rule 15.0.
However it does not explicitly state if this rule is "Required" or "Advisory"
Is rule 15.0 "required" or "advisory"?

Print this item

  Rule 16.9 - Error in code example
Posted by: William Forbes - 24-10-2009, 04:18 PM - Forum: 6.16 Functions - Replies (4)

The code in the normative text of rule 16.9 is:

Code:
if (f)     /* not compliant - gives a constant non-zero value which is
{             the address of f - use either f() or &f */
    /* ... */
}
Maybe it should be:
Code:
if (f)     /* not compliant - gives a constant non-zero value which is
              the address of f - use either f() or &f */
{
    /* ... */
}

Note - the opening brace was inside the comment.
A trivial error but maybe it should get corrected?

Print this item

  14.10 /* no else needed */
Posted by: ABR - 22-10-2009, 01:15 PM - Forum: 6.14 Control Flow - Replies (2)

In rule 14.10 (all if..... else if constructs shall be terminated with an else clause":

is the /* no else needed */ comment on the single if block with no else a suggestion for use in code, where seems useful?

Print this item

  Will there be Model Advisor rules?
Posted by: Matt Cook - 21-10-2009, 01:10 PM - Forum: MISRA AC SLSF discussions - Replies (1)

I am aware that Simulink already has Model Advisor rules for checking against the MAAB guidelines ... as well as MISRA-C, IEC 61508 and DO-178B.

Does anyone know if The Mathworks plan to introduce similar rule checking for this guideline document? (or others)
... and if so, would they be linked to any particular tool license?

Would there be any plan for MISRA to be involved in their formulation? ... or any kind of formal or informal 'approval' process of them?

Print this item

  I question the need of rule 10.6
Posted by: Lundin - 07-10-2009, 09:19 AM - Forum: 6.10 Arithmetic Type Conversions - Replies (8)

This rule seems to be commonly deviated from by most people I have spoken to. I have given it thought, but I still cannot understand how it would lead to more secure programs.

The only argument provided by MISRA for this rule is that “the type of an integer constant is a potential source of confusion”. Yes it is confusing, but only from a theoretical point-of-view, namely when you are determining what implicit type conversions that are done on the integer constant. That is, during a "scratch your head while learning C" session. During actual programming, you don't need to think about this.

I can't come up with one single scenario where leaving out the “U” suffix will lead to bugs or unintended behavior. Can anyone make such an example? If one integer in the operation is signed and the other is unsigned, and they are both of the same size, the "usual arithmetic conversions" will make sure both are unsigned. Consider the following example:

result = -1 - 10;

In this example, the type of -1 is "signed int" and the type of 10 is "int". If "int" is treated as signed by the compiler, there will not be any problems as both operands are then signed.
If "int" is treated as unsigned, then the operand -1 will be converted to unsigned, that is, to the value 65535 (assuming 16-bit system). The result of the expression will then be 655535 - 10 = 65525, which may not be the result the programmer expected. However, this will not lead to a bug! Because we aren't done with that line.

If "result" is of signed type, then the result 65525 will be converted to signed during the assignment, making it -11, which is the expected result.
If "result" is of unsigned type, the code does not make sense. If you try to stuff signed constants into a unsigned variable, no U suffix will save you.

MISRA fails to name a case where the lack of a “U” suffix would cause bugs. Integer constants are by their nature constant, and therefore deterministic. ISO C ensures that they are of the right size and signedness. It all boils down to what type the result is stored inside. If the type of the expression happens to be of different type than the variable, an implicit typecast will be done upon assignment to correct this.

Rule 10.6 also states that the implemented sizes of integer types is a reason why the U suffix should be used. I fail to see how this is relevant to the use of the U suffix. The example says that 40000 will be int or long depending on system... this has nothing to do with signedness at all. The example says that 0x8000 is possible signed in a 32-bit environment... so what? It is still a positive number in 32 bit. And you won't notice the signedness of a hex number until you represent it in a decimal format, and then it is up to you whether you want to display it as signed or unsigned.

Also, integer constants are never subject to the integer promotions, as they are always "int" or "long", so no need to worry about that either.

Further, writing “U” all over the source code is not common practice among programmers. It causes much more confusion than the hidden signedness of integer constants and worst of all, it makes the code harder to read. In addition, the MISRA-C document in itself is filled with examples where MISRA themselves do not even follow this rule. I can easily come up with countless examples, just open a random page and you will likely find it. The rule is simply too rigid and not practical to use in reality.

Though perhaps there exists a case when the lack of this suffix will lead to bugs? If not, I don't see any reasons keeping this rule.

Print this item

  Double casting to bypass 11.4?
Posted by: exoson - 04-09-2009, 10:48 PM - Forum: 6.11 Pointer Type Conversions - Replies (3)

I ran into an issue with a static analysis tool the other day.

In my example:
pool_ptr->list = *((UCHAR **) work_ptr); /*violates MISRA 11.4 */
Both pointers are UCHAR *. The first location in the buffer is being used to hold another pointer.

Adding an intermediate cast to void * clears the warning.
pool_ptr -> tx_list = *((UCHAR **) (void *)work_ptr);

Regardless that this clears the message, I believe it is still a violation of the intent of MISRA 11.4. Do you agree?
-----------------------------------------------------------------------
Scott Nowell
Validated Software Corporation

Print this item

  Combining case and default?
Posted by: exoson - 07-08-2009, 04:21 PM - Forum: 6.15 Switch Statements - Replies (1)

I would be interested in comments on the following simplified fragment.

switch (err) {
case 0:
...
break;

case 1:
case 2:
case 3:
default:
...
break;
}

I am seeing code examples like this where multiple cases are being combined with default. Most compiler will simply optimize this out to a simple test for case 0. The programmers are using this as a way to identify the results they might expect. I think having the other cases in a comment would serve this as well. MISRA C does not seem to mention this.
From a test perspective I feel it is necessary to test all declared cases.
Can anyone offer a comment on this?

Scott
Validated Software

Print this item

  MISRA C++ compliant definition of NULL
Posted by: jorgen_h_karlsson - 05-08-2009, 02:17 PM - Forum: 6.3 Basic concepts (C++) - Replies (1)

According to the rationale of required rule 3-1-1 a header file should not contain or produce definitions of objects or functions.

Does this mean that the following definition of the null-pointer-constant NULL is non-compliant?

Code:
const int NULL = 0; // Compliant with rule 3-1-1?
If so, what is a compliant definition of NULL then?

Best regards,
/J

Print this item

  Do not use the sizeof() operator
Posted by: hanthen - 03-08-2009, 01:13 PM - Forum: 6.20 Standard Libraries - Replies (8)

I would suggest a new rule for MISRA C.
"Do not use the sizeof() operator".
Output seems to be compiler dependent, e.g. TI compiler for TMS320C2x/C2xx/C5x series returns the number of 16bit units.

They (TI) claim, that there is an ambiguity in the ANSI C definition:
- sizeof() shall return the number of bytes
- sizeof(char) shall return 1
As they implement a char as 16 bit, their solution of the dilemma is to define a byte as 16 bit.

This sounds problematic and will cause issues while porting code, which uses the sizeof() operator.

Print this item

Search Forums

(Advanced Search)

Forum Statistics
» Members: 6,210
» Latest member: MC_Justin
» Forum threads: 1,017
» Forum posts: 2,796

Full Statistics

Online Users
There are currently 132 online users.
» 0 Member(s) | 129 Guest(s)
Bing, Google, UptimeRobot

Latest Threads
Rule 7.0.5, example non-c...
Forum: 4.7 Standard conversions
Last Post: cgpzs
17-04-2025, 12:10 PM
» Replies: 0
» Views: 186
A3-3-2 Contradictory exam...
Forum: AUTOSAR C++:2014 rules
Last Post: cgpzs
31-03-2025, 09:30 AM
» Replies: 2
» Views: 320
16.6.1 clarification
Forum: 4.16 Overloading
Last Post: cgpzs
31-03-2025, 09:29 AM
» Replies: 2
» Views: 301
Rule 9.3.1 - iteration st...
Forum: 4.9 Statements
Last Post: misra cpp
28-03-2025, 01:17 PM
» Replies: 1
» Views: 196
Rule 8.2.8 - why aren't a...
Forum: 4.8 Expressions
Last Post: misra cpp
28-03-2025, 01:05 PM
» Replies: 1
» Views: 217
Typo in Appendix C of MIS...
Forum: 8.10 The essential type model
Last Post: Yordan Naydenov
17-03-2025, 02:58 PM
» Replies: 0
» Views: 181
Adopted modal expressions...
Forum: General Questions
Last Post: Yordan Naydenov
17-03-2025, 09:01 AM
» Replies: 0
» Views: 287
Roadmap to c23 support
Forum: General Questions
Last Post: ACHart
28-02-2025, 03:23 PM
» Replies: 0
» Views: 221
Rule 6.2.1 weak linkage
Forum: 4.6 Basic concepts
Last Post: misra cpp
28-02-2025, 01:04 PM
» Replies: 1
» Views: 279
A8-4-5: Should have an ex...
Forum: AUTOSAR C++:2014 rules
Last Post: misra cpp
21-02-2025, 12:58 PM
» Replies: 3
» Views: 726