2

I have some C++ code with a C interface and I'm struggling to imagine how to come in compliance with a few Autosar C++ rules.

For example:

std::string GetAddress(const std::string &clientName);

vs

StatusReturn_t GetAddress(const char *pClientName, char *pAddress, size_t maxAddrLen);

The C++ interface being vaguely compliant and it's C interface runs afoul of A8-4-8 "Output parameters shall not be used." This one could return a struct with a Status and the address. If the interface could live in a C file it might escape the scanner but I have trouble imagining where char * become std::string without violating this rule somewhere.

Second, the C interface can have issues with A20-8-2 or A20-8-3 which is totally incompatible with C callers.

tadman
  • 208,517
  • 23
  • 234
  • 262
foreverska
  • 585
  • 3
  • 20
  • 1
    The C++ interface doesn't have a length limit or require a preallocated buffer so why should the C interface? Can't you just return a `typedef struct { char *data; uint32_t length; } String;` ? – Ted Lyngmo May 25 '23 at 17:44
  • So a pair/tuple (in struct form) of the lookup type and a return code is a solution to A8-4-8 if one puts the onus of memory management on the C developer. But that does in itself run afoul of A20-8-2/3 since the pointer in return can't be unique/shared. – foreverska May 25 '23 at 18:52
  • You mean the C++14 rules A20 8.2 & 8.3? Ok, well, if that's how you implement the Autosar rules, I guess you'll have a really tough time fulfilling them. In order to provide a C interface for a C++ library, you would have to draw the line at the API level and hand over the ownership of the pointer to the C caller in order for the caller to call `FreeString` on it later. The alternative is not very nice but it's simpler to fulfill: Implement the library in C and add a C++ layer on top of it. – Ted Lyngmo May 25 '23 at 19:05
  • 1
    You can't use std::string or any other such PC programming for MISRA compliance. Anything that puts data on the heap is outlawed. – Lundin May 26 '23 at 06:56

0 Answers0