Skip to content

Commit 37e33f4

Browse files
authored
VPLAY-11795 memset on objects is potentially harmful
VPLAY-11795 memset on objects is potentially harmful Reason for Change: Eliminates undefined behavior from memset on non-POD types and apply RAII principles to POD type initialization. Non-POD fixes (prevents corruption/crashes): - CachedFragment: Use Clear() method instead of memset - SpeedCache: Use assignment operator for reset - MediaStreamContext: Initialize in member initializer list - AampLLDashServiceData: Refactor clear() to use assignment (DRY) - RecordingComponent: Use assignment in reset methods and constructor - Remove aesCtrAttrDataList memset (already initialized) POD improvements (RAII compliance): - Local arrays: Use = {} initialization (work, dataDescTags, tuneStrPrefix, description, moneytracebuf, buf) - Member arrays: Add m_SPS, m_PPS to member initializer list - TileInfo: Remove redundant memset Code cleanup: - Remove unused SetLLDashSpeedCache() and refactor test
1 parent 71f8c40 commit 37e33f4

File tree

8 files changed

+68
-107
lines changed

8 files changed

+68
-107
lines changed

AampLLDASHData.h

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,51 +29,23 @@
2929
* @brief To store Low Latency Service configurations
3030
*/
3131
struct AampLLDashServiceData {
32-
bool lowLatencyMode; /**< LL Playback mode enabled */
33-
bool strictSpecConformance; /**< Check for Strict LL Dash spec conformance*/
34-
double availabilityTimeOffset; /**< LL Availability Time Offset */
35-
bool availabilityTimeComplete; /**< LL Availability Time Complete */
36-
int targetLatency; /**< Target Latency of playback */
37-
int minLatency; /**< Minimum Latency of playback */
38-
int maxLatency; /**< Maximum Latency of playback */
39-
int latencyThreshold; /**< Latency when play rate correction kicks-in */
40-
double minPlaybackRate; /**< Minimum playback rate for playback */
41-
double maxPlaybackRate; /**< Maximum playback rate for playback */
42-
bool isSegTimeLineBased; /**< Indicates is stream is segmenttimeline based */
43-
double fragmentDuration; /**< Maximum Fragment Duration */
44-
UtcTiming utcTiming; /**< Server UTC timings */
32+
bool lowLatencyMode = false; /**< LL Playback mode enabled */
33+
bool strictSpecConformance = false; /**< Check for Strict LL Dash spec conformance*/
34+
double availabilityTimeOffset = 0.0; /**< LL Availability Time Offset */
35+
bool availabilityTimeComplete = false; /**< LL Availability Time Complete */
36+
int targetLatency = 0; /**< Target Latency of playback */
37+
int minLatency = 0; /**< Minimum Latency of playback */
38+
int maxLatency = 0; /**< Maximum Latency of playback */
39+
int latencyThreshold = 0; /**< Latency when play rate correction kicks-in */
40+
double minPlaybackRate = 0.0; /**< Minimum playback rate for playback */
41+
double maxPlaybackRate = 0.0; /**< Maximum playback rate for playback */
42+
bool isSegTimeLineBased = false; /**< Indicates is stream is segmenttimeline based */
43+
double fragmentDuration = 0.0; /**< Maximum Fragment Duration */
44+
UtcTiming utcTiming = eUTC_HTTP_INVALID; /**< Server UTC timings */
4545

46-
AampLLDashServiceData() : lowLatencyMode(false),
47-
strictSpecConformance(false),
48-
availabilityTimeOffset(0.0),
49-
availabilityTimeComplete(false),
50-
targetLatency(0),
51-
minLatency(0),
52-
maxLatency(0),
53-
latencyThreshold(0),
54-
minPlaybackRate(0.0),
55-
maxPlaybackRate(0.0),
56-
isSegTimeLineBased(false),
57-
fragmentDuration(0.0),
58-
utcTiming(eUTC_HTTP_INVALID)
59-
{
60-
}
61-
/**< API to reset the Data*/
62-
void clear(void)
46+
void clear()
6347
{
64-
lowLatencyMode =false;
65-
strictSpecConformance = false;
66-
availabilityTimeOffset = 0.0;
67-
availabilityTimeComplete = false;
68-
targetLatency = 0;
69-
minLatency = 0;
70-
maxLatency = 0;
71-
latencyThreshold = 0;
72-
minPlaybackRate = 0.0;
73-
maxPlaybackRate = 0.0;
74-
isSegTimeLineBased = false;
75-
fragmentDuration = 0.0;
76-
utcTiming = eUTC_HTTP_INVALID;
48+
*this = {};
7749
}
7850
};
7951

