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.
This commit is contained in:
John Regan 2022-07-07 10:54:23 -04:00
parent 5cc33e5af7
commit ac021570a3
1 changed files with 11 additions and 3 deletions

View File

@ -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);
}
}