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

Username
  

Password
  





  Rule 18.1 and Rule 11.3 compatibility
Posted by: m.bruna - 06-05-2016, 03:29 PM - Forum: 8.18 Pointers and arrays - Replies (3)

Hi,

i am not sure about compliance of following example.

Code:
uint32_t varialble = 0;
uint8_t * ptr = (uint8_t *)variable;

(*ptr) = 0;
ptr++;        //line in question
(*ptr) = 0;        //line in question

While exception to the rule 11.3 clearly says, that converting the pointer to object to pointer to char is ok and can be used for accessing individual bytes, Rule 18.1 says that object that is not an array shall be treated as an array of single element and does not allow dereferencing a pointer beyond the end of it.

Can an object that is not an array be treated as an array of 8-bit variables with length of its byte size for this purpose? or can i only access the first byte of the object this way?

thanks,

Michal

Print this item

  About 17.4 Question?
Posted by: hutry999 - 05-05-2016, 09:36 AM - Forum: 6.17 Pointers and Arrays - Replies (2)

I need read some parameters from EEPROM. My Code as below:

typedef struct
{
Uint16 ErrorCode;
Uint16 ErrDCVoltage;
Uint16 ErrMCUState;
Uint8 ErrMotorModeCmd;
Uint8 ErrDriveState;
Uint16 ErrTorqueCmd;
Uint16 ErrTorqueFed;
Uint16 ErrSpeedSet;
Uint16 ErrSpeed;
Uint8 ErrIGBTTemp;
Uint8 ErrMotorTemp;
Uint16 ErrUd;
Uint16 ErrUq;
Uint16 ErrIdRef;
Uint16 ErrIqRef;
Uint16 ErrIdFed;
Uint16 ErrIqFed;
Uint16 ErrState;
Uint8 Err12V;
Uint8 Angle;
Uint8 AngleInit;
Uint8 ErrMCUTemp;
Uint16 DC_Cur;
}ErrorLog_t;

