Skip to content
Merged
4 changes: 3 additions & 1 deletion include/boutexception.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ void BoutParallelThrowRhsFail(int &status, const char* message);
class BoutException : public exception {
public:
BoutException(const char *, ...);
BoutException(const std::string);
virtual ~BoutException() throw();

const char* what() const throw();
void Backtrace();
protected:
char * buffer;
static const int BUFFER_LEN = 1024; // Length of char buffer for printing

int buflen; // Length of char buffer for printing
string message;
};

Expand Down
6 changes: 4 additions & 2 deletions include/datafile.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Datafile {
public:
Datafile(Options *opt = NULL);
Datafile(const Datafile &other);
// ~Datafile(); Default destructor is adequate
~Datafile(); // need to delete filename

Datafile& operator=(const Datafile &rhs);

Expand Down Expand Up @@ -86,7 +86,9 @@ class Datafile {
bool shiftOutput; //Do we want to write out in shifted space?

DataFormat *file;
char filename[512];
int filenamelen;
static const int FILENAMELEN=512;
char *filename;
bool appending;

/// A structure to hold a pointer to a class, and associated name and flags
Expand Down
14 changes: 9 additions & 5 deletions include/output.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,21 @@ class Output : private multioutbuf_init<char, std::char_traits<char> >,
public:
Output() : multioutbuf_init(),
std::basic_ostream<char, _Tr>(multioutbuf_init::buf()) {

buffer_len=BUFFER_LEN;
buffer=new char[buffer_len];
enable();
}

/// Specify a log file to open
Output(const char *fname) : multioutbuf_init(),
std::basic_ostream<char, _Tr>(multioutbuf_init::buf()) {

buffer_len=BUFFER_LEN;
buffer=new char[buffer_len];
enable();
open(fname);
}
~Output() {close();}
~Output() {close();
delete[] buffer;}

void enable(); ///< Enables writing to stdout (default)
void disable(); ///< Disables stdout
Expand All @@ -96,8 +99,9 @@ class Output : private multioutbuf_init<char, std::char_traits<char> >,
static Output *instance; ///< Default instance of this class

std::ofstream file; ///< Log file stream
static const int BUFFER_LEN=1024;
char buffer[BUFFER_LEN]; ///< Buffer used for C style output
static const int BUFFER_LEN=1024; ///< default length
int buffer_len; ///< the current length
char * buffer; ///< Buffer used for C style output
bool enabled; ///< Whether output to stdout is enabled
};

Expand Down
24 changes: 24 additions & 0 deletions include/utils.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,28 @@ string trim(const string &, const string &c=" \t\r");
string trimLeft(const string &, const string &c=" \t");
string trimRight(const string &, const string &c=" \t\r");
string trimComments(const string &, const string &c="#;");

/// the bout_vsnprintf macro:
/// The first argument is an char * buffer of length len.
/// It needs to have been allocated with new[], as it may be
/// reallocated.
/// len: the length of said buffer. May be changed, mussn't be const.
/// fmt: the const char * descriping the format.
/// note that fmt should be the first argument of the function of type
/// const char * and has to be directly followed by the variable arguments.
#define bout_vsnprintf(buf,len,fmt) { \
va_list va; \
va_start(va, fmt); \
int _vsnprintflen=vsnprintf(buf,len,fmt,va); \
va_end(va); \
if ( _vsnprintflen+1 > len){ \
_vsnprintflen+=1; \
delete[] buf; \
buf = new char[_vsnprintflen]; \
len = _vsnprintflen; \
va_start(va,fmt); \
vsnprintf(buf,len,fmt,va); \
va_end(va); \
} \
}
#endif // __UTILS_H__
15 changes: 8 additions & 7 deletions src/field/field.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <output.hxx>
#include <msg_stack.hxx>
#include <boutexception.hxx>
#include <utils.hxx>

