@@ -458,9 +458,25 @@ bool AudioFile<T>::load (std::string filePath)
458458 return false ;
459459 }
460460
461- file.unsetf (std::ios::skipws);
462- std::istream_iterator<uint8_t > begin (file), end;
463- std::vector<uint8_t > fileData (begin, end);
461+ std::vector<uint8_t > fileData;
462+
463+ file.unsetf (std::ios::skipws);
464+
465+ file.seekg (0 , std::ios::end);
466+ size_t length = file.tellg ();
467+ file.seekg (0 , std::ios::beg);
468+
469+ // allocate
470+ fileData.resize (length);
471+
472+ file.read (reinterpret_cast <char *> (fileData.data ()), length);
473+ file.close ();
474+
475+ if (file.gcount () != length)
476+ {
477+ reportError (" ERROR: Couldn't read entire file\n " + filePath);
478+ return false ;
479+ }
464480
465481 // get audio file format
466482 audioFileFormat = determineAudioFileFormat (fileData);
@@ -564,6 +580,12 @@ bool AudioFile<T>::decodeWaveFile (std::vector<uint8_t>& fileData)
564580 {
565581 int sampleIndex = samplesStartIndex + (numBytesPerBlock * i) + channel * numBytesPerSample;
566582
583+ if ((sampleIndex + (bitDepth / 8 ) - 1 ) >= fileData.size ())
584+ {
585+ reportError (" ERROR: read file error as the metadata indicates more samples than there are in the file data" );
586+ return false ;
587+ }
588+
567589 if (bitDepth == 8 )
568590 {
569591 T sample = singleByteToSample (fileData[sampleIndex]);
@@ -702,6 +724,12 @@ bool AudioFile<T>::decodeAiffFile (std::vector<uint8_t>& fileData)
702724 {
703725 int sampleIndex = samplesStartIndex + (numBytesPerFrame * i) + channel * numBytesPerSample;
704726
727+ if ((sampleIndex + (bitDepth / 8 ) - 1 ) >= fileData.size ())
728+ {
729+ reportError (" ERROR: read file error as the metadata indicates more samples than there are in the file data" );
730+ return false ;
731+ }
732+
705733 if (bitDepth == 8 )
706734 {
707735 int8_t sampleAsSigned8Bit = (int8_t )fileData[sampleIndex];
0 commit comments