@@ -458,9 +458,25 @@ bool AudioFile<T>::load (std::string filePath)
458
458
return false ;
459
459
}
460
460
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
+ }
464
480
465
481
// get audio file format
466
482
audioFileFormat = determineAudioFileFormat (fileData);
@@ -564,6 +580,12 @@ bool AudioFile<T>::decodeWaveFile (std::vector<uint8_t>& fileData)
564
580
{
565
581
int sampleIndex = samplesStartIndex + (numBytesPerBlock * i) + channel * numBytesPerSample;
566
582
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
+
567
589
if (bitDepth == 8 )
568
590
{
569
591
T sample = singleByteToSample (fileData[sampleIndex]);
@@ -702,6 +724,12 @@ bool AudioFile<T>::decodeAiffFile (std::vector<uint8_t>& fileData)
702
724
{
703
725
int sampleIndex = samplesStartIndex + (numBytesPerFrame * i) + channel * numBytesPerSample;
704
726
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
+
705
733
if (bitDepth == 8 )
706
734
{
707
735
int8_t sampleAsSigned8Bit = (int8_t )fileData[sampleIndex];
0 commit comments