@@ -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 (getenv (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 &&
@@ -3669,7 +3672,7 @@ static int com_go(String *buffer, char *)
36693672 }
36703673
36713674 if (verbose >= 3 || !opt_silent)
3672- end_timer (timer, time_buff);
3675+ end_timer (timer, time_buff, sizeof (time_buff) );
36733676 else
36743677 time_buff[0 ]= ' \0 ' ;
36753678
@@ -3705,9 +3708,9 @@ static int com_go(String *buffer, char *)
37053708 print_tab_data (result);
37063709 else
37073710 print_table_data (result);
3708- snprintf (buff, sizeof (buff), " %ld %s in set" ,
3709- (long ) mysql_num_rows (result),
3710- ( long ) mysql_num_rows (result) == 1 ? " row" : " rows" );
3711+ snprintf (buff, sizeof (buff), " %llu %s in set" ,
3712+ (unsigned long long ) mysql_num_rows (result),
3713+ mysql_num_rows (result) == 1 ? " row" : " rows" );
37113714 end_pager ();
37123715 if (mysql_errno (&mysql))
37133716 {
@@ -3721,7 +3724,7 @@ static int com_go(String *buffer, char *)
37213724 strmov (buff," Query OK" );
37223725 else
37233726 snprintf (buff, sizeof (buff), " Query OK, %llu %s affected" ,
3724- mysql_affected_rows (&mysql),
3727+ ( unsigned long long ) mysql_affected_rows (&mysql),
37253728 mysql_affected_rows (&mysql) == 1 ? " row" : " rows" );
37263729
37273730 pos=strend (buff);
@@ -3900,7 +3903,7 @@ static char *fieldflags2str(uint f) {
39003903 ff2s_check_flag (ON_UPDATE_NOW );
39013904#undef ff2s_check_flag
39023905 if (f)
3903- snprintf (s, sizeof (buf), " unknows =0x%04x" , f);
3906+ snprintf (s, sizeof (buf) - ( size_t )(s - buf) , " unknown =0x%04x" , f);
39043907 return buf;
39053908}
39063909
@@ -4641,8 +4644,10 @@ com_edit(String *buffer,char *)
46414644 strxmov (buff,editor," " ,filename,NullS);
46424645 if ((error= system (buff)))
46434646 {
4644- char errmsg[100 ];
4645- snprintf (errmsg, sizeof (errmsg), " Command '%.40s' failed" , buff);
4647+ #define EDITOR_FAIL_MSG " Command '%.40s' failed"
4648+ char errmsg[sizeof (EDITOR_FAIL_MSG ) - 1 + 40 ];
4649+ snprintf (errmsg, sizeof (errmsg), EDITOR_FAIL_MSG , buff);
4650+ #undef EDITOR_FAIL_MSG
46464651 put_info (errmsg, INFO_ERROR , 0 , NullS);
46474652 goto err;
46484653 }
@@ -5338,7 +5343,7 @@ static int com_status(String *, char *)
53385343 tee_fprintf (stdout, " %.*s\t\t\t " , (int ) (pos-status_str), status_str);
53395344 if ((status_str= str2int (pos,10 ,0 ,LONG_MAX ,(long *) &sec)))
53405345 {
5341- nice_time ((double ) sec,buff,0 );
5346+ nice_time ((double ) sec,buff, sizeof (buff), 0 );
53425347 tee_puts (buff, stdout); /* print nice time */
53435348 while (*status_str == ' ' )
53445349 status_str++; /* to next info */
@@ -5557,8 +5562,10 @@ void tee_putc(int c, FILE *file)
55575562
55585563 len("4294967296 days, 23 hours, 59 minutes, 60.000 seconds") -> 53
55595564*/
5560- static void nice_time (double sec, char *buff, bool part_second)
5565+ static void nice_time (double sec, char *buff, size_t buff_size,
5566+ bool part_second)
55615567{
5568+ char *buff_end= buff + buff_size;
55625569 ulong tmp;
55635570 if (sec >= 3600.0 *24 )
55645571 {
@@ -5582,21 +5589,23 @@ static void nice_time(double sec, char *buff, bool part_second)
55825589 buff=strmov (buff," min " );
55835590 }
55845591 if (part_second)
5585- sprintf (buff," %.3f sec" ,sec);
5592+ snprintf (buff, buff_end - buff, " %.3f sec" , sec);
55865593 else
5587- sprintf (buff," %d sec" ,(int ) sec);
5594+ snprintf (buff, buff_end - buff, " %d sec" , (int ) sec);
55885595}
55895596
55905597
5591- static void end_timer (ulonglong start_time, char *buff)
5598+ static void end_timer (ulonglong start_time, char *buff, size_t buff_size )
55925599{
55935600 double sec;
55945601
5602+ if (buff_size < 4 )
5603+ return ;
55955604 buff[0 ]=' ' ;
55965605 buff[1 ]=' (' ;
55975606 sec= (microsecond_interval_timer () - start_time) / (double ) (1000 * 1000 );
5598- nice_time (sec, buff + 2 , 1 );
5599- strmov (strend (buff)," )" );
5607+ nice_time (sec, buff + 2 , buff_size - 2 , 1 );
5608+ snprintf (strend (buff), buff_size - ( strend (buff) - buff), " )" );
56005609}
56015610
56025611static const char *construct_prompt ()
0 commit comments