Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various issues reported by address sanitizer reported from the unit tests #768

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/common/commonutils/CommandUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ int ExecuteCommand(void* context, const char* command, bool replaceEol, bool for
// Read the text result from the output of the command, if any, whether command succeeded or failed
if (NULL != textResult)
{
*textResult = NULL;
resultsFile = fopen(commandTextResultFile, "r");
if (resultsFile)
{
Expand Down Expand Up @@ -339,7 +340,7 @@ int ExecuteCommand(void* context, const char* command, bool replaceEol, bool for
OsConfigLogInfo(log, "Context: '%p'", context);
OsConfigLogInfo(log, "Command: '%s'", command);
OsConfigLogInfo(log, "Status: %d (errno: %d)", status, errno);
OsConfigLogInfo(log, "Text result: '%s'", (NULL != textResult) ? (*textResult) : "");
OsConfigLogInfo(log, "Text result: '%s'", (NULL != textResult && NULL != *textResult) ? (*textResult) : "");
}

return status;
Expand Down Expand Up @@ -377,5 +378,6 @@ char* HashCommand(const char* source, void* log)
OsConfigLogError(log, "HashCommand: out of memory");
}

FREE_MEMORY(command);
return (0 == status) ? hash : NULL;
}
13 changes: 10 additions & 3 deletions src/common/commonutils/DeviceInfoUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void RemovePrefixBlanks(char* target)
i += 1;
}

memcpy(target, target + i, targetLength - i);
memmove(target, target + i, targetLength - i);
target[targetLength - i] = 0;
}

Expand All @@ -43,7 +43,7 @@ void RemovePrefixUpTo(char* target, char marker)
if (equalSign)
{
targetLength = strlen(equalSign + 1);
memcpy(target, equalSign + 1, targetLength);
memmove(target, equalSign + 1, targetLength);
target[targetLength] = 0;
}
}
Expand All @@ -61,7 +61,7 @@ void RemovePrefixUpToString(char* target, const char* marker)
if (equalSign)
{
targetLength = (int)strlen(equalSign);
memcpy(target, equalSign, targetLength);
memmove(target, equalSign, targetLength);
target[targetLength] = 0;
}
}
Expand Down Expand Up @@ -354,6 +354,7 @@ long GetTotalMemory(void* log)
if (NULL != textResult)
{
totalMemory = atol(textResult);
FREE_MEMORY(textResult);
}

if (IsFullLoggingEnabled())
Expand All @@ -373,6 +374,7 @@ long GetFreeMemory(void* log)
if (NULL != textResult)
{
freeMemory = atol(textResult);
FREE_MEMORY(textResult);
}

if (IsFullLoggingEnabled())
Expand All @@ -391,6 +393,7 @@ char* GetProductName(void* log)

if ((NULL == textResult) || (0 == strlen(textResult)))
{
FREE_MEMORY(textResult);
textResult = GetHardwareProperty(osProductNameAlternateCommand, false, log);
}

Expand All @@ -410,6 +413,7 @@ char* GetProductVendor(void* log)

if ((NULL == textResult) || (0 == strlen(textResult)))
{
FREE_MEMORY(textResult);
textResult = GetHardwareProperty(osProductVendorAlternateCommand, false, log);
}

Expand All @@ -429,6 +433,7 @@ char* GetProductVersion(void* log)

if ((NULL == textResult) || (0 == strlen(textResult)))
{
FREE_MEMORY(textResult);
textResult = GetHardwareProperty(osProductVersionAlternateCommand, false, log);
}

Expand Down Expand Up @@ -697,6 +702,8 @@ int CheckLoginUmask(const char* desired, char** reason, void* log)
OsConfigCaptureReason(reason, "Current login UMASK '%s' does not match desired '%s'", current, desired);
status = ENOENT;
}

FREE_MEMORY(current);
}

return status;
Expand Down
11 changes: 7 additions & 4 deletions src/common/commonutils/FileUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ bool ConcatenateFiles(const char* firstFileName, const char* secondFileName, boo

int RestrictFileAccessToCurrentAccountOnly(const char* fileName)
{
if (NULL == fileName)
{
return EINVAL;
}

// S_ISUID (4000): Set user ID on execution
// S_ISGID (2000): Set group ID on execution
// S_IRUSR (0400): Read permission, owner
Expand Down Expand Up @@ -1547,14 +1552,13 @@ static int FindTextInCommandOutput(const char* command, const char* text, void*
status = ENOENT;
OsConfigLogInfo(log, "FindTextInCommandOutput: '%s' not found in '%s' output", text, command);
}

FREE_MEMORY(results);
}
else
{
OsConfigLogInfo(log, "FindTextInCommandOutput: command '%s' failed with %d", command, status);
}

FREE_MEMORY(results);
return status;
}

