title: 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
body: <p>In the following example:</p>
  
  <pre><code>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&lt;char_t*&gt; (p2); 
    ptr = reinterpret_cast&lt;char_t*&gt; (p2); 
  
    return true;
  }
  </code></pre>
  
  <p>MISRA 5-2-8 and 5-2-7 violations are reported. </p>
  
  <p>How I can remove this violation ?</p>
  
  <p>I need someone experienced with C++ static analysis to help me. I am hitting my head with this stupid rules from few days.</p>
  
  <p>According to MISRA C++ standard (MISRA-Cpp-2008.pdf:  <strong>Rule 5-2-7 (required): An object with pointer type shall not be converted to an unrelated pointer type, either directly or indirectly.</strong></p>
  
  <p>Ok but we have a lot of code which for example needs to convert address to <code>char*</code> and then to use it with <code>std::ifstream</code>, which <code>read(char* buffer, int length)</code> function requires to type cast the address to (<code>char_t*</code>). So how according to MISRA guys someone can program in C++ and not using at all any casts? The standard doesn't say HOW pointer conversion then must be done.</p>
  
  <p>In my production code my problems are in file reading operations using read with <strong>std:ifstream</strong> from files in predefined data structures:</p>
  
  <pre><code>if (file.read((char_t*)&amp;info, (int32_t)sizeof(INFO)).gcount() != (int32_t)sizeof(INFO)
  {
                  LOG("ERROR: Couldn't read the file info header\n");
                  res = GENERAL_FAILURE;
  }
  </code></pre>
  
  <p>How is supposed to do it according to MISRA?</p>
  
  <p>So is there are any solutions at all?</p>
  
  <p>EDIT:  Peter and  Q.Q. answers are both correct, it seems that MISRA really wants to do everything without any casts which is hard to be done if the project is in the final stage. Therea are two options:</p>
  
  <p>1 - document the MISRA deviations one by one and explain why casts are Ok, explain how this has been tested (Q.Q. suggestion)</p>
  
  <p>2 - use byte array from char type for file.read(), then after safely reading the file content cast the byte array to the headers content, this must be done for each member one by one because if you cast char* to int32_t this is again Rule 5-2-7 violation. Sometimes it is too much work.  </p>