Handle next segment wraparound gracefully
Since global overflow/wraparound sanitization is enabled,
mNextSegSmp can wraparound 32 bits and abort.
In this case, we clamp to UINT_MAX to handle this condition
gracefully. We already use this value as a signal to not
transition to a new segment of tone generation.
Test: Compiles
Bug: 229549191
Change-Id: I2e937e3ea2c43f0f021c48d8ad5cebf2416c5a74
diff --git a/media/libaudioclient/ToneGenerator.cpp b/media/libaudioclient/ToneGenerator.cpp
index dd3f86e..4662247 100644
--- a/media/libaudioclient/ToneGenerator.cpp
+++ b/media/libaudioclient/ToneGenerator.cpp
@@ -1485,8 +1485,11 @@
}
// Update next segment transition position. No harm to do it also for last segment as
- // mNextSegSmp won't be used any more
- mNextSegSmp += (mpToneDesc->segments[mCurSegment].duration * mSamplingRate) / 1000;
+ // mNextSegSmp won't be used any more.
+ // Handle 32 bit wraparound gracefully.
+ const uint64_t res = static_cast<uint64_t>(mNextSegSmp) +
+ (mpToneDesc->segments[mCurSegment].duration * mSamplingRate) / 1000;
+ mNextSegSmp = static_cast<uint32_t>(std::min<uint64_t>(TONEGEN_INF, res));
} else {
// Inside a segment keep tone ON or OFF