Expand Down Expand Up @@ -1630,10 +1634,9 @@ char* GetStringOptionFromBuffer(const char* buffer, const char* option, char sep
{
OsConfigLogError(log, "GetStringOptionFromBuffer: failed to duplicate result string (%d)", errno);
}

FREE_MEMORY(internal);
}

FREE_MEMORY(internal);
return result;
}

Expand Down
13 changes: 10 additions & 3 deletions src/common/commonutils/PassUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static char* FindPamModule(const char* pamModule, void* log)
int CheckEnsurePasswordReuseIsLimited(int remember, char** reason, void* log)
{
int status = ENOENT;
char* pamModule = NULL;

if (0 == CheckFileExists(g_etcPamdCommonPassword, NULL, log))
{
Expand All @@ -81,9 +82,13 @@ int CheckEnsurePasswordReuseIsLimited(int remember, char** reason, void* log)
g_etcPamdCommonPassword, g_etcPamdSystemAuth, g_remember);
}

if (status && (false == FindPamModule(g_pamUnixSo, log)))
if (status)
{
OsConfigCaptureReason(reason, "The PAM module '%s' is not available. Automatic remediation is not possible", g_pamUnixSo);
if (NULL == (pamModule = FindPamModule(g_pamUnixSo, log)))
{
OsConfigCaptureReason(reason, "The PAM module '%s' is not available. Automatic remediation is not possible", g_pamUnixSo);
}
FREE_MEMORY(pamModule);
}

return status;
Expand Down Expand Up @@ -219,7 +224,7 @@ int CheckLockoutForFailedPasswordAttempts(const char* fileName, const char* pamS
continue;
}
else if ((NULL != strstr(line, auth)) && (NULL != strstr(line, pamSo)) &&
(NULL != (authValue = GetStringOptionFromBuffer(line, auth, ' ', log))) && (0 == strcmp(authValue, required)) && FreeAndReturnTrue(authValue) &&
(NULL != (authValue = GetStringOptionFromBuffer(line, auth, ' ', log))) && (0 == strcmp(authValue, required)) &&
(0 <= (deny = GetIntegerOptionFromBuffer(line, "deny", '=', log))) && (deny <= 5) &&
(0 < (unlockTime = GetIntegerOptionFromBuffer(line, "unlock_time", '=', log))))
{
Expand All @@ -229,10 +234,12 @@ int CheckLockoutForFailedPasswordAttempts(const char* fileName, const char* pamS
auth, required, pamSo, deny, unlockTime, fileName);

status = 0;
FREE_MEMORY(authValue);
break;
}
else
{
FREE_MEMORY(authValue);
status = ENOENT;
}

Expand Down
14 changes: 12 additions & 2 deletions src/common/commonutils/SshUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ static int IsSshConfigIncludeSupported(void* log)

static int CheckOnlyApprovedMacAlgorithmsAreUsed(const char* macs, char** reason, void* log)
{
char* sshMacs = DuplicateStringToLowercase(g_sshMacs);
char* sshMacs = NULL;
char* macsValue = NULL;
char* value = NULL;
size_t macsValueLength = 0;
Expand All @@ -291,6 +291,11 @@ static int CheckOnlyApprovedMacAlgorithmsAreUsed(const char* macs, char** reason
{
return status;
}
else if (NULL == (sshMacs = DuplicateStringToLowercase(g_sshMacs)))
{
OsConfigLogError(log, "CheckOnlyApprovedMacAlgorithmsAreUsed: failed to duplicate string to lowercase");
return ENOMEM;
}

if (NULL == (macsValue = GetSshServerState(sshMacs, log)))
{
Expand Down Expand Up @@ -343,7 +348,7 @@ static int CheckOnlyApprovedMacAlgorithmsAreUsed(const char* macs, char** reason

static int CheckAppropriateCiphersForSsh(const char* ciphers, char** reason, void* log)
{
char* sshCiphers = DuplicateStringToLowercase(g_sshCiphers);
char* sshCiphers = NULL;
char* ciphersValue = NULL;
char* value = NULL;
size_t ciphersValueLength = 0;
Expand All @@ -359,6 +364,11 @@ static int CheckAppropriateCiphersForSsh(const char* ciphers, char** reason, voi
{
return status;
}
else if (NULL == (sshCiphers = DuplicateStringToLowercase(g_sshCiphers)))
{
OsConfigLogError(log, "CheckAppropriateCiphersForSsh: failed to duplicate string to lowercase");
return ENOMEM;
}

if (NULL == (ciphersValue = GetSshServerState(sshCiphers, log)))
{
Expand Down
2 changes: 1 addition & 1 deletion src/common/commonutils/UrlUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ char* UrlEncode(char* target)

int i = 0, j = 0;
int targetLength = (int)strlen(target);
int encodedLength = 3 * targetLength;
int encodedLength = (3 * targetLength) + 1;
char* encodedTarget = (char*)malloc(encodedLength);
if (NULL != encodedTarget)
{
Expand Down
12 changes: 12 additions & 0 deletions src/modules/commandrunner/tests/CommandRunnerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ namespace Tests

EXPECT_EQ(MMI_OK, m_commandRunner->Get(m_component, m_reportedObject, &reportedPayload, &payloadSizeBytes));
EXPECT_TRUE(IsJsonEq(Command::Status::Serialize(status), std::string(reportedPayload, payloadSizeBytes)));
delete[] reportedPayload;
MariusNi marked this conversation as resolved.
Show resolved Hide resolved
}

TEST_F(CommandRunnerTests, RunCommandTimeout)
Expand All @@ -145,6 +146,7 @@ namespace Tests

EXPECT_EQ(MMI_OK, m_commandRunner->Get(m_component, m_reportedObject, &reportedPayload, &payloadSizeBytes));
EXPECT_TRUE(IsJsonEq(Command::Status::Serialize(status), std::string(reportedPayload, payloadSizeBytes)));
delete[] reportedPayload;
}

TEST_F(CommandRunnerTests, RunCommandSingleLineTextResult)
Expand All @@ -163,6 +165,7 @@ namespace Tests

EXPECT_EQ(MMI_OK, m_commandRunner->Get(m_component, m_reportedObject, &reportedPayload, &payloadSizeBytes));
EXPECT_TRUE(IsJsonEq(Command::Status::Serialize(status), std::string(reportedPayload, payloadSizeBytes)));
delete[] reportedPayload;
}

TEST_F(CommandRunnerTests, RunCommandLimitedPayloadSize)
Expand Down Expand Up @@ -190,6 +193,7 @@ namespace Tests

EXPECT_EQ(MMI_OK, commandRunner.Get(m_component, m_reportedObject, &reportedPayload, &payloadSizeBytes));
EXPECT_TRUE(IsJsonEq(Command::Status::Serialize(status), std::string(reportedPayload, payloadSizeBytes)));
delete[] reportedPayload;
}

TEST_F(CommandRunnerTests, RunCommandMaximumCacheSize)
Expand Down Expand Up @@ -222,6 +226,7 @@ namespace Tests
EXPECT_EQ(MMI_OK, m_commandRunner->Set(m_component, m_desiredObject, (MMI_JSON_STRING)(refresh.c_str()), refresh.size()));
EXPECT_EQ(MMI_OK, m_commandRunner->Get(m_component, m_reportedObject, &reportedPayload, &payloadSizeBytes));
EXPECT_TRUE(IsJsonEq(Command::Status::Serialize(expectedResult.second), std::string(reportedPayload, payloadSizeBytes)));
delete[] reportedPayload;
}

// Add one more command to the cache
Expand All @@ -237,6 +242,7 @@ namespace Tests
// Get the last command from the cache
EXPECT_EQ(MMI_OK, m_commandRunner->Get(m_component, m_reportedObject, &reportedPayload, &payloadSizeBytes));
EXPECT_TRUE(IsJsonEq(Command::Status::Serialize(lastStatus), std::string(reportedPayload, payloadSizeBytes)));
delete[] reportedPayload;

// The first command should have been removed from the cache
Command::Arguments refreshFirstCommand(expectedResults[0].first, "", Command::Action::RefreshCommandStatus, 0, false);
Expand All @@ -246,6 +252,7 @@ namespace Tests
// The last command should still be reported (set as command to report) and in the cache
EXPECT_EQ(MMI_OK, m_commandRunner->Get(m_component, m_reportedObject, &reportedPayload, &payloadSizeBytes));
EXPECT_TRUE(IsJsonEq(Command::Status::Serialize(lastStatus), std::string(reportedPayload, payloadSizeBytes)));
delete[] reportedPayload;
}

TEST_F(CommandRunnerTests, RefreshCommand)
Expand Down Expand Up @@ -273,6 +280,7 @@ namespace Tests
// The last run command should be reported
EXPECT_EQ(MMI_OK, m_commandRunner->Get(m_component, m_reportedObject, &reportedPayload, &payloadSizeBytes));
EXPECT_TRUE(IsJsonEq(Command::Status::Serialize(status2), std::string(reportedPayload, payloadSizeBytes)));
delete[] reportedPayload;

// Refresh the command
Command::Arguments refresh(id1, "", Command::Action::RefreshCommandStatus, 0, false);
Expand All @@ -284,6 +292,7 @@ namespace Tests
// The refreshed command should be reported
EXPECT_EQ(MMI_OK, m_commandRunner->Get(m_component, m_reportedObject, &reportedPayload, &payloadSizeBytes));
EXPECT_TRUE(IsJsonEq(Command::Status::Serialize(status1), std::string(reportedPayload, payloadSizeBytes)));
delete[] reportedPayload;
}

TEST_F(CommandRunnerTests, CancelCommandInProgress)
Expand All @@ -306,6 +315,7 @@ namespace Tests

EXPECT_EQ(MMI_OK, m_commandRunner->Get(m_component, m_reportedObject, &reportedPayload, &payloadSizeBytes));
EXPECT_TRUE(IsJsonEq(Command::Status::Serialize(status), std::string(reportedPayload, payloadSizeBytes)));
delete[] reportedPayload;
}

