diff options
| author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2013-08-30 17:05:43 -0400 |
|---|---|---|
| committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2013-08-30 17:05:43 -0400 |
| commit | f3ad05347e55de2fd2a500464acde0979df9941f (patch) | |
| tree | 57376b3114e193201dcfe2d08ef2a5bdb693ad30 /node/Utils.cpp | |
| parent | 1a7e303f97507842c1a0bfffb27b76277bd43bab (diff) | |
| download | infinitytier-f3ad05347e55de2fd2a500464acde0979df9941f.tar.gz infinitytier-f3ad05347e55de2fd2a500464acde0979df9941f.zip | |
Improve code security posture by replacing sprintf with a safer function.
Diffstat (limited to 'node/Utils.cpp')
| -rw-r--r-- | node/Utils.cpp | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/node/Utils.cpp b/node/Utils.cpp index 94f66c11..0b5afcbd 100644 --- a/node/Utils.cpp +++ b/node/Utils.cpp @@ -464,7 +464,7 @@ std::string Utils::toRfc1123(uint64_t t64) #else gmtime_r(&utc,&t); #endif - sprintf(buf,"%3s, %02d %3s %4d %02d:%02d:%02d GMT",DAY_NAMES[t.tm_wday],t.tm_mday,MONTH_NAMES[t.tm_mon],t.tm_year + 1900,t.tm_hour,t.tm_min,t.tm_sec); + Utils::snprintf(buf,sizeof(buf),"%3s, %02d %3s %4d %02d:%02d:%02d GMT",DAY_NAMES[t.tm_wday],t.tm_mday,MONTH_NAMES[t.tm_mon],t.tm_year + 1900,t.tm_hour,t.tm_min,t.tm_sec); return std::string(buf); } @@ -653,4 +653,22 @@ void Utils::stdsprintf(std::string &s,const char *fmt,...) s.append(buf); } +unsigned int Utils::snprintf(char *buf,unsigned int len,const char *fmt,...) + throw(std::length_error) +{ + va_list ap; + + va_start(ap,fmt); + int n = (int)vsnprintf(buf,len,fmt,ap); + va_end(ap); + + if ((n >= (int)len)||(n < 0)) { + if (len) + buf[len - 1] = (char)0; + throw std::length_error("buf[] overflow in Utils::snprintf"); + } + + return (unsigned int)n; +} + } // namespace ZeroTier |
