Skip to content

Commit

Permalink
Merge pull request #17259 from pshipton/checklength
Browse files Browse the repository at this point in the history
Add check for string length in getCachedUTFString()
  • Loading branch information
keithc-ca committed Apr 24, 2023
2 parents 8c3b2e2 + 6b507fa commit 8490b80
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 17 additions & 4 deletions runtime/shared_common/CacheMap.cpp
Expand Up @@ -5465,10 +5465,10 @@ SH_CacheMap::getCachedUTFString(J9VMThread* currentThread, const char* local, U_
const char* fnName = "getCachedUTFString";
const J9UTF8* pathUTF = NULL;
U_8 temp[J9SH_MAXPATH + sizeof(J9UTF8)];
J9UTF8* temputf = (J9UTF8*)&temp;
char* tempstr = (char*)J9UTF8_DATA(temputf);
J9UTF8* temputf = (J9UTF8*)temp;
SH_ScopeManager* localSCM;
bool allowUpdate = true;
PORT_ACCESS_FROM_VMC(currentThread);

Trc_SHR_Assert_False(_ccHead->hasWriteMutex(currentThread));

Expand All @@ -5495,9 +5495,16 @@ SH_CacheMap::getCachedUTFString(J9VMThread* currentThread, const char* local, U_
allowUpdate = false;
}

J9UTF8_SET_LENGTH(temputf, localLen);
strncpy(tempstr, local, J9UTF8_LENGTH(temputf));
if (localLen > J9SH_MAXPATH) {
temputf = (J9UTF8 *)j9mem_allocate_memory(localLen + sizeof(J9UTF8), J9MEM_CATEGORY_CLASSES);
if (NULL == temputf) {
Trc_SHR_CM_getCachedUTFString_exit4(currentThread);
return NULL;
}
}

memcpy(J9UTF8_DATA(temputf), local, localLen);
J9UTF8_SET_LENGTH(temputf, localLen);
pathUTF = localSCM->findScopeForUTF(currentThread, temputf);
_ccHead->exitReadMutex(currentThread, fnName);
if (NULL == pathUTF) {
Expand All @@ -5509,6 +5516,9 @@ SH_CacheMap::getCachedUTFString(J9VMThread* currentThread, const char* local, U_
*/
if ((itemsRead = runEntryPointChecks(currentThread, NULL, NULL)) == -1) {
_ccHead->exitWriteMutex(currentThread, fnName);
if ((J9UTF8 *)temp != temputf) {
j9mem_free_memory(temputf);
}
Trc_SHR_CM_getCachedUTFString_exit3(currentThread);
return NULL;
}
Expand All @@ -5523,6 +5533,9 @@ SH_CacheMap::getCachedUTFString(J9VMThread* currentThread, const char* local, U_
}
}

if ((J9UTF8 *)temp != temputf) {
j9mem_free_memory(temputf);
}
Trc_SHR_CM_getCachedUTFString_exit2(currentThread, pathUTF);

return pathUTF;
Expand Down
2 changes: 2 additions & 0 deletions runtime/shared_common/j9shr.tdf
Expand Up @@ -2992,3 +2992,5 @@ TraceExit-Exception=Trc_SHR_CMI_Update_Exit5 Overhead=1 Level=2 Template="CMI Up
TraceExit-Exception=Trc_SHR_CMI_validate_Exit_IdentifiedMutex_Failed Overhead=1 Level=2 Template="CMI validate: Failed to acquire _identifiedMutex. Returning -1."
TraceException=Trc_SHR_CC_changePartialPageProtection_NotDone_V1 Overhead=1 Level=1 Template="CC changePartialPageProtection: Returning without changing page protection for address %p to %s"
TraceEvent=Trc_SHR_CC_freeBlockBytes_info NoEnv Overhead=1 Level=10 Template="CC getFreeBlockBytes: case %d, retVal %d, freeBytes %d, minAOT %d, aotBytes %d, minJIT %d, jitBytes %d"

TraceExit-Exception=Trc_SHR_CM_getCachedUTFString_exit4 Overhead=1 Level=2 Template="CM getCachedUTFString: Exiting due to memory allocation failed"

0 comments on commit 8490b80

Please sign in to comment.