@@ -177,11 +177,9 @@ static char *bitmap2rangestr(int bitmap)
177
177
size_t i ;
178
178
int range_start , range_end ;
179
179
bool first , isset ;
180
- char tmp [BUFSIZ ] ;
180
+ char tmp [OMPI_AFFINITY_STRING_MAX - 1 ] = { 0 } ;
181
181
const int stmp = sizeof (tmp ) - 1 ;
182
- static char ret [BUFSIZ ];
183
-
184
- memset (ret , 0 , sizeof (ret ));
182
+ static char ret [OMPI_AFFINITY_STRING_MAX ] = {0 };
185
183
186
184
first = true;
187
185
range_start = -999 ;
@@ -207,7 +205,7 @@ static char *bitmap2rangestr(int bitmap)
207
205
snprintf (tmp , stmp , "%d-%d" , range_start , range_end );
208
206
}
209
207
size_t ret_len = strlen (ret );
210
- snprintf (ret + ret_len , BUFSIZ - ret_len , "%s" , tmp );
208
+ snprintf (ret + ret_len , sizeof ( ret ) - ret_len , "%s" , tmp );
211
209
212
210
range_start = -999 ;
213
211
}
@@ -235,7 +233,7 @@ static char *bitmap2rangestr(int bitmap)
235
233
snprintf (tmp , stmp , "%d-%d" , range_start , range_end );
236
234
}
237
235
size_t ret_len = strlen (ret );
238
- snprintf (ret + ret_len , BUFSIZ - ret_len , "%s" , tmp );
236
+ snprintf (ret + ret_len , sizeof ( ret ) - ret_len , "%s" , tmp );
239
237
}
240
238
241
239
return ret ;
@@ -249,7 +247,7 @@ static int cset2str(char *str, int len, hwloc_topology_t topo, hwloc_cpuset_t cp
249
247
bool first ;
250
248
int num_sockets , num_cores ;
251
249
int ret , socket_index , core_index ;
252
- char tmp [BUFSIZ ] ;
250
+ char tmp [OMPI_AFFINITY_STRING_MAX - 1 ] = { 0 } ;
253
251
const int stmp = sizeof (tmp ) - 1 ;
254
252
int * * map = NULL ;
255
253
@@ -350,7 +348,7 @@ static bool is_single_cpu(hwloc_cpuset_t cpuset)
350
348
*/
351
349
static int cset2mapstr (char * str , int len , hwloc_topology_t topo , hwloc_cpuset_t cpuset )
352
350
{
353
- char tmp [BUFSIZ ] ;
351
+ char tmp [OMPI_AFFINITY_STRING_MAX - 1 ] = { 0 } ;
354
352
int core_index , pu_index ;
355
353
const int stmp = sizeof (tmp ) - 1 ;
356
354
hwloc_obj_t socket , core , pu ;
@@ -432,7 +430,7 @@ static int get_rsrc_current_binding(char str[OMPI_AFFINITY_STRING_MAX])
432
430
433
431
/* If we are not bound, indicate that */
434
432
if (!bound ) {
435
- strncat (str , not_bound_str , OMPI_AFFINITY_STRING_MAX - 1 );
433
+ strncat (str , not_bound_str , OMPI_AFFINITY_STRING_MAX - strlen ( str ) - 1 );
436
434
ret = OMPI_SUCCESS ;
437
435
}
438
436
@@ -461,7 +459,7 @@ static int get_rsrc_exists(char str[OMPI_AFFINITY_STRING_MAX])
461
459
{
462
460
bool first = true;
463
461
int i , num_cores , num_pus ;
464
- char tmp [OMPI_AFFINITY_STRING_MAX ] ;
462
+ char tmp [OMPI_AFFINITY_STRING_MAX - 1 ] = { 0 } ;
465
463
const int stmp = sizeof (tmp ) - 1 ;
466
464
hwloc_obj_t socket , core , c2 ;
467
465
@@ -471,12 +469,12 @@ static int get_rsrc_exists(char str[OMPI_AFFINITY_STRING_MAX])
471
469
NULL != socket ; socket = socket -> next_cousin ) {
472
470
/* If this isn't the first socket, add a delimiter */
473
471
if (!first ) {
474
- strncat (str , "; " , OMPI_AFFINITY_STRING_MAX - strlen (str ));
472
+ strncat (str , "; " , OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
475
473
}
476
474
first = false;
477
475
478
476
snprintf (tmp , stmp , "socket %d has " , socket -> os_index );
479
- strncat (str , tmp , OMPI_AFFINITY_STRING_MAX - strlen (str ));
477
+ strncat (str , tmp , OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
480
478
481
479
/* Find out how many cores are inside this socket, and get an
482
480
object pointing to the first core. Also count how many PUs
@@ -496,13 +494,13 @@ static int get_rsrc_exists(char str[OMPI_AFFINITY_STRING_MAX])
496
494
/* Only 1 core */
497
495
if (1 == num_cores ) {
498
496
strncat (str , "1 core with " ,
499
- OMPI_AFFINITY_STRING_MAX - strlen (str ));
497
+ OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
500
498
if (1 == num_pus ) {
501
499
strncat (str , "1 hwt" ,
502
- OMPI_AFFINITY_STRING_MAX - strlen (str ));
500
+ OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
503
501
} else {
504
502
snprintf (tmp , stmp , "%d hwts" , num_pus );
505
- strncat (str , tmp , OMPI_AFFINITY_STRING_MAX - strlen (str ));
503
+ strncat (str , tmp , OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
506
504
}
507
505
}
508
506
@@ -511,7 +509,7 @@ static int get_rsrc_exists(char str[OMPI_AFFINITY_STRING_MAX])
511
509
bool same = true;
512
510
513
511
snprintf (tmp , stmp , "%d cores" , num_cores );
514
- strncat (str , tmp , OMPI_AFFINITY_STRING_MAX - strlen (str ));
512
+ strncat (str , tmp , OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
515
513
516
514
/* Do all the cores have the same number of PUs? */
517
515
for (c2 = core ; NULL != c2 ; c2 = c2 -> next_cousin ) {
@@ -527,32 +525,32 @@ static int get_rsrc_exists(char str[OMPI_AFFINITY_STRING_MAX])
527
525
/* Yes, they all have the same number of PUs */
528
526
if (same ) {
529
527
snprintf (tmp , stmp , ", each with %d hwt" , num_pus );
530
- strncat (str , tmp , OMPI_AFFINITY_STRING_MAX - strlen (str ));
528
+ strncat (str , tmp , OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
531
529
if (num_pus != 1 ) {
532
- strncat (str , "s" , OMPI_AFFINITY_STRING_MAX - strlen (str ));
530
+ strncat (str , "s" , OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
533
531
}
534
532
}
535
533
536
534
/* No, they have differing numbers of PUs */
537
535
else {
538
536
bool first_iter = true;
539
537
540
- strncat (str , "with (" , OMPI_AFFINITY_STRING_MAX - strlen (str ));
538
+ strncat (str , "with (" , OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
541
539
for (c2 = core ; NULL != c2 ; c2 = c2 -> next_cousin ) {
542
540
if (!first_iter ) {
543
541
strncat (str , ", " ,
544
- OMPI_AFFINITY_STRING_MAX - strlen (str ));
542
+ OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
545
543
}
546
544
first_iter = false;
547
545
548
546
i = hwloc_get_nbobjs_inside_cpuset_by_type (opal_hwloc_topology ,
549
547
core -> cpuset ,
550
548
HWLOC_OBJ_PU );
551
549
snprintf (tmp , stmp , "%d" , i );
552
- strncat (str , tmp , OMPI_AFFINITY_STRING_MAX - strlen (str ));
550
+ strncat (str , tmp , OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
553
551
}
554
552
strncat (str , ") hwts" ,
555
- OMPI_AFFINITY_STRING_MAX - strlen (str ));
553
+ OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
556
554
}
557
555
}
558
556
}
@@ -620,7 +618,7 @@ static int get_layout_current_binding(char str[OMPI_AFFINITY_STRING_MAX])
620
618
621
619
/* If we are not bound, indicate that */
622
620
if (!bound ) {
623
- strncat (str , not_bound_str , OMPI_AFFINITY_STRING_MAX - 1 );
621
+ strncat (str , not_bound_str , OMPI_AFFINITY_STRING_MAX - strlen ( str ) - 1 );
624
622
ret = OMPI_SUCCESS ;
625
623
}
626
624
@@ -652,7 +650,6 @@ static int get_layout_current_binding(char str[OMPI_AFFINITY_STRING_MAX])
652
650
static int get_layout_exists (char str [OMPI_AFFINITY_STRING_MAX ])
653
651
{
654
652
int core_index , pu_index ;
655
- int len = OMPI_AFFINITY_STRING_MAX ;
656
653
hwloc_obj_t socket , core , pu ;
657
654
658
655
str [0 ] = '\0' ;
@@ -662,7 +659,7 @@ static int get_layout_exists(char str[OMPI_AFFINITY_STRING_MAX])
662
659
HWLOC_OBJ_SOCKET , 0 );
663
660
NULL != socket ;
664
661
socket = socket -> next_cousin ) {
665
- strncat (str , "[" , len - strlen (str ));
662
+ strncat (str , "[" , OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
666
663
667
664
/* Iterate over all existing cores in this socket */
668
665
core_index = 0 ;
@@ -674,7 +671,7 @@ static int get_layout_exists(char str[OMPI_AFFINITY_STRING_MAX])
674
671
socket -> cpuset ,
675
672
HWLOC_OBJ_CORE , ++ core_index )) {
676
673
if (core_index > 0 ) {
677
- strncat (str , "/" , len - strlen (str ));
674
+ strncat (str , "/" , OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
678
675
}
679
676
680
677
/* Iterate over all existing PUs in this core */
@@ -686,10 +683,10 @@ static int get_layout_exists(char str[OMPI_AFFINITY_STRING_MAX])
686
683
pu = hwloc_get_obj_inside_cpuset_by_type (opal_hwloc_topology ,
687
684
core -> cpuset ,
688
685
HWLOC_OBJ_PU , ++ pu_index )) {
689
- strncat (str , "." , len - strlen (str ));
686
+ strncat (str , "." , OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
690
687
}
691
688
}
692
- strncat (str , "]" , len - strlen (str ));
689
+ strncat (str , "]" , OMPI_AFFINITY_STRING_MAX - strlen (str ) - 1 );
693
690
}
694
691
695
692
return OMPI_SUCCESS ;
0 commit comments