@@ -410,7 +410,10 @@ getClassSignatureLength(J9VMThread *currentThread, J9Class *clazz)
410
410
j9object_t sigString = J9VMJAVALANGCLASS_CLASSNAMESTRING (currentThread, J9VM_J9CLASS_TO_HEAPCLASS (clazz));
411
411
if (NULL != sigString) {
412
412
/* +2 so that we can fit 'L' and ';' around the class name. */
413
- signatureLength = vm->internalVMFunctions ->getStringUTF8Length (currentThread, sigString) + 2 ;
413
+ signatureLength = vm->internalVMFunctions ->getStringUTF8Length (currentThread, sigString);
414
+ if (signatureLength <= (UDATA_MAX - 2 )) {
415
+ signatureLength += 2 ;
416
+ }
414
417
} else {
415
418
J9Class *myClass = clazz;
416
419
UDATA numDims = 0 ;
@@ -470,24 +473,27 @@ getClassSignatureInout(J9VMThread *currentThread, J9Class *clazz, LocalJ9UTF8Buf
470
473
j9object_t sigString = J9VMJAVALANGCLASS_CLASSNAMESTRING (currentThread, J9VM_J9CLASS_TO_HEAPCLASS (clazz));
471
474
if (NULL != sigString) {
472
475
/* +3 so that we can fit 'L' and ';' around the class name and add null-terminator. */
473
- UDATA utfLength = vm->internalVMFunctions ->getStringUTF8Length (currentThread, sigString) + 3 ;
474
- if (utfLength <= stringBuffer->remaining ()) {
475
- if (J9ROMCLASS_IS_ARRAY (clazz->romClass )) {
476
- vm->internalVMFunctions ->copyStringToUTF8Helper (
477
- currentThread, sigString, J9_STR_XLAT, 0 , J9VMJAVALANGSTRING_LENGTH (currentThread, sigString),
478
- stringBuffer->cursor , utfLength - 3 );
479
- /* Adjust cursor to account for the call to copyStringToUTF8Helper. */
480
- stringBuffer->advanceN (utfLength - 3 );
481
- } else {
482
- stringBuffer->putCharAtCursor (' L' );
483
- vm->internalVMFunctions ->copyStringToUTF8Helper (
484
- currentThread, sigString, J9_STR_XLAT, 0 , J9VMJAVALANGSTRING_LENGTH (currentThread, sigString),
485
- stringBuffer->cursor , utfLength - 3 );
486
- /* Adjust cursor to account for the call to copyStringToUTF8Helper. */
487
- stringBuffer->advanceN (utfLength - 3 );
488
- stringBuffer->putCharAtCursor (' ;' );
476
+ UDATA utfLength = vm->internalVMFunctions ->getStringUTF8Length (currentThread, sigString);
477
+ if (utfLength <= (UDATA_MAX - 3 )) {
478
+ utfLength += 3 ;
479
+ if (utfLength <= stringBuffer->remaining ()) {
480
+ if (J9ROMCLASS_IS_ARRAY (clazz->romClass )) {
481
+ vm->internalVMFunctions ->copyStringToUTF8Helper (
482
+ currentThread, sigString, J9_STR_XLAT, 0 , J9VMJAVALANGSTRING_LENGTH (currentThread, sigString),
483
+ stringBuffer->cursor , utfLength - 3 );
484
+ /* Adjust cursor to account for the call to copyStringToUTF8Helper. */
485
+ stringBuffer->advanceN (utfLength - 3 );
486
+ } else {
487
+ stringBuffer->putCharAtCursor (' L' );
488
+ vm->internalVMFunctions ->copyStringToUTF8Helper (
489
+ currentThread, sigString, J9_STR_XLAT, 0 , J9VMJAVALANGSTRING_LENGTH (currentThread, sigString),
490
+ stringBuffer->cursor , utfLength - 3 );
491
+ /* Adjust cursor to account for the call to copyStringToUTF8Helper. */
492
+ stringBuffer->advanceN (utfLength - 3 );
493
+ stringBuffer->putCharAtCursor (' ;' );
494
+ }
495
+ result = true ;
489
496
}
490
- result = true ;
491
497
}
492
498
} else {
493
499
J9Class *myClass = clazz;
@@ -557,20 +563,34 @@ getJ9UTF8SignatureFromMethodTypeWithMemAlloc(J9VMThread *currentThread, j9object
557
563
j9object_t ptypes = J9VMJAVALANGINVOKEMETHODTYPE_PTYPES (currentThread, typeObject);
558
564
U_32 numArgs = J9INDEXABLEOBJECT_SIZE (currentThread, ptypes);
559
565
UDATA signatureLength = 2 ; /* space for '(', ')' */
566
+ UDATA tempSignatureLength = 0 ;
567
+ UDATA signatureUtf8Size = 0 ;
568
+ J9UTF8 *result = NULL ;
569
+ j9object_t rtype = NULL ;
570
+ J9Class *rclass = NULL ;
560
571
PORT_ACCESS_FROM_JAVAVM (vm);
561
572
562
573
/* Calculate total signature length, including all ptypes and rtype. */
563
574
for (U_32 i = 0 ; i < numArgs; i++) {
564
575
j9object_t pObject = J9JAVAARRAYOFOBJECT_LOAD (currentThread, ptypes, i);
565
576
J9Class *pclass = J9VM_J9CLASS_FROM_HEAPCLASS (currentThread, pObject);
566
- signatureLength += getClassSignatureLength (currentThread, pclass);
577
+ tempSignatureLength = getClassSignatureLength (currentThread, pclass);
578
+ if (signatureLength > (J9UTF8_MAX_LENGTH - tempSignatureLength)) {
579
+ goto done;
580
+ }
581
+ signatureLength += tempSignatureLength;
567
582
}
568
- j9object_t rtype = J9VMJAVALANGINVOKEMETHODTYPE_RTYPE (currentThread, typeObject);
569
- J9Class *rclass = J9VM_J9CLASS_FROM_HEAPCLASS (currentThread, rtype);
570
- signatureLength += getClassSignatureLength (currentThread, rclass);
583
+ rtype = J9VMJAVALANGINVOKEMETHODTYPE_RTYPE (currentThread, typeObject);
584
+ rclass = J9VM_J9CLASS_FROM_HEAPCLASS (currentThread, rtype);
585
+ tempSignatureLength = getClassSignatureLength (currentThread, rclass);
586
+ if (signatureLength > (J9UTF8_MAX_LENGTH - tempSignatureLength)) {
587
+ goto done;
588
+ }
589
+ signatureLength += tempSignatureLength;
590
+
591
+ signatureUtf8Size = signatureLength + sizeof (J9UTF8) + 1 ; /* +1 for a null-terminator */
592
+ result = reinterpret_cast <J9UTF8 *>(j9mem_allocate_memory (signatureUtf8Size, OMRMEM_CATEGORY_VM));
571
593
572
- UDATA signatureUtf8Size = signatureLength + sizeof (J9UTF8) + 1 ; /* +1 for a null-terminator */
573
- J9UTF8 *result = reinterpret_cast <J9UTF8 *>(j9mem_allocate_memory (signatureUtf8Size, OMRMEM_CATEGORY_VM));
574
594
if (NULL != result) {
575
595
LocalJ9UTF8Buffer stringBuffer (result, signatureUtf8Size);
576
596
@@ -588,6 +608,7 @@ getJ9UTF8SignatureFromMethodTypeWithMemAlloc(J9VMThread *currentThread, j9object
588
608
stringBuffer.commitLength ();
589
609
}
590
610
611
+ done:
591
612
return result;
592
613
}
593
614
@@ -1036,12 +1057,21 @@ Java_java_lang_invoke_MethodHandleNatives_resolve(
1036
1057
} else {
1037
1058
LocalJ9UTF8Buffer stringBuffer (reinterpret_cast <J9UTF8 *>(signatureBuffer), sizeof (signatureBuffer));
1038
1059
signature = getJ9UTF8SignatureFromMethodType (currentThread, typeObject, &stringBuffer);
1060
+ if (NULL == signature) {
1061
+ vmFuncs->setCurrentExceptionUTF (currentThread, J9VMCONSTANTPOOL_JAVALANGINTERNALERROR, NULL );
1062
+ goto done;
1063
+ }
1039
1064
}
1040
1065
} else if (J9VMJAVALANGSTRING_OR_NULL (vm) == typeClass) {
1041
1066
signature = vmFuncs->copyStringToJ9UTF8WithMemAlloc (currentThread, typeObject, J9_STR_XLAT, " " , 0 , signatureBuffer, sizeof (signatureBuffer));
1042
1067
} else if (J9VMJAVALANGCLASS (vm) == typeClass) {
1043
1068
J9Class *rclass = J9VM_J9CLASS_FROM_HEAPCLASS (currentThread, typeObject);
1044
- UDATA signatureLength = getClassSignatureLength (currentThread, rclass) + sizeof (J9UTF8) + 1 /* null-terminator */ ;
1069
+ UDATA signatureLength = getClassSignatureLength (currentThread, rclass);
1070
+ if (signatureLength > J9UTF8_MAX_LENGTH) {
1071
+ vmFuncs->setCurrentExceptionUTF (currentThread, J9VMCONSTANTPOOL_JAVALANGINTERNALERROR, NULL );
1072
+ goto done;
1073
+ }
1074
+ signatureLength += sizeof (J9UTF8) + 1 /* null-terminator */ ;
1045
1075
LocalJ9UTF8Buffer stringBuffer;
1046
1076
if (signatureLength <= sizeof (signatureBuffer)) {
1047
1077
stringBuffer = LocalJ9UTF8Buffer (reinterpret_cast <J9UTF8 *>(signatureBuffer), sizeof (signatureBuffer));
0 commit comments