mirror of
https://github.com/opencv/opencv.git
synced 2026-07-31 00:03:03 +04:00
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:
@@ -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"),
|
||||
|
||||
+10
-10
@@ -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;
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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 );
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user