Skip to content

Commit

Permalink
Printf format sanitising.
Browse files Browse the repository at this point in the history
Refactored version for next, use a new helper in
simgear::strutils.
  • Loading branch information
zakalawe committed Sep 15, 2013
1 parent 0e2ddb2 commit a18792c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
13 changes: 13 additions & 0 deletions simgear/misc/strutils.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include "strutils.hxx"

#include <simgear/debug/logstream.hxx>

using std::string;
using std::vector;
using std::stringstream;
Expand Down Expand Up @@ -484,6 +486,17 @@ std::string unescape(const char* s)
return r;
}

string sanitizePrintfFormat(const string& input)
{
string::size_type i = input.find("%n");
if (i != string::npos) {
SG_LOG(SG_IO, SG_WARN, "sanitizePrintfFormat: bad format string:" << input);
return string();
}

return input;
}

} // end namespace strutils

} // end namespace simgear
9 changes: 8 additions & 1 deletion simgear/misc/strutils.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ namespace simgear {

/**
* Like strcmp(), but for dotted versions strings NN.NN.NN
* any number of terms are support.
* any number of terms are supported.
* @return 0 if versions match, -ve number if v1 is lower, +ve if v1
* is greater
*/
Expand Down Expand Up @@ -180,6 +180,13 @@ namespace simgear {

inline std::string unescape(const std::string& str)
{ return unescape(str.c_str()); }

/**
* Check a printf-style format string for dangerous (buffer-overflowing,
* memory re-writing) format tokens. If a problematic token is
* found, logs an error (SG_WARN) and returns an empty format string.
*/
std::string sanitizePrintfFormat(const std::string& input);

} // end namespace strutils
} // end namespace simgear
Expand Down
3 changes: 2 additions & 1 deletion simgear/scene/model/SGText.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <simgear/math/SGMath.hxx>
#include <simgear/misc/sg_path.hxx>
#include <simgear/misc/strutils.hxx>

#include <osg/Geode>
#include <osg/MatrixTransform>
Expand All @@ -43,7 +44,7 @@ class SGText::UpdateCallback : public osg::NodeCallback {
offset( aOffset ),
truncate( aTruncate ),
numeric( aNumeric ),
format( aFormat )
format( simgear::strutils::sanitizePrintfFormat( aFormat ) )
{
if( format.empty() ) {
if( numeric ) format = "%f";
Expand Down

0 comments on commit a18792c

Please sign in to comment.