06_Check-for-multiplication-overflow-in-MSADPCM-decodeSam.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. From: Antonio Larrosa <larrosa@kde.org>
  2. Date: Mon, 6 Mar 2017 13:43:53 +0100
  3. Subject: Check for multiplication overflow in MSADPCM decodeSample
  4. Check for multiplication overflow (using __builtin_mul_overflow
  5. if available) in MSADPCM.cpp decodeSample and return an empty
  6. decoded block if an error occurs.
  7. This fixes the 00193-audiofile-signintoverflow-MSADPCM case of #41
  8. ---
  9. libaudiofile/modules/BlockCodec.cpp | 5 ++--
  10. libaudiofile/modules/MSADPCM.cpp | 47 +++++++++++++++++++++++++++++++++----
  11. 2 files changed, 46 insertions(+), 6 deletions(-)
  12. diff --git a/libaudiofile/modules/BlockCodec.cpp b/libaudiofile/modules/BlockCodec.cpp
  13. index 45925e8..4731be1 100644
  14. --- a/libaudiofile/modules/BlockCodec.cpp
  15. +++ b/libaudiofile/modules/BlockCodec.cpp
  16. @@ -52,8 +52,9 @@ void BlockCodec::runPull()
  17. // Decompress into m_outChunk.
  18. for (int i=0; i<blocksRead; i++)
  19. {
  20. - decodeBlock(static_cast<const uint8_t *>(m_inChunk->buffer) + i * m_bytesPerPacket,
  21. - static_cast<int16_t *>(m_outChunk->buffer) + i * m_framesPerPacket * m_track->f.channelCount);
  22. + if (decodeBlock(static_cast<const uint8_t *>(m_inChunk->buffer) + i * m_bytesPerPacket,
  23. + static_cast<int16_t *>(m_outChunk->buffer) + i * m_framesPerPacket * m_track->f.channelCount)==0)
  24. + break;
  25. framesRead += m_framesPerPacket;
  26. }
  27. diff --git a/libaudiofile/modules/MSADPCM.cpp b/libaudiofile/modules/MSADPCM.cpp
  28. index 8ea3c85..ef9c38c 100644
  29. --- a/libaudiofile/modules/MSADPCM.cpp
  30. +++ b/libaudiofile/modules/MSADPCM.cpp
  31. @@ -101,24 +101,60 @@ static const int16_t adaptationTable[] =
  32. 768, 614, 512, 409, 307, 230, 230, 230
  33. };
  34. +int firstBitSet(int x)
  35. +{
  36. + int position=0;
  37. + while (x!=0)
  38. + {
  39. + x>>=1;
  40. + ++position;
  41. + }
  42. + return position;
  43. +}
  44. +
  45. +#ifndef __has_builtin
  46. +#define __has_builtin(x) 0
  47. +#endif
  48. +
  49. +int multiplyCheckOverflow(int a, int b, int *result)
  50. +{
  51. +#if (defined __GNUC__ && __GNUC__ >= 5) || ( __clang__ && __has_builtin(__builtin_mul_overflow))
  52. + return __builtin_mul_overflow(a, b, result);
  53. +#else
  54. + if (firstBitSet(a)+firstBitSet(b)>31) // int is signed, so we can't use 32 bits
  55. + return true;
  56. + *result = a * b;
  57. + return false;
  58. +#endif
  59. +}
  60. +
  61. +
  62. // Compute a linear PCM value from the given differential coded value.
  63. static int16_t decodeSample(ms_adpcm_state &state,
  64. - uint8_t code, const int16_t *coefficient)
  65. + uint8_t code, const int16_t *coefficient, bool *ok=NULL)
  66. {
  67. int linearSample = (state.sample1 * coefficient[0] +
  68. state.sample2 * coefficient[1]) >> 8;
  69. + int delta;
  70. linearSample += ((code & 0x08) ? (code - 0x10) : code) * state.delta;
  71. linearSample = clamp(linearSample, MIN_INT16, MAX_INT16);
  72. - int delta = (state.delta * adaptationTable[code]) >> 8;
  73. + if (multiplyCheckOverflow(state.delta, adaptationTable[code], &delta))
  74. + {
  75. + if (ok) *ok=false;
  76. + _af_error(AF_BAD_COMPRESSION, "Error decoding sample");
  77. + return 0;
  78. + }
  79. + delta >>= 8;
  80. if (delta < 16)
  81. delta = 16;
  82. state.delta = delta;
  83. state.sample2 = state.sample1;
  84. state.sample1 = linearSample;
  85. + if (ok) *ok=true;
  86. return static_cast<int16_t>(linearSample);
  87. }
  88. @@ -212,13 +248,16 @@ int MSADPCM::decodeBlock(const uint8_t *encoded, int16_t *decoded)
  89. {
  90. uint8_t code;
  91. int16_t newSample;
  92. + bool ok;
  93. code = *encoded >> 4;
  94. - newSample = decodeSample(*state[0], code, coefficient[0]);
  95. + newSample = decodeSample(*state[0], code, coefficient[0], &ok);
  96. + if (!ok) return 0;
  97. *decoded++ = newSample;
  98. code = *encoded & 0x0f;
  99. - newSample = decodeSample(*state[1], code, coefficient[1]);
  100. + newSample = decodeSample(*state[1], code, coefficient[1], &ok);
  101. + if (!ok) return 0;
  102. *decoded++ = newSample;
  103. encoded++;