Commit f71ea4df authored by Maksim Shabunin's avatar Maksim Shabunin Committed by Alexander Alekhin

Merge pull request #8816 from mshabunin:sprintf-fix

Fixed snprintf for VS 2013 (#8816)

* Fixed snprintf for VS 2013

* snprintf: removed declaration from header, changed implementation

* cv_snprintf corrected according to comments

* update snprintf patch
parent 515e01e6
...@@ -41,7 +41,6 @@ ...@@ -41,7 +41,6 @@
#include "precomp.hpp" #include "precomp.hpp"
#include <climits> #include <climits>
#include <algorithm> #include <algorithm>
#include <cstdarg>
#define dprintf(x) #define dprintf(x)
#define print_matrix(x) #define print_matrix(x)
......
...@@ -103,7 +103,7 @@ namespace cv ...@@ -103,7 +103,7 @@ namespace cv
} }
else else
{ {
snprintf(floatFormat, 8, "%%.%dg", std::min(precision, 20)); cv_snprintf(floatFormat, sizeof(floatFormat), "%%.%dg", std::min(precision, 20));
} }
switch(mtx.depth()) switch(mtx.depth())
......
...@@ -65,6 +65,7 @@ ...@@ -65,6 +65,7 @@
#include <float.h> #include <float.h>
#include <limits.h> #include <limits.h>
#include <math.h> #include <math.h>
#include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -319,6 +320,8 @@ cv::Mutex& getInitializationMutex(); ...@@ -319,6 +320,8 @@ cv::Mutex& getInitializationMutex();
#define CV_SINGLETON_LAZY_INIT(TYPE, INITIALIZER) CV_SINGLETON_LAZY_INIT_(TYPE, INITIALIZER, instance) #define CV_SINGLETON_LAZY_INIT(TYPE, INITIALIZER) CV_SINGLETON_LAZY_INIT_(TYPE, INITIALIZER, instance)
#define CV_SINGLETON_LAZY_INIT_REF(TYPE, INITIALIZER) CV_SINGLETON_LAZY_INIT_(TYPE, INITIALIZER, *instance) #define CV_SINGLETON_LAZY_INIT_REF(TYPE, INITIALIZER) CV_SINGLETON_LAZY_INIT_(TYPE, INITIALIZER, *instance)
int cv_snprintf(char* buf, int len, const char* fmt, ...);
int cv_vsnprintf(char* buf, int len, const char* fmt, va_list args);
} }
#endif /*_CXCORE_INTERNAL_H_*/ #endif /*_CXCORE_INTERNAL_H_*/
...@@ -196,8 +196,6 @@ std::wstring GetTempFileNameWinRT(std::wstring prefix) ...@@ -196,8 +196,6 @@ std::wstring GetTempFileNameWinRT(std::wstring prefix)
#include "omp.h" #include "omp.h"
#endif #endif
#include <stdarg.h>
#if defined __linux__ || defined __APPLE__ || defined __EMSCRIPTEN__ || defined __FreeBSD__ #if defined __linux__ || defined __APPLE__ || defined __EMSCRIPTEN__ || defined __FreeBSD__
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
...@@ -752,16 +750,13 @@ String format( const char* fmt, ... ) ...@@ -752,16 +750,13 @@ String format( const char* fmt, ... )
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
int bsize = static_cast<int>(buf.size()); int bsize = static_cast<int>(buf.size());
#if defined _MSC_VER && __cplusplus < 201103L int len = cv_vsnprintf((char *)buf, bsize, fmt, va);
int len = _vsnprintf_s((char *)buf, bsize, _TRUNCATE, fmt, va);
#else
int len = vsnprintf((char *)buf, bsize, fmt, va);
#endif
va_end(va); va_end(va);
if (len < 0 || len >= bsize) CV_Assert(len >= 0 && "Check format string for errors");
if (len >= bsize)
{ {
buf.resize(std::max(bsize << 1, len + 1)); buf.resize(len + 1);
continue; continue;
} }
buf[bsize - 1] = 0; buf[bsize - 1] = 0;
...@@ -856,19 +851,33 @@ bool setBreakOnError(bool value) ...@@ -856,19 +851,33 @@ bool setBreakOnError(bool value)
return prevVal; return prevVal;
} }
static bool cv_snprintf(char* buf, int len, const char* fmt, ...) int cv_snprintf(char* buf, int len, const char* fmt, ...)
{ {
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
#if defined _MSC_VER && __cplusplus < 201103L int res = cv_vsnprintf(buf, len, fmt, va);
int res = _vsnprintf_s((char *)buf, len - 1, _TRUNCATE, fmt, va);
va_end(va); va_end(va);
buf[len - 1] = 0; return res;
return res >= 0 && res < len - 1; }
int cv_vsnprintf(char* buf, int len, const char* fmt, va_list args)
{
#if defined _MSC_VER
if (len <= 0) return len == 0 ? 1024 : -1;
int res = _vsnprintf_s(buf, len, _TRUNCATE, fmt, args);
// ensure null terminating on VS
if (res >= 0 && res < len)
{
buf[res] = 0;
return res;
}
else
{
buf[len - 1] = 0; // truncate happened
return res >= len ? res : (len * 2);
}
#else #else
int res = vsnprintf((char *)buf, len, fmt, va); return vsnprintf(buf, len, fmt, args);
va_end(va);
return res >= 0 && res < len;
#endif #endif
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment