Skip to content

Commit c88081b

Browse files
Sid2001-makersid-TEL
authored andcommitted
RDKEMW-12252: Coverity Scan Report - Analyzing and Fixing all the Critical and High issues
Reason for change: Resolve Critical and high level issues in coverity Test Procedure: build should be successful Risk: low Priority: P2
1 parent 96c54ce commit c88081b

11 files changed

Lines changed: 237 additions & 107 deletions

include/IJavaScriptContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
class IJavaScriptContext
2626
{
2727
public:
28+
virtual ~IJavaScriptContext() = default;
2829
virtual bool runScript(const char *script, bool isModule=true, std::string name="", const char *args = nullptr, bool isApplication=false) = 0;
2930
virtual bool runFile(const char *file, const char* args, bool isApplication=false) = 0;
3031
virtual std::string getUrl() = 0;

include/JSRuntimeClient.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,23 @@ template <typename Derived>
4040
class CommandInterface
4141
{
4242
public:
43-
CommandInterface() = default;
43+
CommandInterface() : mResponseReceived(false) {}
4444

4545
bool sendCommand(std::string command, std::string &response)
4646
{
4747
Derived &derived = static_cast<Derived &>(*this);
4848
if (derived.send(command))
4949
{
5050
std::unique_lock<std::mutex> lock(mResponseMutex);
51-
mResponseCondition.wait_for(lock, std::chrono::seconds(5));
52-
response = mLastResponse;
53-
return true;
51+
mResponseReceived = false;
52+
bool gotResponse = mResponseCondition.wait_for(lock, std::chrono::seconds(5),
53+
[this]() { return mResponseReceived; });
54+
55+
if (gotResponse)
56+
{
57+
response = mLastResponse;
58+
return true;
59+
}
5460
}
5561

5662
return false;
@@ -63,6 +69,7 @@ class CommandInterface
6369
{
6470
std::lock_guard<std::mutex> lock(mResponseMutex);
6571
mLastResponse = message;
72+
mResponseReceived = true;
6673
mResponseCondition.notify_one();
6774
}
6875

@@ -71,6 +78,7 @@ class CommandInterface
7178
CommandInterface &operator=(const CommandInterface &) = delete;
7279

7380
std::string mLastResponse;
81+
bool mResponseReceived;
7482
std::mutex mResponseMutex;
7583
std::condition_variable mResponseCondition;
7684
};

src/JSRuntimeClient.cpp

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ bool JSRuntimeClient::run()
7676
t.detach();
7777

7878
std::unique_lock<std::mutex> lock(mStateMutex);
79-
mStateCondition.wait_for(lock, std::chrono::seconds(5));
79+
mStateCondition.wait_for(lock, std::chrono::seconds(5), [this]() {
80+
return mState != "none";
81+
});
8082
return mState == "open";
8183
}
8284

@@ -152,37 +154,45 @@ void JSRuntimeClient::onClose(websocketpp::connection_hdl hdl)
152154
#ifndef UNIT_TEST_BUILD
153155
int main(int argc, char **argv)
154156
{
155-
std::string command;
156-
std::string response;
157+
try {
158+
std::string command;
159+
std::string response;
157160

158-
if (argc > 1)
159-
{
160-
NativeJSLogger::log(INFO, "Send input commands at ws://localhost:%s\n", std::to_string(WS_SERVER_PORT).c_str());
161-
return -1;
162-
}
163-
164-
JSRuntimeClient *client = JSRuntimeClient::getInstance();
165-
client->initialize(WS_SERVER_PORT);
166-
if (!client->run())
167-
{
168-
NativeJSLogger::log(ERROR, "Unable to connect to server\n");
169-
return -1;
170-
}
161+
if (argc > 1)
162+
{
163+
NativeJSLogger::log(INFO, "Send input commands at ws://localhost:%s\n", std::to_string(WS_SERVER_PORT).c_str());
164+
return -1;
165+
}
171166

172-
while (client->getState() == "open" && std::getline(std::cin, command))
173-
{
174-
client->sendCommand(command, response);
175-
if (!response.empty())
167+
JSRuntimeClient *client = JSRuntimeClient::getInstance();
168+
client->initialize(WS_SERVER_PORT);
169+
if (!client->run())
176170
{
177-
NativeJSLogger::log(INFO, "Response: %s\n", response.c_str());
171+
NativeJSLogger::log(ERROR, "Unable to connect to server\n");
172+
return -1;
178173
}
179-
else
174+
175+
while (client->getState() == "open" && std::getline(std::cin, command))
180176
{
181-
NativeJSLogger::log(WARN, "Missing response\n");
182-
break;
177+
client->sendCommand(command, response);
178+
if (!response.empty())
179+
{
180+
NativeJSLogger::log(INFO, "Response: %s\n", response.c_str());
181+
}
182+
else
183+
{
184+
NativeJSLogger::log(WARN, "Missing response\n");
185+
break;
186+
}
183187
}
184-
}
185188

186-
return 0;
189+
return 0;
190+
} catch (const std::exception& e) {
191+
NativeJSLogger::log(ERROR, "Uncaught exception in main: %s\n", e.what());
192+
return -1;
193+
} catch (...) {
194+
NativeJSLogger::log(ERROR, "Unknown exception caught in main\n");
195+
return -1;
196+
}
187197
}
188198
#endif

src/JSRuntimeClientContainer.cpp

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,36 @@
66
#include "NativeJSLogger.h"
77
int main()
88
{
9-
std::string containerId = "com.sky.as.apps_TestApp";
10-
const std::string basePath = "/opt/twocontext"; // constant base path
11-
const std::vector<std::string> apps = {"app1", "app2"};
12-
13-
std::string ipAddress = JSRuntimeContainer::getContainerIpAddress(containerId);
14-
if (ipAddress.empty()) {
15-
NativeJSLogger::log(ERROR, "Failed to retrieve IP address for container");
16-
return 1;
17-
}
9+
try {
10+
std::string containerId = "com.sky.as.apps_TestApp";
11+
const std::string basePath = "/opt/twocontext"; // constant base path
12+
const std::vector<std::string> apps = {"app1", "app2"};
13+
14+
std::string ipAddress = JSRuntimeContainer::getContainerIpAddress(containerId);
15+
if (ipAddress.empty()) {
16+
NativeJSLogger::log(ERROR, "Failed to retrieve IP address for container\n");
17+
return 1;
18+
}
1819

19-
for (const auto &app : apps) {
20-
std::string url = basePath + std::string("/") + app + std::string("/index.html");
21-
if (access(url.c_str(), F_OK) == 0) {
22-
std::string pathAppConfig = basePath + std::string("/") + app + std::string("/app.config");
23-
std::string options = JSRuntimeContainer::parseAppConfig(pathAppConfig);
24-
std::string message = JSRuntimeContainer::buildLaunchMessage(url, options);
25-
JSRuntimeContainer::connectAndSend(ipAddress, message);
20+
for (const auto &app : apps) {
21+
std::string url = basePath + std::string("/") + app + std::string("/index.html");
22+
if (access(url.c_str(), F_OK) == 0) {
23+
std::string pathAppConfig = basePath + std::string("/") + app + std::string("/app.config");
24+
std::string options = JSRuntimeContainer::parseAppConfig(pathAppConfig);
25+
std::string message = JSRuntimeContainer::buildLaunchMessage(url, options);
26+
JSRuntimeContainer::connectAndSend(ipAddress, message);
27+
}
2628
}
27-
}
2829

29-
return 0;
30+
return 0;
31+
}
32+
catch (const std::exception& e) {
33+
NativeJSLogger::log(ERROR, "Exception in main: %s\n", e.what());
34+
return 1;
35+
}
36+
catch (...) {
37+
NativeJSLogger::log(ERROR, "Unknown exception in main\n");
38+
return 1;
39+
}
3040
}
3141

src/JSRuntimeServer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class JsonWrap
8585
if (!itm || !cJSON_IsNumber(itm))
8686
{
8787
std::cerr << "Error: " << name << "is not a Uint32_t" << std::endl;
88+
res=0;
8889
err = true;
8990
}
9091
else

src/NativeJSRenderer.cpp

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,8 @@ void NativeJSRenderer::setEnvForConsoleMode(ModuleSettings& moduleSettings)
225225

226226
uint32_t NativeJSRenderer::createApplicationIdentifier()
227227
{
228-
static uint32_t id = 1;
229-
uint32_t ret = id;
230-
id++;
231-
return ret;
228+
static std::atomic<uint32_t> id{1};
229+
return id++;
232230
}
233231

234232
uint32_t NativeJSRenderer::createApplication(ModuleSettings& moduleSettings, std::string userAgent)
@@ -292,6 +290,7 @@ bool NativeJSRenderer::terminateApplication(uint32_t id)
292290
return true;
293291
}
294292

293+
// REQUIRES: Caller must hold mUserMutex
295294
void NativeJSRenderer::createApplicationInternal(ApplicationRequest& appRequest)
296295
{
297296
double startTime = getTimeInMilliSec();
@@ -330,9 +329,9 @@ void NativeJSRenderer::createApplicationInternal(ApplicationRequest& appRequest)
330329
context->setCreateApplicationEndTime(endTime, id);
331330

332331
mContextMap[id].context=context;
333-
mUserMutex.unlock();
334332
}
335333

334+
// REQUIRES: Caller must hold mUserMutex
336335
void NativeJSRenderer::runApplicationInternal(ApplicationRequest& appRequest)
337336
{
338337
uint32_t id = appRequest.mId;
@@ -365,7 +364,7 @@ void NativeJSRenderer::runApplicationInternal(ApplicationRequest& appRequest)
365364
NativeJSLogger::log(INFO, "Adding the window location: %s to js file\n", window.str().c_str());
366365
context->runScript(window.str().c_str(),true, url, nullptr, true);
367366
}
368-
NativeJSLogger::log(INFO, "nativeJS application thunder execution url: %s, result: %d\n", url.c_str(), ret ? 1 : 0);
367+
NativeJSLogger::log(INFO, "nativeJS application thunder execution url: %s\n", url.c_str());
369368
ret = context->runScript(chunk.contentsBuffer, true, url, nullptr, true);
370369
NativeJSLogger::log(INFO, "nativeJS application execution result: %d\n", ret ? 1 : 0);
371370
double duration = context->getExecutionDuration();
@@ -398,6 +397,7 @@ void NativeJSRenderer::runApplicationInternal(ApplicationRequest& appRequest)
398397
}
399398
}
400399

