From 504e7918d2329aa98953c0d1f76abdbdf184b3a5 Mon Sep 17 00:00:00 2001 From: Colin Petrie Date: Thu, 18 Aug 2016 17:35:31 +0200 Subject: [PATCH] Description: Fix undefind sprintf usage Author: Christoph Biedl Last-Update: 2016-07-18 Documentation states appending to a buffer using sprintf (buf, "%s.foo", buf); results in undefined behaviour. Work around it. --- bgpdump.c | 7 ++++--- util.c | 4 ++-- util.h | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/bgpdump.c b/bgpdump.c index 5b70386..a77158f 100644 --- a/bgpdump.c +++ b/bgpdump.c @@ -273,19 +273,20 @@ void process(BGPDUMP_ENTRY *entry) { char prefix[BGPDUMP_ADDRSTRLEN]; char *bgp4mp_format; char *bgp4mp_subtype_format; + int len; date=gmtime(&entry->time); time2str(date,time_str_fixed); if (mode == 1) { // Timestamp mode - sprintf(time_str, "%lld", (long long)entry->time); + len = sprintf(time_str, "%lld", (long long)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) diff --git a/util.c b/util.c index 84438dd..d6db86c 100644 --- a/util.c +++ b/util.c @@ -66,9 +66,9 @@ void err(const char *fmt, ...) { log(ERR, error); } 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); } diff --git a/util.h b/util.h index 9ebd6f7..a8cf95b 100644 --- a/util.h +++ b/util.h @@ -38,7 +38,7 @@ char *fmt_ipv4(BGPDUMP_IP_ADDRESS addr, char *buffer); 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);