@@ -1166,8 +1166,9 @@ static void print_tab_data(MYSQL_RES *result);
11661166static void print_table_data_vertically (MYSQL_RES *result);
11671167static void print_warnings (void );
11681168static void print_last_query_cost (void );
1169- static void end_timer (ulonglong start_time, char *buff);
1170- static void nice_time (double sec,char *buff,bool part_second);
1169+ static void end_timer (ulonglong start_time, char *buff, size_t buff_size);
1170+ static void nice_time (double sec, char *buff, size_t buff_size,
1171+ bool part_second);
11711172extern " C" sig_handler mysql_end (int sig) __attribute__ ((noreturn));
11721173extern " C" sig_handler handle_sigint (int sig);
11731174#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
@@ -1425,18 +1426,20 @@ int main(int argc,char *argv[])
14251426 histfile=my_strdup (PSI_NOT_INSTRUMENTED , histfile, MYF (MY_WME ));
14261427 else if ((home= getenv (" HOME" )))
14271428 {
1429+ size_t histfile_size=
1430+ strlen (home) + strlen (" /.mysql_history" ) + 2 ;
14281431 histfile=(char *) my_malloc (PSI_NOT_INSTRUMENTED ,
1429- strlen (home) + strlen ( " /.mariadb_history " )+ 2 , MYF (MY_WME ));
1432+ histfile_size , MYF (MY_WME ));
14301433 if (histfile)
14311434 {
1432- sprintf (histfile," %s/.mariadb_history" , home);
1435+ snprintf (histfile, histfile_size, " %s/.mariadb_history" , home);
14331436 if (my_access (histfile, F_OK ))
14341437 {
14351438 /* no .mariadb_history, look for historical name and use if present */
1436- sprintf (histfile," %s/.mysql_history" , home);
1439+ snprintf (histfile, histfile_size, " %s/.mysql_history" , home);
14371440 /* and go back to original if not found */
14381441 if (my_access (histfile, F_OK ))
1439- sprintf (histfile," %s/.mariadb_history" , home);
1442+ snprintf (histfile, histfile_size, " %s/.mariadb_history" , home);
14401443 }
14411444 char link_name[FN_REFLEN ];
14421445 if (my_readlink (link_name, histfile, 0 ) == 0 &&
@@ -3673,7 +3676,7 @@ static int com_go(String *buffer, char *)
36733676
36743677 report_progress_end ();
36753678 if (verbose >= 3 || !opt_silent)
3676- end_timer (timer, time_buff);
3679+ end_timer (timer, time_buff, sizeof (time_buff) );
36773680 else
36783681 time_buff[0 ]= ' \0 ' ;
36793682
@@ -3709,9 +3712,9 @@ static int com_go(String *buffer, char *)
37093712 print_tab_data (result);
37103713 else
37113714 print_table_data (result);
3712- snprintf (buff, sizeof (buff), " %ld %s in set" ,
3713- (long ) mysql_num_rows (result),
3714- ( long ) mysql_num_rows (result) == 1 ? " row" : " rows" );
3715+ snprintf (buff, sizeof (buff), " %llu %s in set" ,
3716+ (unsigned long long ) mysql_num_rows (result),
3717+ mysql_num_rows (result) == 1 ? " row" : " rows" );
37153718 end_pager ();
37163719 if (mysql_errno (&mysql))
37173720 {
@@ -3725,7 +3728,7 @@ static int com_go(String *buffer, char *)
37253728 strmov (buff," Query OK" );
37263729 else
37273730 snprintf (buff, sizeof (buff), " Query OK, %llu %s affected" ,
3728- mysql_affected_rows (&mysql),
3731+ ( unsigned long long ) mysql_affected_rows (&mysql),
37293732 mysql_affected_rows (&mysql) == 1 ? " row" : " rows" );
37303733
37313734 pos=strend (buff);
@@ -3904,7 +3907,7 @@ static char *fieldflags2str(uint f) {
39043907 ff2s_check_flag (ON_UPDATE_NOW );
39053908#undef ff2s_check_flag
39063909 if (f)
3907- snprintf (s, sizeof (buf), " unknows =0x%04x" , f);
3910+ snprintf (s, sizeof (buf) - ( size_t )(s - buf) , " unknown =0x%04x" , f);
39083911 return buf;
39093912}
39103913
@@ -4655,8 +4658,10 @@ com_edit(String *buffer,char *)
46554658 strxmov (buff,editor," " ,filename,NullS);
46564659 if ((error= system (buff)))
46574660 {
4658- char errmsg[100 ];
4659- snprintf (errmsg, sizeof (errmsg), " Command '%.40s' failed" , buff);
4661+ #define EDITOR_FAIL_MSG " Command '%.40s' failed"
4662+ char errmsg[sizeof (EDITOR_FAIL_MSG ) - 1 + 40 ];
4663+ snprintf (errmsg, sizeof (errmsg), EDITOR_FAIL_MSG , buff);
4664+ #undef EDITOR_FAIL_MSG
46604665 put_info (errmsg, INFO_ERROR , 0 , NullS);
46614666 goto err;
46624667 }
@@ -5352,7 +5357,7 @@ static int com_status(String *, char *)
53525357 tee_fprintf (stdout, " %.*s\t\t\t " , (int ) (pos-status_str), status_str);
53535358 if ((status_str= str2int (pos,10 ,0 ,LONG_MAX ,(long *) &sec)))
53545359 {
5355- nice_time ((double ) sec,buff,0 );
5360+ nice_time ((double ) sec,buff, sizeof (buff), 0 );
53565361 tee_puts (buff, stdout); /* print nice time */
53575362 while (*status_str == ' ' )
53585363 status_str++; /* to next info */
@@ -5571,8 +5576,10 @@ void tee_putc(int c, FILE *file)
55715576
55725577 len("4294967296 days, 23 hours, 59 minutes, 60.000 seconds") -> 53
55735578*/
5574- static void nice_time (double sec, char *buff, bool part_second)
5579+ static void nice_time (double sec, char *buff, size_t buff_size,
5580+ bool part_second)
55755581{
5582+ char *buff_end= buff + buff_size;
55765583 ulong tmp;
55775584 if (sec >= 3600.0 *24 )
55785585 {
@@ -5596,21 +5603,23 @@ static void nice_time(double sec, char *buff, bool part_second)
55965603 buff=strmov (buff," min " );
55975604 }
55985605 if (part_second)
5599- sprintf (buff," %.3f sec" ,sec);
5606+ snprintf (buff, buff_end - buff, " %.3f sec" , sec);
56005607 else
5601- sprintf (buff," %d sec" ,(int ) sec);
5608+ snprintf (buff, buff_end - buff, " %d sec" , (int ) sec);
56025609}
56035610
56045611
5605- static void end_timer (ulonglong start_time, char *buff)
5612+ static void end_timer (ulonglong start_time, char *buff, size_t buff_size )
56065613{
56075614 double sec;
56085615
5616+ if (buff_size < 4 )
5617+ return ;
56095618 buff[0 ]=' ' ;
56105619 buff[1 ]=' (' ;
56115620 sec= (microsecond_interval_timer () - start_time) / (double ) (1000 * 1000 );
5612- nice_time (sec, buff + 2 , 1 );
5613- strmov (strend (buff)," )" );
5621+ nice_time (sec, buff + 2 , buff_size - 2 , 1 );
5622+ snprintf (strend (buff), buff_size - ( strend (buff) - buff), " )" );
56145623}
56155624
56165625static const char *construct_prompt ()
0 commit comments