patch-sfcommands_sfconvert_c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. --- audiofile-0.3.6.orig/sfcommands/sfconvert.c 2013-03-06 06:30:03.000000000 +0100
  2. +++ audiofile-0.3.6/sfcommands/sfconvert.c 2017-04-26 02:15:11.864252147 +0200
  3. @@ -45,6 +45,33 @@ void printusage (void);
  4. void usageerror (void);
  5. bool copyaudiodata (AFfilehandle infile, AFfilehandle outfile, int trackid);
  6. +int firstBitSet(int x)
  7. +{
  8. + int position=0;
  9. + while (x!=0)
  10. + {
  11. + x>>=1;
  12. + ++position;
  13. + }
  14. + return position;
  15. +}
  16. +
  17. +#ifndef __has_builtin
  18. +#define __has_builtin(x) 0
  19. +#endif
  20. +
  21. +int multiplyCheckOverflow(int a, int b, int *result)
  22. +{
  23. +#if (defined __GNUC__ && __GNUC__ >= 5) || ( __clang__ && __has_builtin(__builtin_mul_overflow))
  24. + return __builtin_mul_overflow(a, b, result);
  25. +#else
  26. + if (firstBitSet(a)+firstBitSet(b)>31) // int is signed, so we can't use 32 bits
  27. + return true;
  28. + *result = a * b;
  29. + return false;
  30. +#endif
  31. +}
  32. +
  33. int main (int argc, char **argv)
  34. {
  35. if (argc == 2)
  36. @@ -323,8 +350,11 @@ bool copyaudiodata (AFfilehandle infile,
  37. {
  38. int frameSize = afGetVirtualFrameSize(infile, trackid, 1);
  39. - const int kBufferFrameCount = 65536;
  40. - void *buffer = malloc(kBufferFrameCount * frameSize);
  41. + int kBufferFrameCount = 65536;
  42. + int bufferSize;
  43. + while (multiplyCheckOverflow(kBufferFrameCount, frameSize, &bufferSize))
  44. + kBufferFrameCount /= 2;
  45. + void *buffer = malloc(bufferSize);
  46. AFframecount totalFrames = afGetFrameCount(infile, AF_DEFAULT_TRACK);
  47. AFframecount totalFramesWritten = 0;