Skip to content

Commit

Permalink
Fix bugs found from static analyzer (#76160)
Browse files Browse the repository at this point in the history
* Fix bugs found from static analyzer

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
  • Loading branch information
HJLeee and jkotas committed Sep 27, 2022
1 parent 62db361 commit 8b19f23
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/coreclr/hosts/corerun/corerun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ namespace pal

// Attempt to read
ssize_t bytes_read = ::read(fd, status, sizeof(status) - 1);
::close(fd);

if (bytes_read > 0)
{
// We have data. At this point we can likely make a strong decision.
Expand Down
13 changes: 13 additions & 0 deletions src/native/libs/System.Globalization.Native/pal_localeNumberData.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,20 @@ static char* NormalizeNumericPattern(const UChar* srcPattern, int isNegative)
{
int length = (iEnd - iStart) + 2;
destPattern = (char*)calloc((size_t)length, sizeof(char));
if (!destPattern)
{
return NULL;
}
destPattern[index++] = '-';
}
else
{
int length = (iEnd - iStart) + 1;
destPattern = (char*)calloc((size_t)length, sizeof(char));
if (!destPattern)
{
return NULL;
}
}

for (int i = iStart; i <= iEnd; i++)
Expand Down Expand Up @@ -178,6 +186,11 @@ static int GetNumericPattern(const UNumberFormat* pNumberFormat,

free(icuPattern);

if (!normalizedPattern)
{
return U_MEMORY_ALLOCATION_ERROR;
}

size_t normalizedPatternLength = strlen(normalizedPattern);

assert(normalizedPatternLength > 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ static const UNormalizer2* GetNormalizerForForm(NormalizationForm normalizationF
return unorm2_getNFKCInstance(pErrorCode);
case FormKD:
return unorm2_getNFKDInstance(pErrorCode);
default:
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return NULL;
}

*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return NULL;
}

/*
Expand Down
22 changes: 6 additions & 16 deletions src/native/libs/System.Native/pal_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,23 +244,13 @@ int32_t SystemNative_ForkAndExecProcess(const char* filename,
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &thread_cancel_state);
#endif

// Validate arguments
if (NULL == filename || NULL == argv || NULL == envp || NULL == stdinFd || NULL == stdoutFd ||
NULL == stderrFd || NULL == childPid || (groupsLength > 0 && groups == NULL))
{
assert(false && "null argument.");
errno = EINVAL;
success = false;
goto done;
}
assert(NULL != filename && NULL != argv && NULL != envp && NULL != stdinFd &&
NULL != stdoutFd && NULL != stderrFd && NULL != childPid &&
(groupsLength == 0 || groups != NULL) && "null argument.");

if ((redirectStdin & ~1) != 0 || (redirectStdout & ~1) != 0 || (redirectStderr & ~1) != 0 || (setCredentials & ~1) != 0)
{
assert(false && "Boolean redirect* inputs must be 0 or 1.");
errno = EINVAL;
success = false;
goto done;
}
assert((redirectStdin & ~1) == 0 && (redirectStdout & ~1) == 0 &&
(redirectStderr & ~1) == 0 && (setCredentials & ~1) == 0 &&
"Boolean redirect* inputs must be 0 or 1.");

if (setCredentials && groupsLength > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,13 @@ static DIR* OpenUserStore(const char* storePath, char** pathTmp, size_t* pathTmp
// Leave one byte for '\0' and one for '/'
size_t allocSize = storePathLen + sizeof(ent->d_name) + 2;
char* tmp = (char*)calloc(allocSize, sizeof(char));
if (!tmp)
{
*pathTmp = NULL;
*nextFileWrite = NULL;
return NULL;
}

memcpy_s(tmp, allocSize, storePath, storePathLen);
tmp[storePathLen] = '/';
*pathTmp = tmp;
Expand Down Expand Up @@ -604,6 +611,8 @@ int32_t CryptoNative_X509StackAddDirectoryStore(X509Stack* stack, char* storePat

if (tmpStack == NULL)
{
free(pathTmp);
closedir(storeDir);
return 0;
}

Expand Down

0 comments on commit 8b19f23

Please sign in to comment.