title: Avoiding buffer length checks before buffer access in C++
body: <p>I have a <code>char*</code> buffer which we need to decode into a db. The buffer is such that if not null, it's very big and contains integers and strings.</p>
  
  <p>The buffer will contain contents like a 4-byte integer length followed by that an ASCII string that many characters long. Then it's followed by another 4-byte length and a string of that length, and so on until we reach the end.</p>
  
  <p>Most of the time the buffer is valid but sometimes I see that the buffer is corrupted and I get a SEGV. I get a SEGV since the length resulted in reading more bytes than the rest of the buffer length.</p>
  
  <p>I <em>do not</em> want to put checks everywhere for the remaining buffer length before decoding. The buffer fields are 200 in number. What check can I provide here which can help me solve this problem without the overhead of extra <code>if</code> checks?</p>