void LoadError(ErrorLog_t* Err,Uint16 Index)
{
Uint16 i;
Uint16 *PrErrLog;
PrErrLog = (uint16 *)Err;
for(i=0u;iErrorCode = ReadEeprom(0u);
Err->ErrDCVoltage = ReadEeprom(1u);
Err->ErrMCUState = ReadEeprom(2u);
....

if my struct has more than 100 items, What can I do?

Print this item

  Clarification for 7-3-1: extern "C++"
Posted by: anj - 28-04-2016, 04:21 PM - Forum: 6.7 Declarations (C++) - Replies (2)

For headers included by other C and C++ headers it is vital to use extern "C++".
But the rule implicitly forbids it by only allowing 3 kind of top level declarations.

Is it really intended to use this as the only compliant way:

Code:
extern "C"
{
  extern "C++"
  {
    namespace MY_API
    {
    }
  }
}

Print this item

  Rules 6-5-1 to 6-5-6
Posted by: dg1980 - 28-04-2016, 09:32 AM - Forum: 6.5 Expressions (C++) - Replies (1)

Hi,

i was under the impression that these rules apply to for loops only, as all examples show only for loops.
I recently ran across a static analyzer, which applied these rules to while loops too.
Is this intended by MISRA?
Thanks in advance.

Print this item

  Rules 16-2-1 and 16-2-2
Posted by: dg1980 - 28-04-2016, 09:28 AM - Forum: 6.16 Preprocessing directives (C++) - Replies (2)

Hi,

strictly interpreted, these rules ban conditional compilation, rendering rules 16-1-1 and 16-1-2 mute.
E.g.:

Code:
#ifdef FEATURE_X
// some code
#endif

In addition, there appears to be no such rule in MISRA C 2012 (presumably, because it is a widely used feature in embedded systems):

Rule Mapping to MISRA C 2012
16-0-2 [Required] n.a. but namespaces are not available in C
16-1-1 [Required] n.a.
16-2-1 [Required] n.a.prohibits conditional compilation which is allowed in C
16-2-2 [Required] n.a.prohibits conditional compilation which is allowed in C

I would expect that a feature like the preprocessor, which is equal in C and C++, is treated equally across all MISRA standards.
Is there any chance this will be corrected/clarified in a new version or technical corrigendum?

Print this item

  11.3 - char * to uint8_t *
Posted by: delta_controls - 27-04-2016, 11:05 AM - Forum: 8.11 Pointer type conversions - Replies (5)

In the following example there is an implicit conversion from (char *) to (uint8_t *):

Code:
extern void print(const uint8_t *text);

int main(void)
{
    print("Some text");
}

Am I correct in thinking that this is compliant? Or does the exception to rule 11.3 only apply when conversions are explicit?

Thanks.

Print this item

  Rule 8.4 - main function
Posted by: delta_controls - 20-04-2016, 04:44 PM - Forum: 8.8 Declarations and defnitions - Replies (6)

Should this rule have an exception for main? I can't see how main is automatically excluded from this rule. Is the expectation that a prototype is provided for main?

Thanks.

Print this item

  Rule 19.4 Does "#define XY -1" violate 19.4
Posted by: fst@bm - 17-03-2016, 03:37 PM - Forum: 6.19 Preprocessing Directives - Replies (2)

Dear MISRA Team,

as I found out, 2 out of 3 commercial MISRA checking tools report the statement "#define XY -1" to be a violation of rule 19.4. I am not sure whether or not this #define actually violates rule 19.4 but if so I would not fully understand the risk associated with this particular #define statement. Could someone please help and discuss if this is a violation and why it should be avoided?

Thank your for your support!

Print this item

  Question about 14.2 and the controlling variable
Posted by: triboix - 23-02-2016, 09:43 AM - Forum: 8.14 Control statement expressions - Replies (2)

Hello,

14.2 states that the "controlling variable" must be of essentially boolean type.

Does that mean that the following code is non-compliant?

Code:
struct stuff
{
    uint32_t id;
};
struct stuff stuff_array[MAXSTUFF];

struct stuff* stuff_lookup(uint32_t id)
{
    uint8_t i;
    struct stuff* found = NULL;
    for (i = 0; (i < MAXSTUFF) && (found == NULL); i++) {
        if (stuff_array[i].id == id) {
            found = &(stuff_array[i]);
        }
    }
    return found;
}

Thanks a lot for any help!

Print this item

  How do I create a module in MISRAC:2012 that follows Dir 4.12 and 4.8?
Posted by: crisls1 - 18-02-2016, 08:52 AM - Forum: 7.4 Code design - Replies (2)

I asked the following question on Stack Overflow and received a helpful answer which I have integrated into my question here. (http://stackoverflow.com/questions/35341...12-and-4-8) The following is an extended version of the question I asked. This question relates to coding in ISO C99 following the MISRAC:2012 guidelines.

I am looking for guidance on Dir 4.8 “If a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should be hidden” in conjunction with Dir 4.12 “Dynamic memory allocation shall not be used”.

When implementing an Abstract Data Type in C it is common to refer to the ADT using a handle that is a pointer to a structure describing the internal state of the ADT. This can be done using an opaque pointer as per Dir 4.8 with the benefit that the internal details remain hidden from the user.

Generally more than one of these ADTs could exist so there must be a way to create mulitple handles. This can be solved by allocating memory for the internal details referenced by the handle in an initialisation function, however, this is not allowed under Dir 4.12.

Another option is that the initialisation routine receives a pointer to a statically allocated handle provided by the user, however, this cannot be accomplished using opaque pointers.

I illustrate the problem below.


Module.h

Code:
/* Module handle is only available to  the world as an incomplete type.
           This allows us to satisfy MISRAC 2012 Dir 4.8.*/
        struct module;
        typedef struct module module_t;
Module.c
Code:
#include "module.h"
        struct module
        {
            uint8_t value;
        };
        module_t* module_get_a_handle(void)
        {
             /* MISRAC 2012 Dir 4.12 disallows dynamic memory allocation.*/
             return (module_t*)malloc(sizeof(struct module));
        }
User.c
Code:
#include "module.h"
        module_t* module_handle;
        module_handle = module_get_a_handle();

One solution is to use a statically allocated pool of handles within the ADT that are made available to client code. This solution seems to be technically compliant; however, it seems that the concept of dynamic memory allocation still exists here. I would argue that although the handles are statically allocated, it cannot be determined by the compiler during compilation whether there will be enough handles available for the software to function correctly.

I understand the argument that if a module runs out of statically allocated handles then it is a design error. However, it seems advantageous if the compiler can also determine in many cases whether there is enough space at compile time (this forces developers to fix the problem early rather than discovering it in code reviews or testing later on). The compiler can determine the problem if a statically allocated handle is passed from client code to the ADT as it is then clear to the compiler how many handles in total will be required by the SW. This also feels like a cleaner separation of concerns to me. Is it the responsibility of the ADT to know how many handles may be required, and if so does this not complicate any module reuse somewhat?

Additionally, in my case we are defining a company template that all new modules should be based on. The level of C proficiency of our developers ranges from beginner to intermediate. I am concerned that requiring each ADT to manage a static buffer of handles will be seen as overly complicated (and therefore a maintenance issue) and be met with resistance. Clearly raising the C skill level of developers is desirable but this will not happen immediately.

My solution to this problem would be to deviate on the advisory directive 4.8 and use non-opaque pointers and a strong naming convention that makes it clear to users that the internal details of the ADT must not be changed.

I am curious whether there is a well accepted method to solve this issue that satisfies Dir 4.8, and Dir 4.12, and does not break any other MISRAC:2012 rules. Is it common practice to define a static array of handles in every ADT?

I'm also interested in a critique of

a) deviating Dir 4.8 and using a naming convention that makes it obvious that the handle points to internal details that should not be accessed directly coupled with educating developers not to access the internals details.

b) deviating Dir 4.12(!) to allow dynamic memory allocation in a well defined initialisation phase only. For b), I believe that if it can be shown that the memory allocation is deterministic in this initialisation phase and the initialisation phase can be tested at a system level successfully once then it follows that the memory allocation will always be successful.

