@@ -291,7 +291,7 @@ static const DWORD PDH_PROCESSOR_TIME_IDX = 6;
291
291
static const DWORD PDH_PROCESS_IDX = 230 ;
292
292
static const DWORD PDH_ID_PROCESS_IDX = 784 ;
293
293
294
- /* useful pdh fmt's */
294
+ /* PDH format patterns, and lengths of their constant component. */
295
295
static const char * const OBJECT_COUNTER_FMT = "\\%s\\%s" ;
296
296
static const size_t OBJECT_COUNTER_FMT_LEN = 2 ;
297
297
static const char * const OBJECT_WITH_INSTANCES_COUNTER_FMT = "\\%s(%s)\\%s" ;
@@ -405,8 +405,8 @@ makeFullCounterPath(const char* const objectName,
405
405
assert (objectName );
406
406
assert (counterName );
407
407
408
- fullCounterPathLen = strlen ( objectName );
409
- fullCounterPathLen + = strlen (counterName );
408
+ // Always include space for null terminator:
409
+ fullCounterPathLen = strlen (objectName ) + strlen ( counterName ) + 1 ;
410
410
411
411
if (imageName ) {
412
412
/*
@@ -429,20 +429,19 @@ makeFullCounterPath(const char* const objectName,
429
429
assert (instance );
430
430
431
431
fullCounterPathLen += strlen (instance );
432
-
433
- fullCounterPath = malloc (fullCounterPathLen + 1 );
432
+ fullCounterPath = malloc (fullCounterPathLen );
434
433
435
434
if (!fullCounterPath ) {
436
435
return NULL ;
437
436
}
438
437
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 );
446
445
} else {
447
446
if (instance ) {
448
447
/*
@@ -465,30 +464,28 @@ makeFullCounterPath(const char* const objectName,
465
464
fullCounterPathLen += OBJECT_COUNTER_FMT_LEN ;
466
465
}
467
466
468
- fullCounterPath = malloc (fullCounterPathLen + 1 );
467
+ fullCounterPath = malloc (fullCounterPathLen );
469
468
470
469
if (!fullCounterPath ) {
471
470
return NULL ;
472
471
}
473
472
474
473
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 );
481
480
} 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 );
487
486
}
488
487
}
489
488
490
- fullCounterPath [fullCounterPathLen ] = '\0' ;
491
-
492
489
return fullCounterPath ;
493
490
}
494
491
@@ -719,10 +716,10 @@ currentQueryIndexForProcess(void) {
719
716
PDH_FMT_COUNTERVALUE counterValue ;
720
717
PDH_STATUS res ;
721
718
722
- _snprintf (fullIDProcessCounterPath ,
723
- MAX_PATH ,
724
- pdhIDProcessCounterFmt ,
725
- index );
719
+ snprintf (fullIDProcessCounterPath ,
720
+ MAX_PATH ,
721
+ pdhIDProcessCounterFmt ,
722
+ index );
726
723
727
724
if (addCounter (tmpQuery , fullIDProcessCounterPath , & handleCounter ) != 0 ) {
728
725
break ;
@@ -1050,24 +1047,22 @@ allocateAndInitializePdhConstants() {
1050
1047
pdhIDProcessCounterFmtLen += strlen (pdhLocalizedProcessObject );
1051
1048
pdhIDProcessCounterFmtLen += strlen (pdhLocalizedIDProcessCounter );
1052
1049
pdhIDProcessCounterFmtLen += PROCESS_OBJECT_INSTANCE_COUNTER_FMT_LEN ;
1053
- pdhIDProcessCounterFmtLen += 2 ; // "%d"
1050
+ pdhIDProcessCounterFmtLen += 3 ; // "%d" and '\0'
1054
1051
1055
1052
assert (pdhIDProcessCounterFmtLen < MAX_PATH );
1056
- pdhIDProcessCounterFmt = malloc (pdhIDProcessCounterFmtLen + 1 );
1053
+ pdhIDProcessCounterFmt = malloc (pdhIDProcessCounterFmtLen );
1057
1054
if (!pdhIDProcessCounterFmt ) {
1058
1055
goto end ;
1059
1056
}
1060
1057
1061
1058
/* "\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 );
1071
1066
1072
1067
assert (0 == numberOfJavaProcessesAtInitialization );
1073
1068
currentQueryIndex = currentQueryIndexForProcess ();
0 commit comments