Skip to content

Commit f71ea4d

Browse files
mshabuninalalek
authored andcommitted
Merge pull request opencv#8816 from mshabunin:sprintf-fix
Fixed snprintf for VS 2013 (opencv#8816) * Fixed snprintf for VS 2013 * snprintf: removed declaration from header, changed implementation * cv_snprintf corrected according to comments * update snprintf patch
1 parent 515e01e commit f71ea4d

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

modules/core/src/lpsolver.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#include "precomp.hpp"
4242
#include <climits>
4343
#include <algorithm>
44-
#include <cstdarg>
4544

4645
#define dprintf(x)
4746
#define print_matrix(x)

modules/core/src/out.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ namespace cv
103103
}
104104
else
105105
{
106-
snprintf(floatFormat, 8, "%%.%dg", std::min(precision, 20));
106+
cv_snprintf(floatFormat, sizeof(floatFormat), "%%.%dg", std::min(precision, 20));
107107
}
108108

109109
switch(mtx.depth())

modules/core/src/precomp.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include <float.h>
6666
#include <limits.h>
6767
#include <math.h>
68+
#include <stdarg.h>
6869
#include <stdio.h>
6970
#include <stdlib.h>
7071
#include <string.h>
@@ -319,6 +320,8 @@ cv::Mutex& getInitializationMutex();
319320
#define CV_SINGLETON_LAZY_INIT(TYPE, INITIALIZER) CV_SINGLETON_LAZY_INIT_(TYPE, INITIALIZER, instance)
320321
#define CV_SINGLETON_LAZY_INIT_REF(TYPE, INITIALIZER) CV_SINGLETON_LAZY_INIT_(TYPE, INITIALIZER, *instance)
321322

323+
int cv_snprintf(char* buf, int len, const char* fmt, ...);
324+
int cv_vsnprintf(char* buf, int len, const char* fmt, va_list args);
322325
}
323326

324327
#endif /*_CXCORE_INTERNAL_H_*/

modules/core/src/system.cpp

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,6 @@ std::wstring GetTempFileNameWinRT(std::wstring prefix)
196196
#include "omp.h"
197197
#endif
198198

199-
#include <stdarg.h>
200-
201199
#if defined __linux__ || defined __APPLE__ || defined __EMSCRIPTEN__ || defined __FreeBSD__
202200
#include <unistd.h>
203201
#include <stdio.h>
@@ -752,16 +750,13 @@ String format( const char* fmt, ... )
752750
va_list va;
753751
va_start(va, fmt);
754752
int bsize = static_cast<int>(buf.size());
755-
#if defined _MSC_VER && __cplusplus < 201103L
756-
int len = _vsnprintf_s((char *)buf, bsize, _TRUNCATE, fmt, va);
757-
#else
758-
int len = vsnprintf((char *)buf, bsize, fmt, va);
759-
#endif
753+
int len = cv_vsnprintf((char *)buf, bsize, fmt, va);
760754
va_end(va);
761755

762-
if (len < 0 || len >= bsize)
756+
CV_Assert(len >= 0 && "Check format string for errors");
757+
if (len >= bsize)
763758
{
764-
buf.resize(std::max(bsize << 1, len + 1));
759+
buf.resize(len + 1);
765760
continue;
766761
}
767762
buf[bsize - 1] = 0;
@@ -856,19 +851,33 @@ bool setBreakOnError(bool value)
856851
return prevVal;
857852
}
858853

859-
static bool cv_snprintf(char* buf, int len, const char* fmt, ...)
854+
int cv_snprintf(char* buf, int len, const char* fmt, ...)
860855
{
861856
va_list va;
862857
va_start(va, fmt);
863-
#if defined _MSC_VER && __cplusplus < 201103L
864-
int res = _vsnprintf_s((char *)buf, len - 1, _TRUNCATE, fmt, va);
858+
int res = cv_vsnprintf(buf, len, fmt, va);
865859
va_end(va);
866-
buf[len - 1] = 0;
867-
return res >= 0 && res < len - 1;
860+
return res;
861+
}
862+
863+
int cv_vsnprintf(char* buf, int len, const char* fmt, va_list args)
864+
{
865+
#if defined _MSC_VER
866+
if (len <= 0) return len == 0 ? 1024 : -1;
867+
int res = _vsnprintf_s(buf, len, _TRUNCATE, fmt, args);
868+
// ensure null terminating on VS
869+
if (res >= 0 && res < len)
870+
{
871+
buf[res] = 0;
872+
return res;
873+
}
874+
else
875+
{
876+
buf[len - 1] = 0; // truncate happened
877+
return res >= len ? res : (len * 2);
878+
}
868879
#else
869-
int res = vsnprintf((char *)buf, len, fmt, va);
870-
va_end(va);
871-
return res >= 0 && res < len;
880+
return vsnprintf(buf, len, fmt, args);
872881
#endif
873882
}
874883

0 commit comments

Comments
 (0)