Skip to content

Commit

Permalink
Fixing a few cases of undefined behaviour introduced by recent
Browse files Browse the repository at this point in the history
formatter work.

Thanks, Coverity!
  • Loading branch information
rsmmr committed Mar 14, 2014
1 parent 17f9d0a commit 8b24194
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/input/readers/Ascii.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Ascii::Ascii(ReaderFrontend *frontend) : ReaderBackend(frontend)
{
file = 0;
mtime = 0;
formatter = 0;
}

Ascii::~Ascii()
Expand Down
1 change: 1 addition & 0 deletions src/logging/writers/Ascii.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Ascii::Ascii(WriterFrontend* frontend) : WriterBackend(frontend)
fd = 0;
ascii_done = false;
tsv = false;
formatter = 0;
}

Ascii::~Ascii()
Expand Down
2 changes: 1 addition & 1 deletion src/threading/Formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Formatter {
* @return The new value, or null on error. Errors must also be
* flagged via the thread.
*/
virtual threading::Value* ParseValue(string s, string name, TypeTag type, TypeTag subtype = TYPE_ERROR) const = 0;
virtual threading::Value* ParseValue(const string& s, const string& name, TypeTag type, TypeTag subtype = TYPE_ERROR) const = 0;

/**
* Convert an IP address into a string.
Expand Down
26 changes: 15 additions & 11 deletions src/threading/formatters/Ascii.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ bool Ascii::Describe(ODesc* desc, threading::Value* val, const string& name) con
}


threading::Value* Ascii::ParseValue(string s, string name, TypeTag type, TypeTag subtype) const
threading::Value* Ascii::ParseValue(const string& s, const string& name, TypeTag type, TypeTag subtype) const
{
if ( s.compare(separators.unset_field) == 0 ) // field is not set...
return new threading::Value(type, false);
Expand All @@ -214,10 +214,12 @@ threading::Value* Ascii::ParseValue(string s, string name, TypeTag type, TypeTag
switch ( type ) {
case TYPE_ENUM:
case TYPE_STRING:
s = get_unescaped_string(s);
val->val.string_val.length = s.size();
val->val.string_val.data = copy_string(s.c_str());
{
string unescaped = get_unescaped_string(s);
val->val.string_val.length = unescaped.size();
val->val.string_val.data = copy_string(unescaped.c_str());
break;
}

case TYPE_BOOL:
if ( s == "T" )
Expand Down Expand Up @@ -263,31 +265,33 @@ threading::Value* Ascii::ParseValue(string s, string name, TypeTag type, TypeTag

case TYPE_SUBNET:
{
s = get_unescaped_string(s);
size_t pos = s.find("/");
if ( pos == s.npos )
string unescaped = get_unescaped_string(s);
size_t pos = unescaped.find("/");
if ( pos == unescaped.npos )
{
GetThread()->Error(GetThread()->Fmt("Invalid value for subnet: %s", start));
goto parse_error;
}

string width_str = s.substr(pos + 1);
string width_str = unescaped.substr(pos + 1);
uint8_t width = (uint8_t) strtol(width_str.c_str(), &end, 10);

if ( CheckNumberError(start, end) )
goto parse_error;

string addr = s.substr(0, pos);
string addr = unescaped.substr(0, pos);

val->val.subnet_val.prefix = ParseAddr(addr);
val->val.subnet_val.length = width;
break;
}

case TYPE_ADDR:
s = get_unescaped_string(s);
val->val.addr_val = ParseAddr(s);
{
string unescaped = get_unescaped_string(s);
val->val.addr_val = ParseAddr(unescaped);
break;
}

case TYPE_TABLE:
case TYPE_VECTOR:
Expand Down
2 changes: 1 addition & 1 deletion src/threading/formatters/Ascii.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Ascii : public Formatter {
virtual bool Describe(ODesc* desc, threading::Value* val, const string& name = "") const;
virtual bool Describe(ODesc* desc, int num_fields, const threading::Field* const * fields,
threading::Value** vals) const;
virtual threading::Value* ParseValue(string s, string name, TypeTag type, TypeTag subtype = TYPE_ERROR) const;
virtual threading::Value* ParseValue(const string& s, const string& name, TypeTag type, TypeTag subtype = TYPE_ERROR) const;

private:
bool CheckNumberError(const char* start, const char* end) const;
Expand Down
2 changes: 1 addition & 1 deletion src/threading/formatters/JSON.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ bool JSON::Describe(ODesc* desc, Value* val, const string& name) const
return true;
}

threading::Value* JSON::ParseValue(string s, string name, TypeTag type, TypeTag subtype) const
threading::Value* JSON::ParseValue(const string& s, const string& name, TypeTag type, TypeTag subtype) const
{
GetThread()->Error("JSON formatter does not support parsing yet.");
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/threading/formatters/JSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class JSON : public Formatter {
virtual bool Describe(ODesc* desc, threading::Value* val, const string& name = "") const;
virtual bool Describe(ODesc* desc, int num_fields, const threading::Field* const * fields,
threading::Value** vals) const;
virtual threading::Value* ParseValue(string s, string name, TypeTag type, TypeTag subtype = TYPE_ERROR) const;
virtual threading::Value* ParseValue(const string& s, const string& name, TypeTag type, TypeTag subtype = TYPE_ERROR) const;

private:
TimeFormat timestamps;
Expand Down

0 comments on commit 8b24194

Please sign in to comment.