@@ -325,9 +325,9 @@ abbreviate_list_into_string(char *str, int max, int *list, int nlist)
325325                    strcpy (& str [strlen (str )], ", " );
326326                }
327327                if  (lo  !=  hi ) {
328-                     sprintf (& str [strlen (str )], "%d - %d" , lo , hi );
328+                     snprintf (& str [strlen (str )],  max   -   strlen ( str ) , "%d - %d" , lo , hi );
329329                } else  {
330-                     sprintf (& str [strlen (str )], "%d" , lo );
330+                     snprintf (& str [strlen (str )],  max   -   strlen ( str ) , "%d" , lo );
331331                }
332332            }
333333/* 
@@ -352,9 +352,9 @@ abbreviate_list_into_string(char *str, int max, int *list, int nlist)
352352            strcpy (& str [strlen (str )], ", " );
353353        }
354354        if  (lo  !=  hi ) {
355-             sprintf (& str [strlen (str )], "%d - %d" , lo , hi );
355+             snprintf (& str [strlen (str )],  max   -   strlen ( str ) , "%d - %d" , lo , hi );
356356        } else  {
357-             sprintf (& str [strlen (str )], "%d" , lo );
357+             snprintf (& str [strlen (str )],  max   -   strlen ( str ) , "%d" , lo );
358358        }
359359    }
360360}
@@ -460,7 +460,7 @@ ompi_report_comm_methods(int called_from_location)
460460
461461        len  =  strlen (opal_process_info .nodename ) +  100 ;
462462        hoststring   =  malloc (len  +  1 );
463-         sprintf (hoststring , "Host %d [%s] ranks " ,
463+         snprintf (hoststring ,  len   +   1 , "Host %d [%s] ranks " ,
464464            myleaderrank , opal_process_info .nodename );
465465
466466        abbreviate_list_into_string (& hoststring [strlen (hoststring )],
@@ -548,7 +548,7 @@ ompi_report_comm_methods(int called_from_location)
548548        ompi_count_array_t  lens_desc ;
549549        ompi_disp_array_t  disps_desc ;
550550
551-         // First get the array of host strings (host names and task lists)   
551+         // First get the array of host strings (host names and task lists) 
552552        // for all nodes. 
553553        len  =  strlen (hoststring ) +  1 ;
554554        if  (myleaderrank  ==  0 ) {
@@ -642,7 +642,7 @@ ompi_report_comm_methods(int called_from_location)
642642// 2: 2d table 
643643        if  (nleaderranks  <= max2Dprottable ) {
644644            char  * str , * p ;
645-             int  tmp , per , has_ucx_transport ;
645+             int  tmp , per , has_ucx_transport ,  bufsize ;
646646            int  strlens [NUM_COMM_METHODS ];
647647
648648            // characters per entry in the 2d table, must be large enough 
@@ -668,11 +668,11 @@ ompi_report_comm_methods(int called_from_location)
668668                    if  (tmp + 1  >  per ) { per  =  tmp + 1 ; }
669669                }
670670            }
671- 
672-             str  =  malloc (nleaderranks   *   per   +   1 );
671+              bufsize   =   nleaderranks   *   per   +   1 ; 
672+             str  =  malloc (bufsize );
673673            p  =  str ;
674674            for  (i = 0 ; i < nleaderranks ; ++ i ) {
675-                 sprintf ( p , "%d" , i );
675+                 snprintf ( p ,  bufsize   -  ( p   -   str ) , "%d" , i );
676676                for  (j = (int )strlen (p ); j < per ; ++ j ) {
677677                    p [j ] =  ' ' ;
678678                }
@@ -698,12 +698,12 @@ ompi_report_comm_methods(int called_from_location)
698698                for  (k = 0 ; k < nleaderranks ; ++ k ) {
699699                    char  * method_string ;
700700                    char  ucx_label [20 ];
701-                      
701+ 
702702                    method_string  =  comm_method_to_string (method [i  *  nleaderranks  +  k ]);
703703                    if  (0  ==  strncmp (method_string , UCX_TAG , strlen (UCX_TAG ))) {
704704                        n  =  lookup_string_in_conversion_struct (& comm_method_string_conversion ,
705705                                                               method_string );
706-                         sprintf (ucx_label , "ucx[%3d]" , n );
706+                         snprintf (ucx_label ,  sizeof ( ucx_label ) , "ucx[%3d]" , n );
707707                        strcat (p , ucx_label );
708708                        methods_used [n  / 8 ] |= (1  << (n  % 8 ));
709709                        has_ucx_transport  =  1 ;
@@ -755,7 +755,7 @@ ompi_report_comm_methods(int called_from_location)
755755        }
756756        else  if  (nleaderranks  <= max2D1Cprottable ) {
757757            char  * str , * p ;
758-             int  tmp , per , done ;
758+             int  tmp , per , done ,  bufsize ;
759759            char  char_code [NUM_COMM_METHODS ], next_char ;
760760            int  method_count [NUM_COMM_METHODS ];
761761
@@ -798,12 +798,13 @@ ompi_report_comm_methods(int called_from_location)
798798                }
799799            }
800800
801-             str  =  malloc (per  +  32  +  nleaderranks  *  2  +  1 );
801+             bufsize  =  per  +  32  +  nleaderranks  *  2  +  1 ;
802+             str  =  malloc (bufsize );
802803            p  =  str ;
803-             sprintf (p , "0 1 2 3 " );
804+             snprintf (p ,  bufsize ,   "0 1 2 3 " );
804805            p  +=  8 ;
805806            for  (i = 4 ; i < nleaderranks ; i += 4 ) {
806-                 sprintf ( p , "%d" , i );
807+                 snprintf ( p ,  bufsize   -  ( p   -   str ) , "%d" , i );
807808                for  (j = (int )strlen (p ); j < 8 ; ++ j ) {
808809                    p [j ] =  ' ' ;
809810                }
@@ -972,15 +973,16 @@ ompi_report_comm_methods(int called_from_location)
972973                        }
973974                    }
974975                    if  (is_nonconformist ) {
975-                         char   * str   =   malloc ( 1024 ) ;
976- //                       int first = 1 ;
977-                         sprintf (str , "  host %d:" , i );
976+                         const   size_t   bufsize   =   1024 ;
977+                          char   * str   =   malloc ( bufsize ) ;
978+                         snprintf (str ,  bufsize , "  host %d:" , i );
978979                        for  (k = 0 ; k < NUM_COMM_METHODS ; ++ k ) {
979980                            if  (method_count [k ] >  0 ) {
980981//                              if (!first) { 
981982//                                  strcat(str, " /"); 
982983//                              } 
983-                                 sprintf (& str [strlen (str )],
984+                                 snprintf (& str [strlen (str )],
985+                                     1024  -  strlen (str ),
984986                                    " [%dx %s]" ,
985987                                    method_count [k ],
986988                                    comm_method_to_string (k ));
0 commit comments