Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ defensive programming: reading from a buffer with type safety
    primarykey
    data
    text
    <p>Let's say I have a class that I don't own: DataBuffer. It provides various get member functions:</p> <pre><code>get(uint8_t *value); get(uint16_t *value); ... </code></pre> <p>When reading from a structure contained in this buffer, I know the order and size of fields, and I want to reduce the chance of future code changes causing an error:</p> <pre><code>struct Record { uint16_t Header; uint16_t Content; } void ReadIntoRecord(Record* r) { DataBuffer buf( initialized from the network with bytes ) buf.get(&amp;r-&gt;Header); // Good! buf.get(&amp;r-&gt;Content); } </code></pre> <p>Then someone checks in a change to do something with the header before writing it:</p> <pre><code> uint8_t customHeader; buf.get(&amp;customHeader); // Wrong, stopped reading after only 1 byte r-&gt;Header = customHeader + 1; buf.get(&amp;r-&gt;Content); // now we're reading from the wrong part of the buffer. </code></pre> <p><strong>Is the following an acceptable way to harden the code against changes? Remember, I can't change the function names to getByte, getUShort, etc. I could inherit from DataBuffer, but that seems like overkill.</strong></p> <pre><code> buf.get(static_cast&lt;uint16_t*&gt;(&amp;r-&gt;Header)); // compiler will catch incorrect variable type buf.get(static_cast&lt;uint16_t*&gt;(&amp;r-&gt;Content)) </code></pre> <p>Updated with not-eye-safe legacy code example:</p> <pre><code> float dummy_float; uint32_t dummy32; uint16_t dummy16; uint8_t dummy8; uint16_t headTypeTemp; buf.get(static_cast&lt;uint16_t*&gt;(&amp;headTypeTemp)); m_headType = HeadType(headTypeTemp); buf.get(static_cast&lt;uint8_t*&gt;(&amp;hid)); buf.get(m_Name); buf.get(m_SerialNumber); float start; buf.get(static_cast&lt;float*&gt;(&amp;start)); float stop; buf.get(static_cast&lt;float*&gt;(&amp;stop)); buf.get(static_cast&lt;float*&gt;(&amp;dummy_float)); setStuffA(dummy_float); buf.get(static_cast&lt;uint16_t*&gt;(&amp;dummy16)); setStuffB(float(dummy16)/1000); buf.get(static_cast&lt;uint8_t*&gt;(&amp;dummy8)); //reserved buf.get(static_cast&lt;uint32_t*&gt;(&amp;dummy32)); Entries().setStart( dummy32 ); buf.get(static_cast&lt;uint32_t*&gt;(&amp;dummy32)); Entries().setStop( dummy32 ); buf.get(static_cast&lt;float*&gt;(&amp;dummy_float)); Entries().setMoreStuff( dummy_float ); uint32_t datalength; buf.get(static_cast&lt;uint32_t*&gt;(&amp;datalength)); Entries().data().setLength(datalength); RetVal ret = ReturnCode::SUCCESS; Entry* data_ptr = Entries().data().data(); for (unsigned int i = 0; i &lt; datalength &amp;&amp; ret == ReturnCode::SUCCESS; i++) { ret = buf.get(static_cast&lt;float*&gt;(&amp;dummy_float)); data_ptr[i].FieldA = dummy_float; } for (unsigned int i = 0; i &lt; datalength &amp;&amp; ret == ReturnCode::SUCCESS; i++) { ret = buf.get(static_cast&lt;float*&gt;(&amp;dummy_float)); data_ptr[i].FieldB = dummy_float; } // Read in the normalization vector Util::SimpleVector&lt;float&gt; norm; buf.get(static_cast&lt;uint32_t*&gt;(&amp;datalength)); norm.setLength(datalength); for (unsigned int i=0; i&lt;datalength; i++) { norm[i] = buf.getFloat(); } setNormalization(norm); return ReturnCode::SUCCESS; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload