Violation of rule 5-2-7 MISRA C ++ 2008: an object with a pointer type must not be converted to an unbound pointer type, directly or indirectly

In the following example:

bool bad_function()
{  
  char_t * ptr = 0;

  // MISRA doesn't complains here, it allows cast of char* to void* pointer
  void* p2 = ptr;

  // the following 2 MISRA violations are reported in each of the casts bellow (two per code line)
  // (1) Event misra_violation:     [Required] MISRA C++-2008 Rule 5-2-7 violation: An object with pointer type shall not be converted to an unrelated pointer type, either directly or indirectly
  // (1) Event misra_violation:     [Required] MISRA C++-2008 Rule 5-2-8 violation: An object with integer type or pointer to void type shall not be converted to an object with pointer type
  ptr = (char_t*) (p2); 
  ptr = static_cast<char_t*> (p2); 
  ptr = reinterpret_cast<char_t*> (p2); 

  return true;
}

MISRA 5-2-8 and 5-2-7 violations reported.

How can I remove this violation?

I need someone to test C ++ static analysis to help me. I hit myself about it a few times with these stupid rules.

In accordance with the MISRA C ++ standard (MISRA-Cpp-2008.pdf: Rule 5-2-7 (required): an object with a pointer type should not be converted to an unbound pointer type, directly or indirectly.

, , , , char*, std::ifstream, read(char* buffer, int length) (char_t*). , MISRA - ++ - ? , .

, std: ifstream :

if (file.read((char_t*)&info, (int32_t)sizeof(INFO)).gcount() != (int32_t)sizeof(INFO)
{
                LOG("ERROR: Couldn't read the file info header\n");
                res = GENERAL_FAILURE;
}

MISRA?

, - ?

: Q.Q. , , MISRA - , , . :

1 - MISRA , , , ( Q.Q.)

2 - char file.read(), , , , , char * int32_t - 5-2-7. .

+4
3

MISRA , / , , . . typecast ( ++ _cast) , - , - , undefined.

, undefined , . MISRA , ... , , , , .

, MISRA , , , undefined ( ..) " ", .

file.read() () info.

if (file.read((char_t*)&info, (int32_t)sizeof(INFO)).gcount() != (int32_t)sizeof(INFO)
{
            LOG("ERROR: Couldn't read the file info header\n");
            res = GENERAL_FAILURE;
}

, , , MISRA. -

std::streamsize size_to_read = whatever();
std::vector<char> buffer(size_to_read);  

if (file.read(&buffer[0], size_to_read) == size_to_read)
{
      //  use some rules to interpret contents of buffer (i.e. a protocol) and populate info
      // generally these rules will check that the data is in a valid form
      //   but not rely on doing any pointer type conversions
}
else
{
            LOG("ERROR: Couldn't read the file info header\n");
            res = GENERAL_FAILURE;
}

, , , , . . MISRA, , , , , . ( , sizeof), - A - , , B. MISRA - , , .

. char_t * std::istream::read() int32_t . . char * std::streamsize ( , int32_t).

+1

char* . , , , .

+1

fread - ++ , void*, MISRA.

, fstream, ( "" iostream, , - ).

C- fopen/fclose ++, . , std::unique_ptr, RAII . std::unique_ptr<FILE*, decltype(&fclose)> / ++.


NB: A common misconception is that it std::ios::binaryprovides binary I / O. Is not. All this affects the conversion of a new line and (on some systems) the processing of end-of-file markers, but there is no effect on the conversion of a character to a facet.

0
source

Source: https://habr.com/ru/post/1619220/


All Articles