TEST_F(CommandRunnerTests, RepeatCommandId)
Expand All @@ -327,6 +337,7 @@ namespace Tests

EXPECT_EQ(MMI_OK, m_commandRunner->Get(m_component, m_reportedObject, &reportedPayload, &payloadSizeBytes));
EXPECT_TRUE(IsJsonEq(Command::Status::Serialize(status), std::string(reportedPayload, payloadSizeBytes)));
delete[] reportedPayload;
}

TEST_F(CommandRunnerTests, RepeatCommand)
Expand All @@ -346,6 +357,7 @@ namespace Tests

EXPECT_EQ(MMI_OK, m_commandRunner->Get(m_component, m_reportedObject, &reportedPayload, &payloadSizeBytes));
EXPECT_TRUE(IsJsonEq(Command::Status::Serialize(status), std::string(reportedPayload, payloadSizeBytes)));
delete[] reportedPayload;
}

TEST_F(CommandRunnerTests, ExecuteCommand)
Expand Down
4 changes: 3 additions & 1 deletion src/modules/configuration/src/lib/Configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ static char* LoadConfigurationFromFile(const char* fileName)
g_iotHubManagementEnabled = IsIotHubManagementEnabledInJsonConfig(jsonConfiguration);
g_iotHubProtocol = GetIotHubProtocolFromJsonConfig(jsonConfiguration, ConfigurationGetLog());
g_gitManagementEnabled = GetGitManagementFromJsonConfig(jsonConfiguration, ConfigurationGetLog());

FREE_MEMORY(g_gitBranch);
MariusNi marked this conversation as resolved.
Show resolved Hide resolved
g_gitBranch = GetGitBranchFromJsonConfig(jsonConfiguration, ConfigurationGetLog());
}
else
Expand Down Expand Up @@ -597,7 +599,7 @@ int ConfigurationMmiSet(MMI_HANDLE clientSession, const char* componentName, con
}
else if (0 == strcmp(objectName, g_desiredGitBranchObject))
{
if (NULL != (jsonString = (char*)json_value_get_string(jsonValue)))
if (NULL != (jsonString = json_value_get_string(jsonValue)))
{
if (jsonString)
{
Expand Down
3 changes: 2 additions & 1 deletion src/platform/MpiServer.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ HTTP_STATUS HandleMpiCall(const char* uri, const char* requestBody, char** respo
const char* client = NULL;
const char* component = NULL;
const char* object = NULL;
const char* payload = NULL;
char* payload = NULL;
int maxPayloadSizeBytes = 0;
int estimatedSize = 0;
const char* responseFormat = "\"%s\"";
Expand Down Expand Up @@ -424,6 +424,7 @@ HTTP_STATUS HandleMpiCall(const char* uri, const char* requestBody, char** respo
}
}

json_free_serialized_string(payload);
json_value_free(rootValue);

return status;
Expand Down
Loading
Loading