Fix crashes reported by Bennet Helm and Kornel Benko

* src/support/unicode.[Ch]
	(IconvProcessor): Rework the implementation to fix two bugs:
	- iconv_close was called even if the conversion descriptor was
	  invalid
	- The compiler generated copy constructor did simply copy the pimpl_
	  pointer. Therefore pimpl_ was deleted twice for all IconvProcessor
	  instances created by eightbit_to_ucs4() and ucs4_to_eightbit().
	  This is solved by using a scoped_ptr for pimpl_.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15937 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Georg Baum 2006-11-15 21:40:46 +00:00
parent 51a3125356
commit 2981181ec3
2 changed files with 35 additions and 7 deletions

View File

@ -39,6 +39,15 @@ static const iconv_t invalid_cd = (iconv_t)(-1);
struct IconvProcessor::Private { struct IconvProcessor::Private {
Private(): cd(invalid_cd) {} Private(): cd(invalid_cd) {}
~Private()
{
if (cd != invalid_cd) {
if (iconv_close(cd) == -1) {
lyxerr << "Error returned from iconv_close("
<< errno << ")" << endl;
}
}
}
iconv_t cd; iconv_t cd;
}; };
@ -50,16 +59,27 @@ IconvProcessor::IconvProcessor(char const * tocode,
} }
IconvProcessor::~IconvProcessor() IconvProcessor::IconvProcessor(IconvProcessor const & other)
: tocode_(other.tocode_), fromcode_(other.fromcode_),
pimpl_(new IconvProcessor::Private)
{ {
if (iconv_close(pimpl_->cd) == -1) {
lyxerr << "Error returned from iconv_close("
<< errno << ")" << endl;
}
delete pimpl_;
} }
IconvProcessor & IconvProcessor::operator=(IconvProcessor const & other)
{
if (&other == this)
return *this;
tocode_ = other.tocode_;
fromcode_ = other.fromcode_;
pimpl_.reset(new Private);
return *this;
}
IconvProcessor::~IconvProcessor() {}
bool IconvProcessor::init() bool IconvProcessor::init()
{ {
if (pimpl_->cd != invalid_cd) if (pimpl_->cd != invalid_cd)

View File

@ -15,6 +15,8 @@
#include "support/types.h" #include "support/types.h"
#include <boost/scoped_ptr.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
@ -27,6 +29,12 @@ public:
IconvProcessor( IconvProcessor(
char const * tocode = "", char const * tocode = "",
char const * fromcode = ""); char const * fromcode = "");
/// copy constructor needed because of pimpl_
IconvProcessor(IconvProcessor const &);
/// assignment operator needed because of pimpl_
IconvProcessor & operator=(IconvProcessor const &);
/// destructor (needs to be implemented in the .C file because the
/// boost::scoped_ptr destructor needs a fully defined type
~IconvProcessor(); ~IconvProcessor();
/// convert any data from \c fromcode to \c tocode unicode format. /// convert any data from \c fromcode to \c tocode unicode format.
@ -45,7 +53,7 @@ private:
std::string fromcode_; std::string fromcode_;
struct Private; struct Private;
Private * pimpl_; boost::scoped_ptr<Private> pimpl_;
}; };
// utf8_to_ucs4 // utf8_to_ucs4