Skip to content

Commit ed3dc51

Browse files
authored
Excise the last instances of unsafe sprintf (#3705)
1 parent 494236c commit ed3dc51

File tree

5 files changed

+19
-22
lines changed

5 files changed

+19
-22
lines changed

src/cineon.imageio/cineoninput.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ CineonInput::open(const std::string& name, ImageSpec& newspec)
338338
}
339339
{
340340
char filmedge[17];
341-
m_cin.header.FilmEdgeCode(filmedge);
341+
m_cin.header.FilmEdgeCode(filmedge, sizeof(filmedge));
342342
if (filmedge[0])
343343
m_spec.attribute("cineon:FilmEdgeCode", filmedge);
344344
}

src/cineon.imageio/libcineon/CineonHeader.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -500,21 +500,18 @@ int cineon::GenericHeader::DataSizeByteCount(const DataSize ds)
500500
}
501501

502502

503-
void cineon::IndustryHeader::FilmEdgeCode(char *edge) const
503+
void cineon::IndustryHeader::FilmEdgeCode(char *edge, size_t size) const
504504
{
505-
if (this->filmManufacturingIdCode == 0xff
506-
&& this->filmType == 0xff
507-
&& this->perfsOffset == 0xff
508-
&& this->prefix == 0xffffffff
509-
&& this->count == 0xffffffff)
510-
*edge = 0;
511-
else
512-
sprintf(edge, "%02u%02u%02u%06u%04u",
513-
(unsigned int)this->filmManufacturingIdCode,
514-
(unsigned int)this->filmType,
515-
(unsigned int)this->perfsOffset,
516-
this->prefix,
517-
this->count);
505+
if (this->filmManufacturingIdCode == 0xff && this->filmType == 0xff
506+
&& this->perfsOffset == 0xff && this->prefix == 0xffffffff
507+
&& this->count == 0xffffffff)
508+
*edge = 0;
509+
else
510+
snprintf(edge, size, "%02u%02u%02u%06u%04u",
511+
(unsigned int)this->filmManufacturingIdCode,
512+
(unsigned int)this->filmType,
513+
(unsigned int)this->perfsOffset, this->prefix,
514+
this->count);
518515
}
519516

520517

src/cineon.imageio/libcineon/CineonHeader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ namespace cineon
955955
* \brief Get the film edge code information that is machine readable
956956
* \param edge buffer to write film edge code information (16+1 chars)
957957
*/
958-
void FilmEdgeCode(char *edge) const;
958+
void FilmEdgeCode(char *edge, size_t size) const;
959959

960960
/*!
961961
* \brief Set the film edge code information that is machine readable

src/dpx.imageio/libdpx/DPXHeader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ void dpx::IndustryHeader::SetFileEdgeCode(const char *edge)
688688
void dpx::IndustryHeader::TimeCode(char *str) const
689689
{
690690
U32 tc = this->timeCode;
691-
::sprintf(str, "%c%c:%c%c:%c%c:%c%c",
691+
::snprintf(str, 12, "%c%c:%c%c:%c%c:%c%c",
692692
Hex((tc & 0xf0000000) >> 28), Hex((tc & 0xf000000) >> 24),
693693
Hex((tc & 0xf00000) >> 20), Hex((tc & 0xf0000) >> 16),
694694
Hex((tc & 0xf000) >> 12), Hex((tc & 0xf00) >> 8),
@@ -699,7 +699,7 @@ void dpx::IndustryHeader::TimeCode(char *str) const
699699
void dpx::IndustryHeader::UserBits(char *str) const
700700
{
701701
U32 ub = this->userBits;
702-
::sprintf(str, "%c%c:%c%c:%c%c:%c%c",
702+
::snprintf(str, 12, "%c%c:%c%c:%c%c:%c%c",
703703
Hex((ub & 0xf0000000) >> 28), Hex((ub & 0xf000000) >> 24),
704704
Hex((ub & 0xf00000) >> 20), Hex((ub & 0xf0000) >> 16),
705705
Hex((ub & 0xf000) >> 12), Hex((ub & 0xf00) >> 8),

src/libutil/strutil_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,18 @@ test_format()
7777
bench.indent (2);
7878
bench.units (Benchmarker::Unit::ns);
7979
char buffer[256];
80-
bench ("std::sprintf(\"%g\")", [&](){ DoNotOptimize (std::sprintf(buffer,"%g",123.45f)); });
80+
bench ("std::snprintf(\"%g\")", [&](){ DoNotOptimize (std::snprintf(buffer,sizeof(buffer),"%g",123.45f)); });
8181
bench ("Strutil::sprintf(\"%g\")", [&](){ DoNotOptimize (Strutil::sprintf("%g",123.45f)); });
8282
bench ("Strutil::fmt::format(\"{:g}\")", [&](){ DoNotOptimize (Strutil::fmt::format("{:g}",123.45f)); });
8383
bench ("Strutil::to_string(float)", [&](){ DoNotOptimize (Strutil::to_string(123.45f)); });
8484

85-
bench ("std::sprintf(\"%d\")", [&](){ DoNotOptimize (std::sprintf(buffer,"%d",123)); });
85+
bench ("std::snprintf(\"%d\")", [&](){ DoNotOptimize (std::snprintf(buffer,sizeof(buffer),"%d",123)); });
8686
bench ("Strutil::sprintf(\"%d\")", [&](){ DoNotOptimize (Strutil::sprintf("%g",123.0f)); });
8787
bench ("Strutil::fmt::format(\"{}\")", [&](){ DoNotOptimize (Strutil::fmt::format("{}",123)); });
8888
bench ("Strutil::to_string(int)", [&](){ DoNotOptimize (Strutil::to_string(123)); });
8989

90-
bench ("std::sprintf(\"%g %d %s %d %s %g\")", [&](){
91-
DoNotOptimize (std::sprintf(buffer,"%g %d %s %d %s %g", 123.45f, 1234, "foobar", 42, "kablooey", 3.14159f));
90+
bench ("std::snprintf(\"%g %d %s %d %s %g\")", [&](){
91+
DoNotOptimize (std::snprintf(buffer,sizeof(buffer),"%g %d %s %d %s %g", 123.45f, 1234, "foobar", 42, "kablooey", 3.14159f));
9292
});
9393
bench ("Strutil::sprintf(\"%g %d %s %d %s %g\")", [&](){
9494
DoNotOptimize (Strutil::sprintf("%g %d %s %d %s %g", 123.45f, 1234, "foobar", 42, "kablooey", 3.14159f));

0 commit comments

Comments
 (0)