You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.7 KiB

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