My preference is to deviate on the advisory directive 4.8 in this particular case as it seems to have the least disadvantages.

Any comments would be greatly appreciated.

Print this item

Search Forums

(Advanced Search)

Forum Statistics
» Members: 6,171
» Latest member: stephanmuench
» Forum threads: 998
» Forum posts: 2,752

Full Statistics

Online Users
There are currently 378 online users.
» 0 Member(s) | 376 Guest(s)
Applebot, Bing

Latest Threads
Rule 6.2.1: non-inline co...
Forum: 4.6 Basic concepts
Last Post: cgpzs
22-11-2024, 10:11 AM
» Replies: 0
» Views: 23
Rule 0.1.2
Forum: 4.0 Language independent issues
Last Post: stephanmuench
21-11-2024, 01:12 PM
» Replies: 0
» Views: 34
A18-9-4
Forum: AUTOSAR C++:2014 rules
Last Post: cgpzs
23-10-2024, 12:04 PM
» Replies: 2
» Views: 363
A8-4-5: are partial moves...
Forum: AUTOSAR C++:2014 rules
Last Post: misra cpp
22-10-2024, 02:03 PM
» Replies: 1
» Views: 320
model information blocks ...
Forum: MISRA AC SLSF discussions
Last Post: misra-ac
22-10-2024, 01:27 PM
» Replies: 1
» Views: 4,440
MISRA AL SLSF - Rule 043I
Forum: MISRA AC SLSF discussions
Last Post: misra-ac
22-10-2024, 01:11 PM
» Replies: 1
» Views: 8,860
MISRA AC EC guidelines
Forum: MISRA AC SLSF discussions
Last Post: misra-ac
21-10-2024, 08:21 AM
» Replies: 4
» Views: 15,497
News on future releases
Forum: MISRA AC SLSF discussions
Last Post: misra-ac
21-10-2024, 08:05 AM
» Replies: 1
» Views: 5,689
Signal naming convention ...
Forum: MISRA AC SLSF discussions
Last Post: misra-ac
21-10-2024, 07:57 AM
» Replies: 1
» Views: 7,244
Rule 7.0.2: operator cons...
Forum: 4.7 Standard conversions
Last Post: karos
14-10-2024, 08:52 PM
» Replies: 2
» Views: 429