@@ -508,7 +508,17 @@ bool AudioFile<T>::load (std::string filePath)
508
508
return false ;
509
509
}
510
510
511
- return loadFromMemory (fileData);
511
+ // Handle very small files that will break our attempt to read the
512
+ // first header info from them
513
+ if (fileData.size () < 12 )
514
+ {
515
+ reportError (" ERROR: File is not a valid audio file\n " + filePath);
516
+ return false ;
517
+ }
518
+ else
519
+ {
520
+ return loadFromMemory (fileData);
521
+ }
512
522
}
513
523
514
524
// =============================================================
@@ -650,10 +660,16 @@ bool AudioFile<T>::decodeWaveFile (std::vector<uint8_t>& fileData)
650
660
int32_t sampleAsInt = fourBytesToInt (fileData, sampleIndex);
651
661
T sample;
652
662
653
- if (audioFormat == WavAudioFormat::IEEEFloat)
654
- sample = (T)reinterpret_cast <float &> (sampleAsInt);
663
+ if (audioFormat == WavAudioFormat::IEEEFloat)
664
+ {
665
+ float f;
666
+ memcpy (&f, &sampleAsInt, sizeof (int32_t ));
667
+ sample = (T)f;
668
+ }
655
669
else // assume PCM
670
+ {
656
671
sample = resampleIntegerSample<int32_t , T>(sampleAsInt);
672
+ }
657
673
658
674
samples[channel].push_back (sample);
659
675
}
@@ -1200,14 +1216,22 @@ AudioFileFormat AudioFile<T>::determineAudioFileFormat (std::vector<uint8_t>& fi
1200
1216
template <class T >
1201
1217
int32_t AudioFile<T>::fourBytesToInt (std::vector<uint8_t >& source, int startIndex, Endianness endianness)
1202
1218
{
1203
- int32_t result;
1204
-
1205
- if (endianness == Endianness::LittleEndian)
1206
- result = (source[startIndex + 3 ] << 24 ) | (source[startIndex + 2 ] << 16 ) | (source[startIndex + 1 ] << 8 ) | source[startIndex];
1219
+ if (source.size () >= (startIndex + 4 ))
1220
+ {
1221
+ int32_t result;
1222
+
1223
+ if (endianness == Endianness::LittleEndian)
1224
+ result = (source[startIndex + 3 ] << 24 ) | (source[startIndex + 2 ] << 16 ) | (source[startIndex + 1 ] << 8 ) | source[startIndex];
1225
+ else
1226
+ result = (source[startIndex] << 24 ) | (source[startIndex + 1 ] << 16 ) | (source[startIndex + 2 ] << 8 ) | source[startIndex + 3 ];
1227
+
1228
+ return result;
1229
+ }
1207
1230
else
1208
- result = (source[startIndex] << 24 ) | (source[startIndex + 1 ] << 16 ) | (source[startIndex + 2 ] << 8 ) | source[startIndex + 3 ];
1209
-
1210
- return result;
1231
+ {
1232
+ assert (false && " Attempted to read four bytes from vector at position where out of bounds access would occur" );
1233
+ return 0 ; // this is a dummy value as we don't have one to return
1234
+ }
1211
1235
}
1212
1236
1213
1237
// =============================================================
@@ -1250,6 +1274,7 @@ template <class T>
1250
1274
int AudioFile<T>::getIndexOfChunk (std::vector<uint8_t >& source, const std::string& chunkHeaderID, int startIndex, Endianness endianness)
1251
1275
{
1252
1276
constexpr int dataLen = 4 ;
1277
+
1253
1278
if (chunkHeaderID.size () != dataLen)
1254
1279
{
1255
1280
assert (false && " Invalid chunk header ID string" );
@@ -1265,6 +1290,11 @@ int AudioFile<T>::getIndexOfChunk (std::vector<uint8_t>& source, const std::stri
1265
1290
}
1266
1291
1267
1292
i += dataLen;
1293
+
1294
+ // If somehow we don't have 4 bytes left to read, then exit with -1
1295
+ if ((i + 4 ) >= source.size ())
1296
+ return -1 ;
1297
+
1268
1298
auto chunkSize = fourBytesToInt (source, i, endianness);
1269
1299
i += (dataLen + chunkSize);
1270
1300
}
0 commit comments