Merge pull request #22149 from seanm:sprintf

Replaced sprintf with safer snprintf

* Straightforward replacement of sprintf with safer snprintf

* Trickier replacement of sprintf with safer snprintf

Some functions were changed to take another parameter: the size of the buffer, so that they can pass that size on to snprintf.
This commit is contained in:
Sean McBride 2022-06-24 23:48:22 -04:00 committed by GitHub
parent a6ca48a1c2
commit 35f1a90df7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 126 additions and 131 deletions

View File

@ -1677,7 +1677,7 @@ void CvCascadeBoost::write( FileStorage &fs, const Mat& featureMap ) const
fs << CC_WEAK_CLASSIFIERS << "[";
for( int wi = 0; wi < weak->total; wi++)
{
/*sprintf( cmnt, "tree %i", wi );
/*snprintf( cmnt, sizeof(cmnt), "tree %i", wi );
cvWriteComment( fs, cmnt, 0 );*/
weakTree = *((CvCascadeBoostTree**) cvGetSeqElem( weak, wi ));
weakTree->write( fs, featureMap );

View File

@ -253,7 +253,7 @@ bool CvCascadeClassifier::train( const string _cascadeDirName,
}
// save current stage
char buf[32];
sprintf(buf, "%s%d", "stage", i );
snprintf(buf, sizeof(buf), "%s%d", "stage", i );
string stageFilename = dirName + buf + ".xml";
FileStorage fs( stageFilename, FileStorage::WRITE );
if ( !fs.isOpened() )
@ -369,7 +369,7 @@ void CvCascadeClassifier::writeStages( FileStorage &fs, const Mat& featureMap )
for( vector< Ptr<CvCascadeBoost> >::const_iterator it = stageClassifiers.begin();
it != stageClassifiers.end();++it, ++i )
{
sprintf( cmnt, "stage %d", i );
snprintf( cmnt, sizeof(cmnt), "stage %d", i );
cvWriteComment( fs.fs, cmnt, 0 );
fs << "{";
(*it)->write( fs, featureMap );
@ -460,7 +460,7 @@ void CvCascadeClassifier::save( const string filename, bool baseFormat )
for( size_t si = 0; si < stageClassifiers.size(); si++ )
{
fs << "{"; //stage
/*sprintf( buf, "stage %d", si );
/*snprintf( buf, sizeof(buf), "stage %d", si );
CV_CALL( cvWriteComment( fs, buf, 1 ) );*/
weak = stageClassifiers[si]->get_weak_predictors();
fs << ICV_HAAR_TREES_NAME << "[";
@ -471,7 +471,7 @@ void CvCascadeClassifier::save( const string filename, bool baseFormat )
CvCascadeBoostTree* tree = *((CvCascadeBoostTree**) cvGetSeqElem( weak, wi ));
fs << "[";
/*sprintf( buf, "tree %d", wi );
/*snprintf( buf, sizeof(buf), "tree %d", wi );
CV_CALL( cvWriteComment( fs, buf, 1 ) );*/
const CvDTreeNode* tempNode;
@ -535,10 +535,10 @@ bool CvCascadeClassifier::load( const string cascadeDirName )
featureEvaluator->init( featureParams, numPos + numNeg, cascadeParams.winSize );
fs.release();
char buf[16] = {0};
char buf[5+10+1] = {0};
for ( int si = 0; si < numStages; si++ )
{
sprintf( buf, "%s%d", "stage", si);
snprintf( buf, sizeof(buf), "%s%d", "stage", si);
fs.open( cascadeDirName + buf + ".xml", FileStorage::READ );
node = fs.getFirstTopLevelNode();
if ( !fs.isOpened() )

View File

@ -520,7 +520,7 @@ cvPreprocessCategoricalResponses( const CvMat* responses,
if( ri != rf )
{
char buf[100];
sprintf( buf, "response #%d is not integral", idx );
snprintf( buf, sizeof(buf), "response #%d is not integral", idx );
CV_ERROR( CV_StsBadArg, buf );
}
dst[i] = ri;

View File

@ -442,7 +442,7 @@ void CvDTreeTrainData::set_data( const CvMat* _train_data, int _tflag,
val = cvRound(t);
if( fabs(t - val) > FLT_EPSILON )
{
sprintf( err, "%d-th value of %d-th (categorical) "
snprintf( err, sizeof(err), "%d-th value of %d-th (categorical) "
"variable is not an integer", i, vi );
CV_ERROR( CV_StsBadArg, err );
}
@ -450,7 +450,7 @@ void CvDTreeTrainData::set_data( const CvMat* _train_data, int _tflag,
if( val == INT_MAX )
{
sprintf( err, "%d-th value of %d-th (categorical) "
snprintf( err, sizeof(err), "%d-th value of %d-th (categorical) "
"variable is too large", i, vi );
CV_ERROR( CV_StsBadArg, err );
}
@ -557,7 +557,7 @@ void CvDTreeTrainData::set_data( const CvMat* _train_data, int _tflag,
if( fabs(val) >= ord_nan )
{
sprintf( err, "%d-th value of %d-th (ordered) "
snprintf( err, sizeof(err), "%d-th value of %d-th (ordered) "
"variable (=%g) is too large", i, vi, val );
CV_ERROR( CV_StsBadArg, err );
}

View File

@ -92,7 +92,7 @@ static bool ocl_binary_op(InputArray _src1, InputArray _src2, OutputArray _dst,
const int dstType1 = CV_MAKETYPE(dstDepth, 1);
const int scalarType = CV_MAKETYPE(srcdepth, scalarcn);
sprintf(opts, "-D %s%s -D %s%s -D dstT=%s -D DEPTH_dst=%d -D dstT_C1=%s -D workST=%s -D cn=%d -D rowsPerWI=%d",
snprintf(opts, sizeof(opts), "-D %s%s -D %s%s -D dstT=%s -D DEPTH_dst=%d -D dstT_C1=%s -D workST=%s -D cn=%d -D rowsPerWI=%d",
haveMask ? "MASK_" : "", haveScalar ? "UNARY_OP" : "BINARY_OP", oclop2str[oclop],
doubleSupport ? " -D DOUBLE_SUPPORT" : "",
bitwise ? ocl::memopTypeToStr(dstType) : ocl::typeToStr(dstType),
@ -490,7 +490,7 @@ static bool ocl_arithm_op(InputArray _src1, InputArray _src2, OutputArray _dst,
int scalarcn = kercn == 3 ? 4 : kercn, rowsPerWI = d.isIntel() ? 4 : 1;
char cvtstr[4][32], opts[1024];
sprintf(opts, "-D %s%s -D %s -D srcT1=%s -D srcT1_C1=%s -D srcT2=%s -D srcT2_C1=%s "
snprintf(opts, sizeof(opts), "-D %s%s -D %s -D srcT1=%s -D srcT1_C1=%s -D srcT2=%s -D srcT2_C1=%s "
"-D dstT=%s -D DEPTH_dst=%d -D dstT_C1=%s -D workT=%s -D workST=%s -D scaleT=%s -D wdepth=%d -D convertToWT1=%s "
"-D convertToWT2=%s -D convertToDT=%s%s -D cn=%d -D rowsPerWI=%d -D convertFromU=%s",
(haveMask ? "MASK_" : ""), (haveScalar ? "UNARY_OP" : "BINARY_OP"),

View File

@ -70,14 +70,14 @@ namespace cv
char braces[5];
void (FormattedImpl::*valueToStr)();
void valueToStr8u() { sprintf(buf, "%3d", (int)mtx.ptr<uchar>(row, col)[cn]); }
void valueToStr8s() { sprintf(buf, "%3d", (int)mtx.ptr<schar>(row, col)[cn]); }
void valueToStr16u() { sprintf(buf, "%d", (int)mtx.ptr<ushort>(row, col)[cn]); }
void valueToStr16s() { sprintf(buf, "%d", (int)mtx.ptr<short>(row, col)[cn]); }
void valueToStr32s() { sprintf(buf, "%d", mtx.ptr<int>(row, col)[cn]); }
void valueToStr32f() { sprintf(buf, floatFormat, mtx.ptr<float>(row, col)[cn]); }
void valueToStr64f() { sprintf(buf, floatFormat, mtx.ptr<double>(row, col)[cn]); }
void valueToStr16f() { sprintf(buf, floatFormat, (float)mtx.ptr<float16_t>(row, col)[cn]); }
void valueToStr8u() { snprintf(buf, sizeof(buf), "%3d", (int)mtx.ptr<uchar>(row, col)[cn]); }
void valueToStr8s() { snprintf(buf, sizeof(buf), "%3d", (int)mtx.ptr<schar>(row, col)[cn]); }
void valueToStr16u() { snprintf(buf, sizeof(buf), "%d", (int)mtx.ptr<ushort>(row, col)[cn]); }
void valueToStr16s() { snprintf(buf, sizeof(buf), "%d", (int)mtx.ptr<short>(row, col)[cn]); }
void valueToStr32s() { snprintf(buf, sizeof(buf), "%d", mtx.ptr<int>(row, col)[cn]); }
void valueToStr32f() { snprintf(buf, sizeof(buf), floatFormat, mtx.ptr<float>(row, col)[cn]); }
void valueToStr64f() { snprintf(buf, sizeof(buf), floatFormat, mtx.ptr<double>(row, col)[cn]); }
void valueToStr16f() { snprintf(buf, sizeof(buf), floatFormat, (float)mtx.ptr<float16_t>(row, col)[cn]); }
void valueToStrOther() { buf[0] = 0; }
public:
@ -151,10 +151,10 @@ namespace cv
}
else
row = 0;
sprintf(buf, "\n(:, :, %d) = \n", cn+1);
snprintf(buf, sizeof(buf), "\n(:, :, %d) = \n", cn+1);
return buf;
}
sprintf(buf, "(:, :, %d) = \n", cn+1);
snprintf(buf, sizeof(buf), "(:, :, %d) = \n", cn+1);
return buf;
case STATE_EPILOGUE:
state = STATE_FINISHED;

View File

@ -56,7 +56,7 @@ char* itoa( int _val, char* buffer, int /*radix*/ )
return ptr;
}
char* doubleToString( char* buf, double value, bool explicitZero )
char* doubleToString( char* buf, size_t bufSize, double value, bool explicitZero )
{
Cv64suf val;
unsigned ieee754_hi;
@ -70,15 +70,15 @@ char* doubleToString( char* buf, double value, bool explicitZero )
if( ivalue == value )
{
if( explicitZero )
sprintf( buf, "%d.0", ivalue );
snprintf( buf, bufSize, "%d.0", ivalue );
else
sprintf( buf, "%d.", ivalue );
snprintf( buf, bufSize, "%d.", ivalue );
}
else
{
static const char* fmt = "%.16e";
char* ptr = buf;
sprintf( buf, fmt, value );
snprintf( buf, bufSize, fmt, value );
if( *ptr == '+' || *ptr == '-' )
ptr++;
for( ; cv_isdigit(*ptr); ptr++ )
@ -99,7 +99,7 @@ char* doubleToString( char* buf, double value, bool explicitZero )
return buf;
}
char* floatToString( char* buf, float value, bool halfprecision, bool explicitZero )
char* floatToString( char* buf, size_t bufSize, float value, bool halfprecision, bool explicitZero )
{
Cv32suf val;
unsigned ieee754;
@ -112,17 +112,17 @@ char* floatToString( char* buf, float value, bool halfprecision, bool explicitZe
if( ivalue == value )
{
if( explicitZero )
sprintf( buf, "%d.0", ivalue );
snprintf( buf, bufSize, "%d.0", ivalue );
else
sprintf( buf, "%d.", ivalue );
snprintf( buf, bufSize, "%d.", ivalue );
}
else
{
char* ptr = buf;
if (halfprecision)
sprintf(buf, "%.4e", value);
snprintf(buf, bufSize, "%.4e", value);
else
sprintf(buf, "%.8e", value);
snprintf(buf, bufSize, "%.8e", value);
if( *ptr == '+' || *ptr == '-' )
ptr++;
for( ; cv_isdigit(*ptr); ptr++ )
@ -585,7 +585,7 @@ bool FileStorage::Impl::open(const char *filename_or_buf, int _flags, const char
CV_Assert(strlen(encoding) < 1000);
char buf[1100];
sprintf(buf, "<?xml version=\"1.0\" encoding=\"%s\"?>\n", encoding);
snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"%s\"?>\n", encoding);
puts(buf);
} else
puts("<?xml version=\"1.0\"?>\n");
@ -1107,15 +1107,15 @@ void FileStorage::Impl::writeRawData(const std::string &dt, const void *_data, s
data += sizeof(int);
break;
case CV_32F:
ptr = fs::floatToString(buf, *(float *) data, false, explicitZero);
ptr = fs::floatToString(buf, sizeof(buf), *(float *) data, false, explicitZero);
data += sizeof(float);
break;
case CV_64F:
ptr = fs::doubleToString(buf, *(double *) data, explicitZero);
ptr = fs::doubleToString(buf, sizeof(buf), *(double *) data, explicitZero);
data += sizeof(double);
break;
case CV_16F: /* reference */
ptr = fs::floatToString(buf, (float) *(float16_t *) data, true, explicitZero);
ptr = fs::floatToString(buf, sizeof(buf), (float) *(float16_t *) data, true, explicitZero);
data += sizeof(float16_t);
break;
default:

View File

@ -86,8 +86,8 @@ namespace fs
{
int strcasecmp(const char* str1, const char* str2);
char* itoa( int _val, char* buffer, int /*radix*/ );
char* floatToString( char* buf, float value, bool halfprecision, bool explicitZero );
char* doubleToString( char* buf, double value, bool explicitZero );
char* floatToString( char* buf, size_t bufSize, float value, bool halfprecision, bool explicitZero );
char* doubleToString( char* buf, size_t bufSize, double value, bool explicitZero );
int calcStructSize( const char* dt, int initial_size );
int calcElemSize( const char* dt, int initial_size );

View File

@ -84,7 +84,7 @@ public:
void write( const char* key, double value )
{
char buf[128];
writeScalar( key, fs::doubleToString( buf, value, true ));
writeScalar( key, fs::doubleToString( buf, sizeof(buf), value, true ));
}
void write(const char* key, const char* str, bool quote)

View File

@ -148,7 +148,7 @@ public:
void write( const char* key, double value )
{
char buf[128];
writeScalar( key, fs::doubleToString( buf, value, false ) );
writeScalar( key, fs::doubleToString( buf, sizeof(buf), value, false ) );
}
void write(const char* key, const char* str, bool quote)

View File

@ -40,7 +40,7 @@ public:
{
/* reset struct flag. in order not to print ']' */
struct_flags = FileNode::SEQ;
sprintf(buf, "!!binary |");
snprintf(buf, sizeof(buf), "!!binary |");
data = buf;
}
else if( FileNode::isFlow(struct_flags))
@ -49,7 +49,7 @@ public:
struct_flags |= FileNode::FLOW;
if( type_name )
sprintf( buf, "!!%s %c", type_name, c );
snprintf( buf, sizeof(buf), "!!%s %c", type_name, c );
else
{
buf[0] = c;
@ -59,7 +59,7 @@ public:
}
else if( type_name )
{
sprintf( buf, "!!%s", type_name );
snprintf( buf, sizeof(buf), "!!%s", type_name );
data = buf;
}
@ -110,7 +110,7 @@ public:
void write( const char* key, double value )
{
char buf[128];
writeScalar( key, fs::doubleToString( buf, value, false ));
writeScalar( key, fs::doubleToString( buf, sizeof(buf), value, false ));
}
void write(const char* key, const char* str, bool quote)

View File

@ -1385,7 +1385,7 @@ CV_IMPL const char* cvErrorStr( int status )
case CV_OpenGlApiCallError : return "OpenGL API call";
};
sprintf(buf, "Unknown %s code %d", status >= 0 ? "status":"error", status);
snprintf(buf, sizeof(buf), "Unknown %s code %d", status >= 0 ? "status":"error", status);
return buf;
}

View File

@ -487,7 +487,7 @@ TEST(Core_InputOutput, FileStorage)
cv::FileStorage f(file, cv::FileStorage::WRITE);
char arr[66];
sprintf(arr, "sprintf is hell %d", 666);
snprintf(arr, sizeof(arr), "snprintf is hell %d", 666);
EXPECT_NO_THROW(f << arr);
}
@ -1765,8 +1765,8 @@ TEST(Core_InputOutput, FileStorage_JSON_VeryLongLines)
std::string val;
for(int i = 0; i < 52500; i += 100)
{
sprintf(key, "KEY%d", i);
sprintf(val0, "VALUE%d", i);
snprintf(key, sizeof(key), "KEY%d", i);
snprintf(val0, sizeof(val0), "VALUE%d", i);
fs[key] >> val;
ASSERT_EQ(val, val0);
}

View File

@ -161,7 +161,7 @@ int Core_ReduceTest::checkOp( const Mat& src, int dstType, int opType, const Mat
getMatTypeStr( dstType, dstTypeStr );
const char* dimStr = dim == 0 ? "ROWS" : "COLS";
sprintf( msg, "bad accuracy with srcType = %s, dstType = %s, opType = %s, dim = %s",
snprintf( msg, sizeof(msg), "bad accuracy with srcType = %s, dstType = %s, opType = %s, dim = %s",
srcTypeStr.c_str(), dstTypeStr.c_str(), opTypeStr, dimStr );
ts->printf( cvtest::TS::LOG, msg );
return cvtest::TS::FAIL_BAD_ACCURACY;

View File

@ -89,7 +89,7 @@ static bool ocl_boxFilter3x3_8UC1( InputArray _src, OutputArray _dst, int ddepth
globalsize[1] = size.height / 2;
char build_opts[1024];
sprintf(build_opts, "-D %s %s", borderMap[borderType], normalize ? "-D NORMALIZE" : "");
snprintf(build_opts, sizeof(build_opts), "-D %s %s", borderMap[borderType], normalize ? "-D NORMALIZE" : "");
ocl::Kernel kernel("boxFilter3x3_8UC1_cols16_rows2", cv::ocl::imgproc::boxFilter3x3_oclsrc, build_opts);
if (kernel.empty())
@ -196,7 +196,7 @@ static bool ocl_boxFilter( InputArray _src, OutputArray _dst, int ddepth,
globalsize[0] = roundUp(globalsize[0], wgRound);
char build_options[1024], cvt[2][40];
sprintf(build_options, "-D cn=%d "
snprintf(build_options, sizeof(build_options), "-D cn=%d "
"-D ANCHOR_X=%d -D ANCHOR_Y=%d -D KERNEL_SIZE_X=%d -D KERNEL_SIZE_Y=%d "
"-D PX_LOAD_VEC_SIZE=%d -D PX_LOAD_NUM_PX=%d "
"-D PX_PER_WI_X=%d -D PX_PER_WI_Y=%d -D PRIV_DATA_WIDTH=%d -D %s -D %s "

View File

@ -384,7 +384,7 @@ static bool ocl_sepFilter3x3_8UC1(InputArray _src, OutputArray _dst, int ddepth,
const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
char build_opts[1024];
sprintf(build_opts, "-D %s %s%s", borderMap[borderType],
snprintf(build_opts, sizeof(build_opts), "-D %s %s%s", borderMap[borderType],
ocl::kernelToStr(kernelX, CV_32F, "KERNEL_MATRIX_X").c_str(),
ocl::kernelToStr(kernelY, CV_32F, "KERNEL_MATRIX_Y").c_str());
@ -684,7 +684,7 @@ static bool ocl_Laplacian3_8UC1(InputArray _src, OutputArray _dst, int ddepth,
const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
char build_opts[1024];
sprintf(build_opts, "-D %s %s", borderMap[borderType],
snprintf(build_opts, sizeof(build_opts), "-D %s %s", borderMap[borderType],
ocl::kernelToStr(kernel, CV_32F, "KERNEL_MATRIX").c_str());
ocl::Kernel k("laplacian3_8UC1_cols16_rows2", cv::ocl::imgproc::laplacian3_oclsrc, build_opts);

View File

@ -637,7 +637,7 @@ static bool ocl_filter2D( InputArray _src, OutputArray _dst, int ddepth,
globalsize[0] = ROUNDUP(globalsize[0], wgRound);
char build_options[1024];
sprintf(build_options, "-D cn=%d "
snprintf(build_options, sizeof(build_options), "-D cn=%d "
"-D ANCHOR_X=%d -D ANCHOR_Y=%d -D KERNEL_SIZE_X=%d -D KERNEL_SIZE_Y=%d "
"-D PX_LOAD_VEC_SIZE=%d -D PX_LOAD_NUM_PX=%d "
"-D PX_PER_WI_X=%d -D PX_PER_WI_Y=%d -D PRIV_DATA_WIDTH=%d -D %s -D %s "

View File

@ -354,7 +354,7 @@ static bool ocl_GaussianBlur_8UC1(InputArray _src, OutputArray _dst, Size ksize,
const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
char build_opts[1024];
sprintf(build_opts, "-D %s %s%s", borderMap[borderType & ~BORDER_ISOLATED],
snprintf(build_opts, sizeof(build_opts), "-D %s %s%s", borderMap[borderType & ~BORDER_ISOLATED],
ocl::kernelToStr(kernelX, CV_32F, "KERNEL_MATRIX_X").c_str(),
ocl::kernelToStr(kernelY, CV_32F, "KERNEL_MATRIX_Y").c_str());

View File

@ -48,7 +48,7 @@ void __wrap_printf_func(const char* fmt, ...)
va_list args;
va_start(args, fmt);
char buffer[256];
vsprintf (buffer, fmt, args);
vsnprintf (buffer, sizeof(buffer), fmt, args);
cvtest::TS::ptr()->printf(cvtest::TS::SUMMARY, buffer);
va_end(args);
}

View File

@ -234,7 +234,7 @@ PyObject* pyopencv_from(const TYPE& src)
static PyObject* pyopencv_##CLASS_ID##_repr(PyObject* self) \
{ \
char str[1000]; \
sprintf(str, "< " MODULESTR SCOPE"."#EXPORT_NAME" %p>", self); \
snprintf(str, sizeof(str), "< " MODULESTR SCOPE"."#EXPORT_NAME" %p>", self); \
return PyString_FromString(str); \
}
@ -294,7 +294,7 @@ PyObject* pyopencv_from(const TYPE& src)
static PyObject* pyopencv_##CLASS_ID##_repr(PyObject* self) \
{ \
char str[1000]; \
sprintf(str, "< " MODULESTR SCOPE"."#EXPORT_NAME" %p>", self); \
snprintf(str, sizeof(str), "< " MODULESTR SCOPE"."#EXPORT_NAME" %p>", self); \
return PyString_FromString(str); \
} \
static PyType_Slot pyopencv_##CLASS_ID##_Slots[] = \

View File

@ -312,7 +312,7 @@ void BaseTest::safe_run( int start_from )
char buf[1 << 16];
const char* delim = exc.err.find('\n') == cv::String::npos ? "" : "\n";
sprintf( buf, "OpenCV Error:\n\t%s (%s%s) in %s, file %s, line %d",
snprintf( buf, sizeof(buf), "OpenCV Error:\n\t%s (%s%s) in %s, file %s, line %d",
errorStr, delim, exc.err.c_str(), exc.func.size() > 0 ?
exc.func.c_str() : "unknown function", exc.file.c_str(), exc.line );
ts->printf(TS::LOG, "%s\n", buf);
@ -603,7 +603,7 @@ void TS::set_gtest_status()
return SUCCEED();
char seedstr[32];
sprintf(seedstr, "%08x%08x", (unsigned)(current_test_info.rng_seed>>32),
snprintf(seedstr, sizeof(seedstr), "%08x%08x", (unsigned)(current_test_info.rng_seed>>32),
(unsigned)(current_test_info.rng_seed));
string logs = "";

View File

@ -29,7 +29,7 @@ string vec2str( const string& sep, const int* v, size_t nelems )
string result = "";
for( size_t i = 0; i < nelems; i++ )
{
sprintf(buf, "%d", v[i]);
snprintf(buf, sizeof(buf), "%d", v[i]);
result += string(buf);
if( i < nelems - 1 )
result += sep;
@ -2154,15 +2154,15 @@ int cmpEps2( TS* ts, const Mat& a, const Mat& b, double success_err_level,
switch( code )
{
case CMP_EPS_BIG_DIFF:
sprintf( msg, "%s: Too big difference (=%g > %g)", desc, diff, success_err_level );
snprintf( msg, sizeof(msg), "%s: Too big difference (=%g > %g)", desc, diff, success_err_level );
code = TS::FAIL_BAD_ACCURACY;
break;
case CMP_EPS_INVALID_TEST_DATA:
sprintf( msg, "%s: Invalid output", desc );
snprintf( msg, sizeof(msg), "%s: Invalid output", desc );
code = TS::FAIL_INVALID_OUTPUT;
break;
case CMP_EPS_INVALID_REF_DATA:
sprintf( msg, "%s: Invalid reference output", desc );
snprintf( msg, sizeof(msg), "%s: Invalid reference output", desc );
code = TS::FAIL_INVALID_OUTPUT;
break;
default:

View File

@ -78,7 +78,7 @@ void CV_OptFlowPyrLKTest::run( int )
for(;;)
{
sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_prev.dat" );
snprintf( filename, sizeof(filename), "%soptflow/%s", ts->get_data_path().c_str(), "lk_prev.dat" );
{
FileStorage fs(filename, FileStorage::READ);
@ -91,7 +91,7 @@ void CV_OptFlowPyrLKTest::run( int )
}
}
sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_next.dat" );
snprintf( filename, sizeof(filename), "%soptflow/%s", ts->get_data_path().c_str(), "lk_next.dat" );
{
FileStorage fs(filename, FileStorage::READ);
@ -115,7 +115,7 @@ void CV_OptFlowPyrLKTest::run( int )
}
/* read first image */
sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_1.bmp" );
snprintf( filename, sizeof(filename), "%soptflow/%s", ts->get_data_path().c_str(), "rock_1.bmp" );
imgI = cv::imread( filename, cv::IMREAD_UNCHANGED );
if( imgI.empty() )
@ -126,7 +126,7 @@ void CV_OptFlowPyrLKTest::run( int )
}
/* read second image */
sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_2.bmp" );
snprintf( filename, sizeof(filename), "%soptflow/%s", ts->get_data_path().c_str(), "rock_2.bmp" );
imgJ = cv::imread( filename, cv::IMREAD_UNCHANGED );
if( imgJ.empty() )

View File

@ -88,11 +88,6 @@ Thanks to:
*/
/////////////////////////////////////////////////////////
#if defined _MSC_VER && _MSC_VER >= 100
//'sprintf': name was marked as #pragma deprecated
#pragma warning(disable: 4995)
#endif
#if defined(__clang__) // clang or MSVC clang
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
#elif defined(__GNUC__) // MinGW
@ -2304,29 +2299,29 @@ void videoInput::processPixels(unsigned char * src, unsigned char * dst, int wid
void videoInput::getMediaSubtypeAsString(GUID type, char * typeAsString){
char tmpStr[8];
if( type == MEDIASUBTYPE_RGB24) sprintf(tmpStr, "RGB24");
else if(type == MEDIASUBTYPE_RGB32) sprintf(tmpStr, "RGB32");
else if(type == MEDIASUBTYPE_RGB555)sprintf(tmpStr, "RGB555");
else if(type == MEDIASUBTYPE_RGB565)sprintf(tmpStr, "RGB565");
else if(type == MEDIASUBTYPE_YUY2) sprintf(tmpStr, "YUY2");
else if(type == MEDIASUBTYPE_YVYU) sprintf(tmpStr, "YVYU");
else if(type == MEDIASUBTYPE_YUYV) sprintf(tmpStr, "YUYV");
else if(type == MEDIASUBTYPE_IYUV) sprintf(tmpStr, "IYUV");
else if(type == MEDIASUBTYPE_UYVY) sprintf(tmpStr, "UYVY");
else if(type == MEDIASUBTYPE_YV12) sprintf(tmpStr, "YV12");
else if(type == MEDIASUBTYPE_YVU9) sprintf(tmpStr, "YVU9");
else if(type == MEDIASUBTYPE_Y411) sprintf(tmpStr, "Y411");
else if(type == MEDIASUBTYPE_Y41P) sprintf(tmpStr, "Y41P");
else if(type == MEDIASUBTYPE_Y211) sprintf(tmpStr, "Y211");
else if(type == MEDIASUBTYPE_AYUV) sprintf(tmpStr, "AYUV");
else if(type == MEDIASUBTYPE_MJPG) sprintf(tmpStr, "MJPG");
else if(type == MEDIASUBTYPE_Y800) sprintf(tmpStr, "Y800");
else if(type == MEDIASUBTYPE_Y8) sprintf(tmpStr, "Y8");
else if(type == MEDIASUBTYPE_GREY) sprintf(tmpStr, "GREY");
else if(type == MEDIASUBTYPE_I420) sprintf(tmpStr, "I420");
else if (type == MEDIASUBTYPE_BY8) sprintf(tmpStr, "BY8");
else if (type == MEDIASUBTYPE_Y16) sprintf(tmpStr, "Y16");
else sprintf(tmpStr, "OTHER");
if( type == MEDIASUBTYPE_RGB24) snprintf(tmpStr, sizeof(tmpStr), "RGB24");
else if(type == MEDIASUBTYPE_RGB32) snprintf(tmpStr, sizeof(tmpStr), "RGB32");
else if(type == MEDIASUBTYPE_RGB555)snprintf(tmpStr, sizeof(tmpStr), "RGB555");
else if(type == MEDIASUBTYPE_RGB565)snprintf(tmpStr, sizeof(tmpStr), "RGB565");
else if(type == MEDIASUBTYPE_YUY2) snprintf(tmpStr, sizeof(tmpStr), "YUY2");
else if(type == MEDIASUBTYPE_YVYU) snprintf(tmpStr, sizeof(tmpStr), "YVYU");
else if(type == MEDIASUBTYPE_YUYV) snprintf(tmpStr, sizeof(tmpStr), "YUYV");
else if(type == MEDIASUBTYPE_IYUV) snprintf(tmpStr, sizeof(tmpStr), "IYUV");
else if(type == MEDIASUBTYPE_UYVY) snprintf(tmpStr, sizeof(tmpStr), "UYVY");
else if(type == MEDIASUBTYPE_YV12) snprintf(tmpStr, sizeof(tmpStr), "YV12");
else if(type == MEDIASUBTYPE_YVU9) snprintf(tmpStr, sizeof(tmpStr), "YVU9");
else if(type == MEDIASUBTYPE_Y411) snprintf(tmpStr, sizeof(tmpStr), "Y411");
else if(type == MEDIASUBTYPE_Y41P) snprintf(tmpStr, sizeof(tmpStr), "Y41P");
else if(type == MEDIASUBTYPE_Y211) snprintf(tmpStr, sizeof(tmpStr), "Y211");
else if(type == MEDIASUBTYPE_AYUV) snprintf(tmpStr, sizeof(tmpStr), "AYUV");
else if(type == MEDIASUBTYPE_MJPG) snprintf(tmpStr, sizeof(tmpStr), "MJPG");
else if(type == MEDIASUBTYPE_Y800) snprintf(tmpStr, sizeof(tmpStr), "Y800");
else if(type == MEDIASUBTYPE_Y8) snprintf(tmpStr, sizeof(tmpStr), "Y8");
else if(type == MEDIASUBTYPE_GREY) snprintf(tmpStr, sizeof(tmpStr), "GREY");
else if(type == MEDIASUBTYPE_I420) snprintf(tmpStr, sizeof(tmpStr), "I420");
else if (type == MEDIASUBTYPE_BY8) snprintf(tmpStr, sizeof(tmpStr), "BY8");
else if (type == MEDIASUBTYPE_Y16) snprintf(tmpStr, sizeof(tmpStr), "Y16");
else snprintf(tmpStr, sizeof(tmpStr), "OTHER");
memcpy(typeAsString, tmpStr, sizeof(char)*8);
}
@ -2352,15 +2347,15 @@ void videoInput::getVideoPropertyAsString(int prop, char * propertyAsString){
char tmpStr[16];
if ( prop==VideoProcAmp_Brightness) sprintf(tmpStr, "Brightness");
else if ( prop==VideoProcAmp_Contrast) sprintf(tmpStr, "Contrast");
else if ( prop==VideoProcAmp_Saturation) sprintf(tmpStr, "Saturation");
else if ( prop==VideoProcAmp_Hue) sprintf(tmpStr, "Hue");
else if ( prop==VideoProcAmp_Gain) sprintf(tmpStr, "Gain");
else if ( prop==VideoProcAmp_Gamma) sprintf(tmpStr, "Gamma");
else if ( prop==VideoProcAmp_ColorEnable) sprintf(tmpStr, "ColorEnable");
else if ( prop==VideoProcAmp_Sharpness) sprintf(tmpStr, "Sharpness");
else sprintf(tmpStr, "%u",prop);
if ( prop==VideoProcAmp_Brightness) snprintf(tmpStr, sizeof(tmpStr), "Brightness");
else if ( prop==VideoProcAmp_Contrast) snprintf(tmpStr, sizeof(tmpStr), "Contrast");
else if ( prop==VideoProcAmp_Saturation) snprintf(tmpStr, sizeof(tmpStr), "Saturation");
else if ( prop==VideoProcAmp_Hue) snprintf(tmpStr, sizeof(tmpStr), "Hue");
else if ( prop==VideoProcAmp_Gain) snprintf(tmpStr, sizeof(tmpStr), "Gain");
else if ( prop==VideoProcAmp_Gamma) snprintf(tmpStr, sizeof(tmpStr), "Gamma");
else if ( prop==VideoProcAmp_ColorEnable) snprintf(tmpStr, sizeof(tmpStr), "ColorEnable");
else if ( prop==VideoProcAmp_Sharpness) snprintf(tmpStr, sizeof(tmpStr), "Sharpness");
else snprintf(tmpStr, sizeof(tmpStr), "%u",prop);
memcpy(propertyAsString, tmpStr, sizeof(char)*16);
}
@ -2455,14 +2450,14 @@ void videoInput::getCameraPropertyAsString(int prop, char * propertyAsString){
char tmpStr[16];
if ( prop==CameraControl_Pan) sprintf(tmpStr, "Pan");
else if ( prop==CameraControl_Tilt) sprintf(tmpStr, "Tilt");
else if ( prop==CameraControl_Roll) sprintf(tmpStr, "Roll");
else if ( prop==CameraControl_Zoom) sprintf(tmpStr, "Zoom");
else if ( prop==CameraControl_Exposure) sprintf(tmpStr, "Exposure");
else if ( prop==CameraControl_Iris) sprintf(tmpStr, "Iris");
else if ( prop==CameraControl_Focus) sprintf(tmpStr, "Focus");
else sprintf(tmpStr, "%u",prop);
if ( prop==CameraControl_Pan) snprintf(tmpStr, sizeof(tmpStr), "Pan");
else if ( prop==CameraControl_Tilt) snprintf(tmpStr, sizeof(tmpStr), "Tilt");
else if ( prop==CameraControl_Roll) snprintf(tmpStr, sizeof(tmpStr), "Roll");
else if ( prop==CameraControl_Zoom) snprintf(tmpStr, sizeof(tmpStr), "Zoom");
else if ( prop==CameraControl_Exposure) snprintf(tmpStr, sizeof(tmpStr), "Exposure");
else if ( prop==CameraControl_Iris) snprintf(tmpStr, sizeof(tmpStr), "Iris");
else if ( prop==CameraControl_Focus) snprintf(tmpStr, sizeof(tmpStr), "Focus");
else snprintf(tmpStr, sizeof(tmpStr), "%u",prop);
memcpy(propertyAsString, tmpStr, sizeof(char)*16);
}

View File

@ -667,7 +667,7 @@ double DigitalCameraCapture::getProperty(int propertyId) const
catch (const GPhoto2Exception & e)
{
char buf[128] = "";
sprintf(buf, "cannot get property: %d", propertyId);
snprintf(buf, sizeof(buf), "cannot get property: %d", propertyId);
message(WARNING, (const char *) buf, e);
return 0;
}
@ -810,7 +810,7 @@ bool DigitalCameraCapture::setProperty(int propertyId, double value)
catch (const GPhoto2Exception & e)
{
char buf[128] = "";
sprintf(buf, "cannot set property: %d to %f", propertyId, value);
snprintf(buf, sizeof(buf), "cannot set property: %d to %f", propertyId, value);
message(WARNING, (const char *) buf, e);
return false;
}

View File

@ -1784,7 +1784,7 @@ void CvCaptureCAM_XIMEA::errMsg(const char* msg, int errNum) const
#if defined _WIN32
char buf[512]="";
sprintf( buf, "%s : %d, %s\n", msg, errNum, error_message.c_str());
snprintf( buf, sizeof(buf), "%s : %d, %s\n", msg, errNum, error_message.c_str());
OutputDebugString(buf);
#else
fprintf(stderr, "%s : %d, %s\n", msg, errNum, error_message.c_str());

View File

@ -111,7 +111,7 @@ class XINECapture : public IVideoCapture
char configfile[2048] = {0};
xine = xine_new();
sprintf(configfile, "%s%s", xine_get_homedir(), "/.xine/config");
snprintf(configfile, sizeof(configfile), "%s%s", xine_get_homedir(), "/.xine/config");
xine_config_load(xine, configfile);
xine_init(xine);
xine_engine_set_param(xine, 0, 0);

View File

@ -225,7 +225,7 @@ static void saveCameraParams( const string& filename,
if( flags != 0 )
{
sprintf( buf, "flags: %s%s%s%s",
snprintf( buf, sizeof(buf), "flags: %s%s%s%s",
flags & CALIB_USE_INTRINSIC_GUESS ? "+use_intrinsic_guess" : "",
flags & CALIB_FIX_ASPECT_RATIO ? "+fix_aspectRatio" : "",
flags & CALIB_FIX_PRINCIPAL_POINT ? "+fix_principal_point" : "",

View File

@ -469,7 +469,7 @@ int main(int argc, char** argv)
outbarename = strrchr(outprefix.c_str(), '/');
const char* tmp = strrchr(outprefix.c_str(), '\\');
char cmd[1000];
sprintf(cmd, "mkdir %s", outprefix.c_str());
snprintf(cmd, sizeof(cmd), "mkdir %s", outprefix.c_str());
if( tmp && tmp > outbarename )
outbarename = tmp;
if( outbarename )
@ -568,7 +568,7 @@ int main(int argc, char** argv)
char path[1000];
for(;frameIdx < maxFrameIdx;frameIdx++)
{
sprintf(path, "%s%04d.jpg", outprefix.c_str(), frameIdx);
snprintf(path, sizeof(path), "%s%04d.jpg", outprefix.c_str(), frameIdx);
FILE* f = fopen(path, "rb");
if( !f )
break;

View File

@ -60,7 +60,7 @@ int main( void )
//![create_trackbar]
char TrackbarName[50];
sprintf( TrackbarName, "Alpha x %d", alpha_slider_max );
snprintf( TrackbarName, sizeof(TrackbarName), "Alpha x %d", alpha_slider_max );
createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar );
//![create_trackbar]

View File

@ -58,7 +58,7 @@ int main( int argc, char** argv )
/// Create Trackbars for Thresholds
char thresh_label[50];
sprintf( thresh_label, "Thres: %d + input", min_threshold );
snprintf( thresh_label, sizeof(thresh_label), "Thres: %d + input", min_threshold );
namedWindow( standard_name, WINDOW_AUTOSIZE );
createTrackbar( thresh_label, standard_name, &s_trackbar, max_trackbar, Standard_Hough);

View File

@ -57,7 +57,7 @@ namespace {
case 27: //escape key
return 0;
case ' ': //Save an image
sprintf(filename,"filename%.3d.jpg",n++);
snprintf(filename,sizeof(filename),"filename%.3d.jpg",n++);
imwrite(filename,frame);
cout << "Saved " << filename << endl;
break;

View File

@ -258,22 +258,22 @@ public:
int y = 0;
buf[0] = 0;
sprintf(buf, "mode: %s", m_modeStr[mode].c_str());
snprintf(buf, sizeof(buf), "mode: %s", m_modeStr[mode].c_str());
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight;
buf[0] = 0;
sprintf(buf, m_demo_processing ? "blur frame" : "copy frame");
snprintf(buf, sizeof(buf), m_demo_processing ? "blur frame" : "copy frame");
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight;
buf[0] = 0;
sprintf(buf, "time: %4.1f msec", time);
snprintf(buf, sizeof(buf), "time: %4.1f msec", time);
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight;
buf[0] = 0;
sprintf(buf, "OpenCL device: %s", oclDevName.c_str());
snprintf(buf, sizeof(buf), "OpenCL device: %s", oclDevName.c_str());
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
::SelectObject(hDC, hOldFont);

View File

@ -259,22 +259,22 @@ public:
int y = 0;
buf[0] = 0;
sprintf(buf, "mode: %s", m_modeStr[mode].c_str());
snprintf(buf, sizeof(buf), "mode: %s", m_modeStr[mode].c_str());
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight;
buf[0] = 0;
sprintf(buf, m_demo_processing ? "blur frame" : "copy frame");
snprintf(buf, sizeof(buf), m_demo_processing ? "blur frame" : "copy frame");
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight;
buf[0] = 0;
sprintf(buf, "time: %4.1f msec", time);
snprintf(buf, sizeof(buf), "time: %4.1f msec", time);
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight;
buf[0] = 0;
sprintf(buf, "OpenCL device: %s", oclDevName.c_str());
snprintf(buf, sizeof(buf), "OpenCL device: %s", oclDevName.c_str());
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
::SelectObject(hDC, hOldFont);