Skip to content

Commit

Permalink
Description: Fix undefind sprintf usage
Browse files Browse the repository at this point in the history
Author: Christoph Biedl <[email protected]>
Last-Update: 2016-07-18

    Documentation states appending to a buffer using
        sprintf (buf, "%s.foo", buf);
    results in undefined behaviour. Work around it.
  • Loading branch information
spakka committed Aug 18, 2016
1 parent c57fb8c commit 504e791
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
7 changes: 4 additions & 3 deletions bgpdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 504e791

Please sign in to comment.