Field::Field() {
#ifdef CHECK
Expand All @@ -40,27 +41,27 @@ Field::Field() {

/////////////////// PROTECTED ////////////////////

char err_buffer[512];

// Report an error occurring
void Field::error(const char *s, ...) const {
va_list ap; // List of arguments
int buf_len=512;
char * err_buffer=new char[buf_len];

if(s == (const char*) NULL) {
output.write("Unspecified error in field\n");
}else {

va_start(ap, s);
vsprintf(err_buffer, s, ap);
va_end(ap);
bout_vsnprintf(err_buffer,buf_len, s);

#ifdef TRACK
output.write("Error in '%s': %s", name.c_str(), err_buffer);
#else
output.write("Error in field: %s", err_buffer);
#endif
}

throw BoutException("Error in field: %s", err_buffer);
std::string msg="Error in field: ";
msg+=err_buffer;
delete[] err_buffer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will never be hit, and presumably leads to a memory leak if the exception isn't caught?
I'm not really sure a) if this is a problem and b) if so, how to fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leads to a memory leak, no matter if caught or not. (not caught however doesn't really matter)
b) use std::string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added BoutException(std::string) in order to avoid this issue

throw BoutException(msg);
}

54 changes: 25 additions & 29 deletions src/fileio/datafile.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@
#include <boutexception.hxx>
#include <output.hxx>
#include <boutcomm.hxx>

#include <utils.hxx>
#include "formatfactory.hxx"

Datafile::Datafile(Options *opt) : parallel(false), flush(true), guards(true), floats(false), openclose(true), enabled(true), shiftOutput(false), file(NULL) {
filenamelen=FILENAMELEN;
filename=new char[filenamelen];
if(opt == NULL)
return; // To allow static initialisation

// Read options

OPTION(opt, parallel, false); // By default no parallel formats for now
Expand All @@ -61,12 +62,13 @@ Datafile::Datafile(Options *opt) : parallel(false), flush(true), guards(true), f
}

Datafile::Datafile(const Datafile &other) :
parallel(other.parallel), flush(other.flush), guards(other.guards),
floats(other.floats), openclose(other.openclose), Lx(other.Lx), Ly(other.Ly), Lz(other.Lz),
enabled(other.enabled), shiftOutput(other.shiftOutput), file(NULL), int_arr(other.int_arr),
init_missing(other.init_missing), BoutReal_arr(other.BoutReal_arr), f2d_arr(other.f2d_arr),
parallel(other.parallel), flush(other.flush), guards(other.guards),
floats(other.floats), openclose(other.openclose), Lx(other.Lx), Ly(other.Ly), Lz(other.Lz),
enabled(other.enabled), shiftOutput(other.shiftOutput), file(NULL), int_arr(other.int_arr),
BoutReal_arr(other.BoutReal_arr), f2d_arr(other.f2d_arr),
f3d_arr(other.f3d_arr), v2d_arr(other.v2d_arr), v3d_arr(other.v3d_arr) {

filenamelen=FILENAMELEN;
filename=new char[filenamelen];
// Same added variables, but the file not the same
}

Expand All @@ -86,20 +88,20 @@ Datafile& Datafile::operator=(const Datafile &rhs) {
f3d_arr = rhs.f3d_arr;
v2d_arr = rhs.v2d_arr;
v3d_arr = rhs.v3d_arr;
filenamelen=FILENAMELEN;
filename = new char[filenamelen];
return *this;
}

// Datafile::~Datafile() {
// }
Datafile::~Datafile() {
delete[] filename;
}