400+
// REQUIRES: Caller must hold mUserMutex
401401
void NativeJSRenderer::runJavaScriptInternal(ApplicationRequest& appRequest)
402402
{
403403
uint32_t id = appRequest.mId;
@@ -427,6 +427,7 @@ void NativeJSRenderer::runJavaScriptInternal(ApplicationRequest& appRequest)
427427
}
428428
}
429429

430+
// REQUIRES: Caller must hold mUserMutex
430431
void NativeJSRenderer::terminateApplicationInternal(ApplicationRequest& AppRequest)
431432
{
432433
uint32_t id = AppRequest.mId;
@@ -445,7 +446,7 @@ void NativeJSRenderer::terminateApplicationInternal(ApplicationRequest& AppReque
445446

446447
else
447448
{
448-
NativeJSLogger::log(ERROR, "Unable to find application with id: %d and url: %s\n", id, mContextMap[id].url);
449+
NativeJSLogger::log(ERROR, "Unable to find application with id: %d\n", id);
449450
return ;
450451
}
451452

@@ -468,11 +469,10 @@ void NativeJSRenderer::run()
468469
{
469470
while(mRunning)
470471
{
471-
uint32_t id;
472-
mUserMutex.lock();
473472
if (mConsoleMode) {
474473
processDevConsoleRequests();
475474
}
475+
mUserMutex.lock();
476476
for (int i=0; i<gPendingRequests.size(); i++)
477477
{
478478

@@ -506,10 +506,13 @@ void NativeJSRenderer::run()
506506
if(!mTestFileName.empty())
507507
{
508508
ModuleSettings settings;
509+
uint32_t id = createApplicationIdentifier();
509510
settings.enableJSDOM = mEnableTestFileDOMSupport;
510511
ApplicationRequest appRequest(id, RUN, mTestFileName, settings.enableHttp, settings.enableXHR, settings.enableWebSocket, settings.enableWebSocketEnhanced, settings.enableFetch, settings.enableJSDOM, settings.enableWindow, settings.enablePlayer);
512+
mUserMutex.lock();
511513
NativeJSRenderer::createApplicationInternal(appRequest);
512514
NativeJSRenderer::runApplicationInternal(appRequest);
515+
mUserMutex.unlock();
513516
mTestFileName = "";
514517
}
515518

@@ -531,27 +534,30 @@ void NativeJSRenderer::run()
531534

532535
void NativeJSRenderer::processDevConsoleRequests()
533536
{
537+
std::deque<std::string> localQueue;
538+
539+
// Move items from shared queue to local queue while holding the lock
534540
mConsoleState->inputMutex.lock();
535-
536541
if (mConsoleState->codeToExecute.empty()) {
537542
mConsoleState->inputMutex.unlock();
538543
return;
539544
}
545+
localQueue.swap(mConsoleState->codeToExecute);
546+
mConsoleState->inputMutex.unlock();
540547

541548
std::lock_guard<std::mutex> lockg(mConsoleState->isProcessing_cv_m);
542549
bool dataProcessed = false;
543550

544-
for (; !mConsoleState->codeToExecute.empty(); mConsoleState->codeToExecute.pop_front()) {
545-
bool ret = mConsoleState->consoleContext->runScript(mConsoleState->codeToExecute.front().c_str(), false);
551+
// Process items from local queue (no race condition)
552+
for (const auto& code : localQueue) {
553+
bool ret = mConsoleState->consoleContext->runScript(code.c_str(), false);
546554
dataProcessed = true;
547555
}
548556

549557
if (dataProcessed) {
550558
mConsoleState->isProcessing = false;
551559
mConsoleState->isProcessing_cv.notify_one();
552560
}
553-
554-
mConsoleState->inputMutex.unlock();
555561
}
556562

557563
std::atomic_bool NativeJSRenderer::consoleLoop = true;
@@ -620,18 +626,34 @@ bool NativeJSRenderer::downloadFile(std::string& url, MemoryStruct& chunk)
620626
curl = curl_easy_init();
621627
if (curl)
622628
{
623-
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
624-
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
625-
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, HeaderCallback);
626-
curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *)&chunk);
627-
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
628-
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
629-
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
630-
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
631-
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2);
632-
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
633-
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
634-
curl_easy_setopt(curl, CURLOPT_PROXY, "");
629+
// Helper lambda to check curl_easy_setopt results
630+
auto setOptWithCheck = [curl](CURLoption option, auto value, const char* optionName) -> bool {
631+
CURLcode optRes = curl_easy_setopt(curl, option, value);
632+
if (optRes != CURLE_OK) {
633+
NativeJSLogger::log(ERROR, "Failed to set %s: %s\n", optionName, curl_easy_strerror(optRes));
634+
return false;
635+
}
636+
return true;
637+
};
638+
639+
// Set options; cleanup and return if any fail
640+
if (
641+
!setOptWithCheck(CURLOPT_URL, url.c_str(), "CURLOPT_URL") ||
642+
!setOptWithCheck(CURLOPT_FOLLOWLOCATION, 1L, "CURLOPT_FOLLOWLOCATION") ||
643+
!setOptWithCheck(CURLOPT_HEADERFUNCTION, HeaderCallback, "CURLOPT_HEADERFUNCTION") ||
644+
!setOptWithCheck(CURLOPT_HEADERDATA, (void *)&chunk, "CURLOPT_HEADERDATA") ||
645+
!setOptWithCheck(CURLOPT_WRITEFUNCTION, WriteMemoryCallback, "CURLOPT_WRITEFUNCTION") ||
646+
!setOptWithCheck(CURLOPT_WRITEDATA, (void *)&chunk, "CURLOPT_WRITEDATA") ||
647+
!setOptWithCheck(CURLOPT_TIMEOUT, 30L, "CURLOPT_TIMEOUT") ||
648+
!setOptWithCheck(CURLOPT_NOSIGNAL, 1L, "CURLOPT_NOSIGNAL") ||
649+
!setOptWithCheck(CURLOPT_SSL_VERIFYHOST, 2L, "CURLOPT_SSL_VERIFYHOST") ||
650+
!setOptWithCheck(CURLOPT_SSL_VERIFYPEER, 1L, "CURLOPT_SSL_VERIFYPEER") ||
651+
!setOptWithCheck(CURLOPT_USERAGENT, "libcurl-agent/1.0", "CURLOPT_USERAGENT") ||
652+
!setOptWithCheck(CURLOPT_PROXY, "", "CURLOPT_PROXY")
653+
) {
654+
curl_easy_cleanup(curl);
655+
return ret;
656+
}
635657

636658

637659
//curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);

src/jsc/JavaScriptContext.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ if (mModuleSettings.enablePlayer)
119119
gTopLevelContext = nullptr;
120120
}
121121
mPriv->releaseAllProtected();
122+
123+
if (mNetworkMetricsData)
124+
{
125+
delete mNetworkMetricsData;
126+
mNetworkMetricsData = nullptr;
127+
}
128+
122129
JSGlobalContextRelease(mContext);
123130
JSContextGroupRelease(mContextGroup);
124131
rtLogInfo("%s end", __FUNCTION__);
@@ -394,7 +401,7 @@ if (mModuleSettings.enablePlayer)
394401
gAAMPJSBindings = new AAMPJSBindings();
395402
loadAAMPJSBindingsLib();
396403
}
397-
if (gAAMPJSBindings->fnLoadJS)
404+
if (gAAMPJSBindings && gAAMPJSBindings->fnLoadJS)
398405
{
399406
gAAMPJSBindings->fnLoadJS(mContext);
400407
}

0 commit comments

Comments
 (0)