@@ -196,8 +196,6 @@ std::wstring GetTempFileNameWinRT(std::wstring prefix)
196
196
#include " omp.h"
197
197
#endif
198
198
199
- #include < stdarg.h>
200
-
201
199
#if defined __linux__ || defined __APPLE__ || defined __EMSCRIPTEN__ || defined __FreeBSD__
202
200
#include < unistd.h>
203
201
#include < stdio.h>
@@ -752,16 +750,13 @@ String format( const char* fmt, ... )
752
750
va_list va;
753
751
va_start (va, fmt);
754
752
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);
760
754
va_end (va);
761
755
762
- if (len < 0 || len >= bsize)
756
+ CV_Assert (len >= 0 && " Check format string for errors" );
757
+ if (len >= bsize)
763
758
{
764
- buf.resize (std::max (bsize << 1 , len + 1 ) );
759
+ buf.resize (len + 1 );
765
760
continue ;
766
761
}
767
762
buf[bsize - 1 ] = 0 ;
@@ -856,19 +851,33 @@ bool setBreakOnError(bool value)
856
851
return prevVal;
857
852
}
858
853
859
- static bool cv_snprintf (char * buf, int len, const char * fmt, ...)
854
+ int cv_snprintf (char * buf, int len, const char * fmt, ...)
860
855
{
861
856
va_list va;
862
857
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);
865
859
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
+ }
868
879
#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);
872
881
#endif
873
882
}
874
883
0 commit comments