fix-sprintf-append.patch 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. Description: Fix undefined sprintf usage
  2. Author: Christoph Biedl <debian.axhn@manchmal.in-ulm.de>
  3. Bug: https://bitbucket.org/ripencc/bgpdump/issues/33/
  4. Last-Update: 2016-07-18
  5. Documentation states appending to a buffer using
  6. sprintf (buf, "%s.foo", buf);
  7. results in undefined behaviour. Work around it.
  8. --- a/bgpdump.c
  9. +++ b/bgpdump.c
  10. @@ -261,19 +261,20 @@
  11. char time_str_fixed[128];
  12. char prefix[BGPDUMP_ADDRSTRLEN];
  13. char *bgp4mp_format;
  14. + int len;
  15. date=gmtime(&entry->time);
  16. time2str(date,time_str_fixed);
  17. if (mode == 1) {
  18. // Timestamp mode
  19. - sprintf(time_str, "%ld", entry->time);
  20. + len = sprintf(time_str, "%ld", entry->time);
  21. } else {
  22. - time2str(date,time_str);
  23. + len = time2str(date,time_str);
  24. }
  25. // Appending microseconds to time_str if needed
  26. if (entry->type == BGPDUMP_TYPE_ZEBRA_BGP_ET) {
  27. - sprintf(time_str, "%s.%06ld", time_str, entry->ms);
  28. + sprintf(time_str + len, ".%06ld", entry->ms);
  29. }
  30. if (mode==0)
  31. --- a/util.c
  32. +++ b/util.c
  33. @@ -66,9 +66,9 @@
  34. void warn(const char *fmt, ...) { log(WARNING, warn); }
  35. void debug(const char *fmt, ...) { log(INFO, info); }
  36. -void time2str(struct tm* date,char *time_str)
  37. +int time2str(struct tm* date,char *time_str)
  38. {
  39. - sprintf(time_str, "%02d/%02d/%02d %02d:%02d:%02d", date->tm_mon+1, date->tm_mday, date->tm_year%100,
  40. + return sprintf(time_str, "%02d/%02d/%02d %02d:%02d:%02d", date->tm_mon+1, date->tm_mday, date->tm_year%100,
  41. date->tm_hour, date->tm_min, date->tm_sec);
  42. }
  43. --- a/util.h
  44. +++ b/util.h
  45. @@ -38,7 +38,7 @@
  46. char *fmt_ipv6(BGPDUMP_IP_ADDRESS addr, char *buffer);
  47. void test_fmt_ip(void);
  48. -void time2str(struct tm* date,char *time_str);
  49. +int time2str(struct tm* date,char *time_str);
  50. int int2str(uint32_t value, char* str);
  51. void test_utils(void);