[Insight-developers] formatstring vulnerability in NumericSeriesFileNames::GetFileNames

Kent Williams kent at psychiatry.uiowa.edu
Wed Jun 28 11:07:58 EDT 2006


If someone wants to use Boost alongside ITK, I assume that they will do 
what they need to in order to install both.  I don't think anyone wants 
ITK to depend on Boost.  ITK already carries around a lot of dependency 
overhead in the Insight/Utilities directory.

The example of using boost from CMake depends on Boost being seperately 
built and installed.

But perhaps this code is more expedient, and is as safe as I could think 
of to make it. It depends on kwsys::SystemTools::EstimateFormatLength to 
make sure a large enough buffer is allocated, but then also uses 
vsnprintf to avoid overriding that buffer if EstimateFormatLength isn't 
perfect:

#include <stdio.h>
#include <string>
#include <stdarg.h>
#include <itksys/SystemTools.hxx>
#include <iostream>

int sprintf(std::string &buffer,const char *format,...)
{
  va_list ap;
  va_start(ap,format);
  size_t bufsize =
    itksys::SystemTools::EstimateFormatLength(format,
                                              ap);
  va_end(ap);
  char *buf = new char[bufsize];
  va_list ap2;
  va_start(ap2,format);
  int result = vsnprintf(buf,bufsize,format,ap2);
  buffer = buf;
  delete [] buf;
  return result;
}

int main(int argc, char **argv)
{
  std::string s;
  int x = 32;
  std::string test("Testing");
  double f = 3.1415926;
  sprintf(s,"%d %08d %s %16.16g\n",
          x,x,test.c_str(),f);
  std::cout << s;
         
}



More information about the Insight-developers mailing list