bool Datafile::openr(const char *format, ...) {
va_list ap; // List of arguments
if(format == (const char*) NULL)
if(format == (const char*) NULL)
throw BoutException("Datafile::open: No argument given for opening file!");

va_start(ap, format);
vsprintf(filename, format, ap);
va_end(ap);
bout_vsnprintf(filename,filenamelen, format);

// Get the data format
file = FormatFactory::getInstance()->createDataFormat(filename, parallel);
Expand Down Expand Up @@ -130,13 +132,10 @@ bool Datafile::openw(const char *format, ...) {
if(!enabled)
return true;

va_list ap; // List of arguments
if(format == (const char*) NULL)
throw BoutException("Datafile::open: No argument given for opening file!");

va_start(ap, format);
vsprintf(filename, format, ap);
va_end(ap);
bout_vsnprintf(filename, filenamelen, format);

// Get the data format
file = FormatFactory::getInstance()->createDataFormat(filename, parallel);
Expand Down Expand Up @@ -174,14 +173,11 @@ bool Datafile::opena(const char *format, ...) {
if(!enabled)
return true;

va_list ap; // List of arguments
if(format == (const char*) NULL)
throw BoutException("Datafile::open: No argument given for opening file!");

va_start(ap, format);
vsprintf(filename, format, ap);
va_end(ap);

bout_vsnprintf(filename, filenamelen, format);

// Get the data format
file = FormatFactory::getInstance()->createDataFormat(filename, parallel);

Expand Down Expand Up @@ -528,14 +524,14 @@ bool Datafile::write() {
bool Datafile::write(const char *format, ...) const {
if(!enabled)
return true;

va_list ap; // List of arguments

if(format == (const char*) NULL)
throw BoutException("Datafile::write: No argument given!");
char filename[512];
va_start(ap, format);
vsprintf(filename, format, ap);
va_end(ap);

int filenamelen=512;
char * filename=new char[filenamelen];

bout_vsnprintf(filename, filenamelen, format);

// Create a new datafile
Datafile tmp(*this);
Expand Down
23 changes: 15 additions & 8 deletions src/sys/boutexception.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifdef BACKTRACE
#include <execinfo.h>
#endif
#include <utils.hxx>

void BoutParallelThrowRhsFail(int &status, const char* message) {
int allstatus;
Expand All @@ -17,6 +18,7 @@ void BoutParallelThrowRhsFail(int &status, const char* message) {
}

BoutException::~BoutException() throw() {
delete[] buffer;
}

void BoutException::Backtrace() {
Expand Down Expand Up @@ -70,16 +72,15 @@ void BoutException::Backtrace() {
}

#define INIT_EXCEPTION(s) { \
va_list ap; \
\
buflen=0; \
buffer=NULL; \
if(s == (const char*) NULL) { \
message="No error message given!\n"; \
} else { \
char buffer[1024]; \
va_start(ap, s); \
vsprintf(buffer, s, ap); \
va_end(ap); \
for (int i=0;i<1024;++i){ \
buflen=BoutException::BUFFER_LEN; \
buffer = new char[buflen]; \
bout_vsnprintf(buffer, buflen, s); \
for (int i=0;i<buflen;++i){ \
if (buffer[i]==0){ \
if (i>0 && buffer[i-1]=='\n'){ \
buffer[i-1]=0; \
Expand All @@ -97,9 +98,15 @@ void BoutException::Backtrace() {

BoutException::BoutException(const char* s, ...)
{

INIT_EXCEPTION(s);
}
BoutException::BoutException(const std::string msg)
{
message="====== Exception thrown ======\n"+msg+"\n";

this->Backtrace();
}

const char* BoutException::what() const throw()
{
Expand Down
12 changes: 5 additions & 7 deletions src/sys/expressionparser.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -406,17 +406,15 @@ int ExpressionParser::LexInfo::getPos() {


ParseException::ParseException(const char *s, ...) {
va_list ap; // List of arguments

if(s == (const char*) NULL)
return;

char buffer[1024];
va_start(ap, s);
vsprintf(buffer, s, ap);
va_end(ap);

int buf_len=1024;
char * buffer= new char[buf_len];
bout_vsnprintf(buffer,buf_len, s);

message.assign(buffer);
delete[] buffer;
}

const char* ParseException::what() const throw() {
Expand Down
3 changes: 1 addition & 2 deletions src/sys/msg_stack.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@ int MsgStack::push(const char *s, ...)
if(s != NULL) {

va_start(ap, s);
vsprintf(buffer, s, ap);
vsnprintf(buffer,MSG_MAX_SIZE, s, ap);
va_end(ap);

strncpy(m->str, buffer, MSG_MAX_SIZE);
m->str[MSG_MAX_SIZE] = '\0';

//output.write("Pushing '%s' -> %d\n", buffer, nmsg);
}else
Expand Down
9 changes: 4 additions & 5 deletions src/sys/optionsreader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ OptionsReader* OptionsReader::getInstance() {
void OptionsReader::read(Options *options, const char *file, ...) {
if(file == (const char*) NULL) throw new BoutException("OptionsReader::read passed NULL filename\n");

va_list ap; // List of arguments
char filename[512];
int buf_len=512;
char * filename=new char[buf_len];

va_start(ap, file);
vsprintf(filename, file, ap);
va_end(ap);
bout_vsnprintf(filename,buf_len, file);

output.write("Reading options file %s\n", filename);

Expand All @@ -35,6 +33,7 @@ void OptionsReader::read(Options *options, const char *file, ...) {

parser->read(options, filename);

delete[] filename;
delete parser;
}

Expand Down
Loading