diff --git a/sfcommands/sfconvert.c b/sfcommands/sfconvert.c index 80a1bc4..783c37c 100644 --- a/sfcommands/sfconvert.c +++ b/sfcommands/sfconvert.c @@ -24,6 +24,7 @@ sound files. */ +#include #include "config.h" #ifdef __USE_SGI_HEADERS__ @@ -321,16 +322,34 @@ void printversion (void) */ bool copyaudiodata (AFfilehandle infile, AFfilehandle outfile, int trackid) { + const int kBufferFrameCount = 65536; int frameSize = afGetVirtualFrameSize(infile, trackid, 1); + bool success = true; + void *buffer = NULL; - const int kBufferFrameCount = 65536; - void *buffer = malloc(kBufferFrameCount * frameSize); + if (frameSize <= 0) + { + fprintf(stderr, "afGetVirtualFrameSize error! \n"); + return false; + } + + if (frameSize > INT_MAX / kBufferFrameCount) + { + fprintf(stderr, "Prevent integer overflow! \n"); + return false; + } + + buffer = malloc(kBufferFrameCount * frameSize); + + if (buffer == NULL) + { + fprintf(stderr, "allocation of bytes failed! \n"); + return false; + } AFframecount totalFrames = afGetFrameCount(infile, AF_DEFAULT_TRACK); AFframecount totalFramesWritten = 0; - bool success = true; - while (totalFramesWritten < totalFrames) { AFframecount framesToRead = totalFrames - totalFramesWritten;