Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is what you should do:</p> <pre><code>reader.read((char*)myArray, myArrayLength); /* note, that isn't (sizeof myArray) */ if (!reader) { /* report error */ } </code></pre> <p>If there's processing going on inside the loop, then</p> <pre><code>int c = 0; while (c + 2 &lt; myArraySize) //reader is a ifstream open to a BMP file { reader.read(pixelInfo, 3); myArray[c] = (unsigned char)pixelInfo[0]; myArray[c + 1] = (unsigned char)pixelInfo[1]; myArray[c + 2] = (unsigned char)pixelInfo[2]; c += 3; } </code></pre> <p>Trying to read after you've hit the end is not a problem -- you'll get junk in the rest of the array, but you can deal with that at the end.</p> <p>Assuming your array is big enough to hold the whole file invites buffer corruption. Buffer overrun attacks involving image files with carefully crafted incorrect metadata are quite well-known.</p> <ul> <li><a href="http://www.mozilla.org/security/announce/2012/mfsa2012-92.html" rel="nofollow">in Mozilla</a></li> <li><a href="http://www.kb.cert.org/vuls/id/388289" rel="nofollow">in Sun Java</a></li> <li><a href="https://www.kb.cert.org/vuls/id/189754" rel="nofollow">in Internet Explorer</a></li> <li><a href="http://technet.microsoft.com/en-us/security/bulletin/ms06-024" rel="nofollow">in Windows Media Player</a></li> <li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=251381" rel="nofollow">again in Mozilla</a></li> <li><a href="http://www.securityfocus.com/bid/12506/discuss" rel="nofollow">in MSN Messenger</a></li> <li><a href="http://www.us-cert.gov/cas/techalerts/TA05-362A.html" rel="nofollow">in Windows XP</a></li> </ul> <p><strong>Do not rely on the entire file content fitting in the calculated buffer size.</strong></p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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