From ac021570a31f6dc99222a6b363ec9737d4f0f6bc Mon Sep 17 00:00:00 2001 From: John Regan Date: Thu, 7 Jul 2022 10:54:23 -0400 Subject: [PATCH] fix undefined behavior: unaligned read Casting a type pointer into a larger type pointer requires alignment. This replaces the unportable code with a portable equivalent. --- src/lib/exhaleEnc.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/lib/exhaleEnc.cpp b/src/lib/exhaleEnc.cpp index 4af64d1..6255a66 100644 --- a/src/lib/exhaleEnc.cpp +++ b/src/lib/exhaleEnc.cpp @@ -275,10 +275,18 @@ static inline void applyTnsCoeffPreProcessing (LinearPredictor& predictor, TnsDa if (commonFlag != nullptr) *commonFlag &= (tnsData1.coeffResLow[n] == tnsData2.coeffResLow[n] && tnsData1.filterOrder[n] == tnsData2.filterOrder[n]); if (commonFlag != nullptr && *commonFlag) { - const int32_t* coeff1 = (int32_t*) tnsData1.coeff[n]; // fast - const int32_t* coeff2 = (int32_t*) tnsData2.coeff[n]; // comp + const int32_t coeff1 = (int32_t) + ((uint32_t) tnsData1.coeff[n][0]) | + ((uint32_t) tnsData1.coeff[n][1] << 8) | + ((uint32_t) tnsData1.coeff[n][2] << 16) | + ((uint32_t) tnsData1.coeff[n][3] << 24); + const int32_t coeff2 = (int32_t) + ((uint32_t) tnsData2.coeff[n][0]) | + ((uint32_t) tnsData2.coeff[n][1] << 8) | + ((uint32_t) tnsData2.coeff[n][2] << 16) | + ((uint32_t) tnsData2.coeff[n][3] << 24); - *commonFlag &= (*coeff1 == *coeff2); // might not be portable! + *commonFlag &= (coeff1 == coeff2); } }