fragmentcollector_mpd.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,14 @@ StreamAbstractionAAMP_MPD::StreamAbstractionAAMP_MPD(class PrivateInstanceAAMP *
159159
,mIsSegmentTimelineEnabled(false)
160160
,mSeekedInPeriod(false)
161161
,mIsFinalFirstPTS(false)
162+
,mMediaStreamContext{}
162163
{
163164
this->aamp = aamp;
164165
if (aamp->mDRMLicenseManager)
165166
{
166167
AampDRMLicenseManager *licenseManager = aamp->mDRMLicenseManager;
167168
licenseManager->SetLicenseFetcher(this);
168169
}
169-
memset(&mMediaStreamContext, 0, sizeof(mMediaStreamContext));
170170
GetABRManager().clearProfiles();
171171
mLastPlaylistDownloadTimeMs = aamp_GetCurrentTimeMS();
172172

@@ -10199,7 +10199,7 @@ StreamAbstractionAAMP_MPD::~StreamAbstractionAAMP_MPD()
1019910199
}
1020010200

1020110201
aamp->CurlTerm(eCURLINSTANCE_VIDEO, DEFAULT_CURL_INSTANCE_COUNT);
10202-
memset(aamp->GetLLDashServiceData(),0x00,sizeof(AampLLDashServiceData));
10202+
aamp->GetLLDashServiceData()->clear();
1020310203
aamp->SetLowLatencyServiceConfigured(false);
1020410204
aamp->SyncEnd();
1020510205
mManifestDnldRespPtr = nullptr;
@@ -10983,7 +10983,6 @@ static void indexThumbnails(dash::mpd::IMPD *mpd, int thumbIndexValue, std::vect
1098310983
{
1098410984
std::string tmedia = media;
1098510985
TileInfo tileInfo;
10986-
memset( &tileInfo,0,sizeof(tileInfo) );
1098710986
tileInfo.startTime = startTime + ( adDuration / timeScale) ;
1098810987
AAMPLOG_TRACE("timeLineIndex[%d] size [%zu] updated durationMs[%" PRIu64 "] startTime:%f adDuration:%f repeatCount:%d", timeLineIndex, timelines.size(), durationMs, startTime, adDuration, repeatCount);
1098910988

@@ -11031,7 +11030,6 @@ static void indexThumbnails(dash::mpd::IMPD *mpd, int thumbIndexValue, std::vect
1103111030
{
1103211031
std::string tmedia = media;
1103311032
TileInfo tileInfo;
11034-
memset( &tileInfo,0,sizeof(tileInfo));
1103511033
tileInfo.startTime = tStartTime;
1103611034
tStartTime += tDuration; //increment the nextStartTime by TileDuration
1103711035
replace(tmedia,"RepresentationID",RepresentationID);
@@ -12595,10 +12593,9 @@ double StreamAbstractionAAMP_MPD::GetEncoderDisplayLatency()
1259512593
{
1259612594
AAMPLOG_TRACE("ProducerReferenceTime@wallClockTime [%s]", wallClockTime.c_str());
1259712595

12598-
std::tm tmTime;
12596+
std::tm tmTime{};
1259912597
const char* format = "%Y-%m-%dT%H:%M:%S.%f%Z";
1260012598
char out_buffer[ 80 ];
12601-
memset(&tmTime, 0, sizeof(tmTime));
1260212599
strptime(wallClockTime.c_str(), format, &tmTime);
1260312600
wTime = mktime(&tmTime);
1260412601

priv_aamp.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ int PrivateInstanceAAMP::HandleSSLProgressCallback ( void *clientp, double dltot
979979
//Reset speedcache when Fragment download Starts
980980
struct SpeedCache* speedcache = NULL;
981981
speedcache = aamp->GetLLDashSpeedCache();
982-
memset(speedcache, 0x00, sizeof(struct SpeedCache));
982+
*speedcache = SpeedCache();
983983
}
984984

985985
downloadbps = getCurrentContentDownloadSpeed(aamp, context->mediaType, context->dlStarted, (long)context->downloadStartTime, dlnow);
@@ -1375,7 +1375,6 @@ PrivateInstanceAAMP::PrivateInstanceAAMP(AampConfig *config) : mReportProgressPo
13751375
mCustomHeaders["Connection:"] = std::vector<std::string> { "Keep-Alive" };
13761376
preferredLanguagesList.push_back("en");
13771377

1378-
memset(&aesCtrAttrDataList, 0, sizeof(aesCtrAttrDataList));
13791378
mHarvestCountLimit = GETCONFIGVALUE_PRIV(eAAMPConfig_HarvestCountLimit);
13801379
mHarvestConfig = GETCONFIGVALUE_PRIV(eAAMPConfig_HarvestConfig);
13811380
mAsyncTuneEnabled = ISCONFIGSET_PRIV(eAAMPConfig_AsyncTune);
@@ -6171,9 +6170,8 @@ void PrivateInstanceAAMP::Tune(const char *mainManifestUrl,
61716170
mIsFirstRequestToFOG = (mFogTSBEnabled == true);
61726171

61736172
{
6174-
char tuneStrPrefix[64];
6173+
char tuneStrPrefix[64] = {};
61756174
mTsbSessionRequestUrl.clear();
6176-
memset(tuneStrPrefix, '\0', sizeof(tuneStrPrefix));
61776175
if (!mAppName.empty())
61786176
{
61796177
snprintf(tuneStrPrefix, sizeof(tuneStrPrefix), "%s PLAYER[%d] APP: %s",(mbPlayEnabled?STRFGPLAYER:STRBGPLAYER), mPlayerId, mAppName.c_str());
@@ -8990,8 +8988,7 @@ void PrivateInstanceAAMP::UpdateLiveOffset()
89908988
*/
89918989
void PrivateInstanceAAMP::SendStalledErrorEvent()
89928990
{
8993-
char description[MAX_ERROR_DESCRIPTION_LENGTH];
8994-
memset(description, '\0', MAX_ERROR_DESCRIPTION_LENGTH);
8991+
char description[MAX_ERROR_DESCRIPTION_LENGTH] = {};
89958992
int stalltimeout = GETCONFIGVALUE_PRIV(eAAMPConfig_StallTimeoutMS);
89968993
snprintf(description, (MAX_ERROR_DESCRIPTION_LENGTH - 1), "Playback has been stalled for more than %d ms due to lack of new fragments", stalltimeout);
89978994
SendErrorEvent(AAMP_TUNE_PLAYBACK_STALLED, description);
@@ -9128,8 +9125,7 @@ MediaFormat PrivateInstanceAAMP::GetMediaFormatTypeEnum() const
91289125
*/
91299126
void PrivateInstanceAAMP::GetMoneyTraceString(std::string &customHeader) const
91309127
{
9131-
char moneytracebuf[512];
9132-
memset(moneytracebuf, 0, sizeof(moneytracebuf));
9128+
char moneytracebuf[512] = {};
91339129

91349130
if (mCustomHeaders.size() > 0)
91359131
{
@@ -12806,8 +12802,7 @@ struct curl_slist* PrivateInstanceAAMP::GetCustomHeaders(AampMediaType mediaType
1280612802
{
1280712803
continue;
1280812804
}
12809-
char buf[512];
12810-
memset(buf, '\0', 512);
12805+
char buf[512] = {};
1281112806
if (it->second.size() >= 2)
1281212807
{
1281312808
snprintf(buf, 512, "trace-id=%s;parent-id=%s;span-id=%lld",
@@ -13109,14 +13104,6 @@ uint32_t PrivateInstanceAAMP::GetSubTimeScale(void)
1310913104
return subTimeScale;
1311013105
}
1311113106

13112-
/**
13113-
* @brief Sets Speed Cache
13114-
*/
13115-
void PrivateInstanceAAMP::SetLLDashSpeedCache(struct SpeedCache &speedCache)
13116-
{
13117-
this->speedCache = speedCache;
13118-
}
13119-
1312013107
/**
1312113108
* @brief Gets Speed Cache
1312213109
*/

priv_aamp.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3534,14 +3534,6 @@ class PrivateInstanceAAMP : public DrmCallbacks, public std::enable_shared_from_
35343534
*/
35353535
uint32_t GetSubTimeScale(void);
35363536

3537-
/**
3538-
* @fn SetLLDashSpeedCache
3539-
*
3540-
* @param[in] speedCache - Speed Cache
3541-
* @return void
3542-
*/
3543-
void SetLLDashSpeedCache(struct SpeedCache &speedCache);
3544-
35453537
/**
35463538
* @fn GetLLDashSpeedCache
35473539
*

streamabstraction.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,8 +1994,7 @@ void MediaTrack::FlushFragments()
19941994
{
19951995
for (int i = 0; i < maxCachedFragmentsPerTrack; i++)
19961996
{
1997-
mCachedFragment[i].fragment.Free();
1998-
memset(&mCachedFragment[i], 0, sizeof(CachedFragment));
1997+
mCachedFragment[i].Clear();
19991998
}
20001999
fragmentIdxToInject = 0;
20012000
fragmentIdxToFetch = 0;

test/utests/tests/PrivAampTests/PrivAampTests.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3791,12 +3791,35 @@ TEST_F(PrivAampTests,SetAudTimeScaleTest)
37913791
EXPECT_EQ(val,val1);
37923792
}
37933793

3794-
TEST_F(PrivAampTests,SetLLDashSpeedCacheTest)
3794+
TEST_F(PrivAampTests,GetLLDashSpeedCacheTest)
37953795
{
3796-
struct SpeedCache spCache;
3797-
p_aamp->SetLLDashSpeedCache(spCache);
3798-
3799-
struct SpeedCache *pCache1 = p_aamp->GetLLDashSpeedCache();
3796+
// Test that GetLLDashSpeedCache returns a valid pointer and can be modified
3797+
struct SpeedCache *pCache = p_aamp->GetLLDashSpeedCache();
3798+
3799+
ASSERT_NE(pCache, nullptr);
3800+
3801+
pCache->last_sample_time_val = 12345;
3802+
pCache->speed_now = 5000000; // 5 Mbps
3803+
pCache->totalDownloaded = 1024000;
3804+
pCache->bStart = true;
3805+
pCache->mChunkSpeedData.push_back(std::make_pair(1.5, 4000000));
3806+
3807+
struct SpeedCache *pCache2 = p_aamp->GetLLDashSpeedCache();
3808+
EXPECT_EQ(pCache2->last_sample_time_val, 12345);
3809+
EXPECT_EQ(pCache2->speed_now, 5000000);
3810+
EXPECT_EQ(pCache2->totalDownloaded, 1024000);
3811+
EXPECT_TRUE(pCache2->bStart);
3812+
EXPECT_EQ(pCache2->mChunkSpeedData.size(), 1);
3813+
3814+
// Test resetting the cache
3815+
*pCache = SpeedCache();
3816+
3817+
// Verify reset worked
3818+
EXPECT_EQ(pCache->last_sample_time_val, 0);
3819+
EXPECT_EQ(pCache->speed_now, 0);
3820+
EXPECT_EQ(pCache->totalDownloaded, 0);
3821+
EXPECT_FALSE(pCache->bStart);
3822+
EXPECT_TRUE(pCache->mChunkSpeedData.empty());
38003823
}
38013824

38023825
TEST_F(PrivAampTests,SetLiveOffsetAppRequestTest)

tsFragmentProcessor.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -623,14 +623,12 @@ void TSFragmentProcessor::DemuxFragment(const uint8_t * base_packet_ptr, size_t
623623
void TSFragmentProcessor::ProcessPMTSection(uint8_t * section, size_t sectionLength)
624624
{
625625
unsigned char *programInfo, *programInfoEnd;
626-
char work[32];
626+
char work[32] = {};
627627

628628
int version = ((section[2] >> 1) & 0x1F);
629629
int pcrPid = (((section[5] & 0x1F) << 8) + section[6]);
630630
int infoLength = (((section[7] & 0x0F) << 8) + section[8]);
631631

632-
memset(work, 0, sizeof(work));
633-
634632
// Reset of old values
635633
ResetAudioComponents();
636634
ResetVideoComponents();
@@ -817,15 +815,13 @@ void TSFragmentProcessor::ProcessPMTSection(uint8_t * section, size_t sectionLen
817815

818816
void TSFragmentProcessor::ResetAudioComponents()
819817
{
820-
// Reset of old values
821818
for (auto & comp : m_audioComponents)
822819
{
823820
if (comp.associatedLanguage)
824821
{
825822
free(comp.associatedLanguage);
826823
}
827-
comp.associatedLanguage = nullptr;
828-
memset(&comp, 0, sizeof(RecordingComponent));
824+
comp = RecordingComponent();
829825
}
830826

831827
m_audioComponentCount = 0;
@@ -835,7 +831,7 @@ void TSFragmentProcessor::ResetVideoComponents()
835831
{
836832
for (auto & comp : m_videoComponents)
837833
{
838-
memset(&comp, 0, sizeof(RecordingComponent));
834+
comp = RecordingComponent();
839835
}
840836
m_videoComponentCount = 0;
841837
}

tsprocessor.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ TSProcessor::TSProcessor(class PrivateInstanceAAMP *aamp,StreamOperation streamO
196196
, m_auxiliaryAudio(false)
197197
,m_audioGroupId()
198198
,m_applyOffset(true)
199+
,m_SPS{}
200+
,m_PPS{}
199201
{
200202
AAMPLOG_INFO(" constructor: %p", this);
201203
bool optimizeMuxed = false;
@@ -205,8 +207,6 @@ TSProcessor::TSProcessor(class PrivateInstanceAAMP *aamp,StreamOperation streamO
205207
optimizeMuxed = (m_streamOperation == eStreamOp_DEMUX_ALL);
206208
}
207209

208-
memset(m_SPS, 0, 32 * sizeof(H264SPS));
209-
memset(m_PPS, 0, 256 * sizeof(H264PPS));
210210
m_versionPMT = 0;
211211

212212
if ((m_streamOperation == eStreamOp_DEMUX_ALL) || (m_streamOperation == eStreamOp_DEMUX_VIDEO) || (m_streamOperation == eStreamOp_DEMUX_VIDEO_AND_AUX))
@@ -237,10 +237,6 @@ TSProcessor::TSProcessor(class PrivateInstanceAAMP *aamp,StreamOperation streamO
237237
}
238238
m_demux = true;
239239
}
240-
241-
int compBufLen = MAX_PIDS*sizeof(RecordingComponent);
242-
memset(videoComponents, 0, compBufLen);
243-
memset(audioComponents, 0, compBufLen);
244240
}
245241

246242
/**
@@ -395,9 +391,9 @@ void TSProcessor::insertPCR(unsigned char *packet, int pid)
395391
void TSProcessor::processPMTSection(unsigned char* section, int sectionLength)
396392
{
397393
unsigned char *programInfo, *programInfoEnd;
398-
unsigned int dataDescTags[MAX_PIDS];
394+
unsigned int dataDescTags[MAX_PIDS] = {};
399395
int streamType = 0, pid = 0, len = 0;
400-
char work[32];
396+
char work[32] = {};
401397
StreamOutputFormat videoFormat = FORMAT_INVALID;
402398
StreamOutputFormat audioFormat = FORMAT_INVALID;
403399
bool cueiDescriptorFound = false;
@@ -406,21 +402,20 @@ void TSProcessor::processPMTSection(unsigned char* section, int sectionLength)
406402
int pcrPid = (((section[5] & 0x1F) << 8) + section[6]);
407403
int infoLength = (((section[7] & 0x0F) << 8) + section[8]);
408404

409-
for (int i = 0; i < audioComponentCount; ++i)
405+
for (auto & comp : videoComponents)
410406
{
411-
if (audioComponents[i].associatedLanguage)
407+
comp = RecordingComponent();
408+
}
409+
410+
for (auto & comp : audioComponents)
411+
{
412+
if (comp.associatedLanguage)
412413
{
413-
free(audioComponents[i].associatedLanguage);
414+
free(comp.associatedLanguage);
414415
}
416+
comp = RecordingComponent();
415417
}
416418

417-
memset(videoComponents, 0, sizeof(videoComponents));
418-
memset(audioComponents, 0, sizeof(audioComponents));
419-
420-
memset(dataDescTags, 0, sizeof(dataDescTags));
421-
422-
memset(work, 0, sizeof(work));
423-
424419
videoComponentCount = audioComponentCount = 0;
425420
m_dsmccComponentFound = false;
426421

0 commit comments

Comments
 (0)