Skip to content

Commit 8f8a879

Browse files
committedMar 6, 2025
8350939: Revisit Windows PDH buffer size calculation for OperatingSystemMXBean
Reviewed-by: dholmes, lmesnik, sspitsyn
1 parent cfab88b commit 8f8a879

File tree

1 file changed

+36
-41
lines changed

1 file changed

+36
-41
lines changed
 

‎src/jdk.management/windows/native/libmanagement_ext/OperatingSystemImpl.c

+36-41
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ static const DWORD PDH_PROCESSOR_TIME_IDX = 6;
291291
static const DWORD PDH_PROCESS_IDX = 230;
292292
static const DWORD PDH_ID_PROCESS_IDX = 784;
293293

294-
/* useful pdh fmt's */
294+
/* PDH format patterns, and lengths of their constant component. */
295295
static const char* const OBJECT_COUNTER_FMT = "\\%s\\%s";
296296
static const size_t OBJECT_COUNTER_FMT_LEN = 2;
297297
static const char* const OBJECT_WITH_INSTANCES_COUNTER_FMT = "\\%s(%s)\\%s";
@@ -405,8 +405,8 @@ makeFullCounterPath(const char* const objectName,
405405
assert(objectName);
406406
assert(counterName);
407407

408-
fullCounterPathLen = strlen(objectName);
409-
fullCounterPathLen += strlen(counterName);
408+
// Always include space for null terminator:
409+
fullCounterPathLen = strlen(objectName) + strlen(counterName) + 1;
410410

411411
if (imageName) {
412412
/*
@@ -429,20 +429,19 @@ makeFullCounterPath(const char* const objectName,
429429
assert(instance);
430430

431431
fullCounterPathLen += strlen(instance);
432-
433-
fullCounterPath = malloc(fullCounterPathLen + 1);
432+
fullCounterPath = malloc(fullCounterPathLen);
434433

435434
if (!fullCounterPath) {
436435
return NULL;
437436
}
438437

439-
_snprintf(fullCounterPath,
440-
fullCounterPathLen,
441-
PROCESS_OBJECT_INSTANCE_COUNTER_FMT,
442-
objectName,
443-
imageName,
444-
instance,
445-
counterName);
438+
snprintf(fullCounterPath,
439+
fullCounterPathLen,
440+
PROCESS_OBJECT_INSTANCE_COUNTER_FMT,
441+
objectName,
442+
imageName,
443+
instance,
444+
counterName);
446445
} else {
447446
if (instance) {
448447
/*
@@ -465,30 +464,28 @@ makeFullCounterPath(const char* const objectName,
465464
fullCounterPathLen += OBJECT_COUNTER_FMT_LEN;
466465
}
467466

468-
fullCounterPath = malloc(fullCounterPathLen + 1);
467+
fullCounterPath = malloc(fullCounterPathLen);
469468

470469
if (!fullCounterPath) {
471470
return NULL;
472471
}
473472

474473
if (instance) {
475-
_snprintf(fullCounterPath,
476-
fullCounterPathLen,
477-
OBJECT_WITH_INSTANCES_COUNTER_FMT,
478-
objectName,
479-
instance,
480-
counterName);
474+
snprintf(fullCounterPath,
475+
fullCounterPathLen,
476+
OBJECT_WITH_INSTANCES_COUNTER_FMT,
477+
objectName,
478+
instance,
479+
counterName);
481480
} else {
482-
_snprintf(fullCounterPath,
483-
fullCounterPathLen,
484-
OBJECT_COUNTER_FMT,
485-
objectName,
486-
counterName);
481+
snprintf(fullCounterPath,
482+
fullCounterPathLen,
483+
OBJECT_COUNTER_FMT,
484+
objectName,
485+
counterName);
487486
}
488487
}
489488

490-
fullCounterPath[fullCounterPathLen] = '\0';
491-
492489
return fullCounterPath;
493490
}
494491

@@ -719,10 +716,10 @@ currentQueryIndexForProcess(void) {
719716
PDH_FMT_COUNTERVALUE counterValue;
720717
PDH_STATUS res;
721718

722-
_snprintf(fullIDProcessCounterPath,
723-
MAX_PATH,
724-
pdhIDProcessCounterFmt,
725-
index);
719+
snprintf(fullIDProcessCounterPath,
720+
MAX_PATH,
721+
pdhIDProcessCounterFmt,
722+
index);
726723

727724
if (addCounter(tmpQuery, fullIDProcessCounterPath, &handleCounter) != 0) {
728725
break;
@@ -1050,24 +1047,22 @@ allocateAndInitializePdhConstants() {
10501047
pdhIDProcessCounterFmtLen += strlen(pdhLocalizedProcessObject);
10511048
pdhIDProcessCounterFmtLen += strlen(pdhLocalizedIDProcessCounter);
10521049
pdhIDProcessCounterFmtLen += PROCESS_OBJECT_INSTANCE_COUNTER_FMT_LEN;
1053-
pdhIDProcessCounterFmtLen += 2; // "%d"
1050+
pdhIDProcessCounterFmtLen += 3; // "%d" and '\0'
10541051

10551052
assert(pdhIDProcessCounterFmtLen < MAX_PATH);
1056-
pdhIDProcessCounterFmt = malloc(pdhIDProcessCounterFmtLen + 1);
1053+
pdhIDProcessCounterFmt = malloc(pdhIDProcessCounterFmtLen);
10571054
if (!pdhIDProcessCounterFmt) {
10581055
goto end;
10591056
}
10601057

10611058
/* "\Process(java#%d)\ID Process" */
1062-
_snprintf(pdhIDProcessCounterFmt,
1063-
pdhIDProcessCounterFmtLen,
1064-
PROCESS_OBJECT_INSTANCE_COUNTER_FMT,
1065-
pdhLocalizedProcessObject,
1066-
pdhProcessImageName,
1067-
"%d",
1068-
pdhLocalizedIDProcessCounter);
1069-
1070-
pdhIDProcessCounterFmt[pdhIDProcessCounterFmtLen] = '\0';
1059+
snprintf(pdhIDProcessCounterFmt,
1060+
pdhIDProcessCounterFmtLen,
1061+
PROCESS_OBJECT_INSTANCE_COUNTER_FMT,
1062+
pdhLocalizedProcessObject,
1063+
pdhProcessImageName,
1064+
"%d",
1065+
pdhLocalizedIDProcessCounter);
10711066

10721067
assert(0 == numberOfJavaProcessesAtInitialization);
10731068
currentQueryIndex = currentQueryIndexForProcess();

0 commit comments

Comments
 (0)
Please sign in to comment.