Description: Fix undefined sprintf usage Author: Christoph Biedl Bug: https://bitbucket.org/ripencc/bgpdump/issues/33/ Last-Update: 2016-07-18 Documentation states appending to a buffer using sprintf (buf, "%s.foo", buf); results in undefined behaviour. Work around it. --- a/bgpdump.c +++ b/bgpdump.c @@ -261,19 +261,20 @@ char time_str_fixed[128]; char prefix[BGPDUMP_ADDRSTRLEN]; char *bgp4mp_format; + int len; date=gmtime(&entry->time); time2str(date,time_str_fixed); if (mode == 1) { // Timestamp mode - sprintf(time_str, "%ld", entry->time); + len = sprintf(time_str, "%ld", entry->time); } else { - time2str(date,time_str); + len = time2str(date,time_str); } // Appending microseconds to time_str if needed if (entry->type == BGPDUMP_TYPE_ZEBRA_BGP_ET) { - sprintf(time_str, "%s.%06ld", time_str, entry->ms); + sprintf(time_str + len, ".%06ld", entry->ms); } if (mode==0) --- a/util.c +++ b/util.c @@ -66,9 +66,9 @@ void warn(const char *fmt, ...) { log(WARNING, warn); } void debug(const char *fmt, ...) { log(INFO, info); } -void time2str(struct tm* date,char *time_str) +int time2str(struct tm* date,char *time_str) { - sprintf(time_str, "%02d/%02d/%02d %02d:%02d:%02d", date->tm_mon+1, date->tm_mday, date->tm_year%100, + return sprintf(time_str, "%02d/%02d/%02d %02d:%02d:%02d", date->tm_mon+1, date->tm_mday, date->tm_year%100, date->tm_hour, date->tm_min, date->tm_sec); } --- a/util.h +++ b/util.h @@ -38,7 +38,7 @@ char *fmt_ipv6(BGPDUMP_IP_ADDRESS addr, char *buffer); void test_fmt_ip(void); -void time2str(struct tm* date,char *time_str); +int time2str(struct tm* date,char *time_str); int int2str(uint32_t value, char* str); void test_utils(void);