Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/topic/jsiwek/coverity'
Browse files Browse the repository at this point in the history
* origin/topic/jsiwek/coverity:
  Fix uninitialized (or unused) fields.
  Generate initialization code for external types.
  Optimize negative string length check.
  • Loading branch information
rsmmr committed Oct 2, 2013
2 parents d433188 + 323d02e commit a5700df
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 42 deletions.
11 changes: 11 additions & 0 deletions CHANGES
@@ -1,4 +1,15 @@

0.4-5 | 2013-10-02 10:33:05 -0700

* Fix uninitialized (or unused) fields. (Jon Siwek)

* Generate initialization code for external types. Numeric/pointer
types can be initialized to 0. (Jon Siwek)

* Optimize negative string length check. (Jon Siwek)

* Fix for setting REPO in Makefile. (Robin Sommer)

0.4 | 2013-09-23 20:56:19 -0700

* Update 'make dist' target. (Jon Siwek)
Expand Down
2 changes: 1 addition & 1 deletion README
@@ -1,7 +1,7 @@
.. -*- mode: rst-mode -*-
..
.. Version number is filled in automatically.
.. |version| replace:: 0.4
.. |version| replace:: 0.4-5

======
BinPAC
Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.4
0.4-5
1 change: 1 addition & 0 deletions src/pac_case.cc
Expand Up @@ -240,6 +240,7 @@ CaseField::CaseField(ExprList* index, ID* id, Type* type)
ASSERT(type_);
type_->set_value_var(id, MEMBER_VAR);
case_type_ = 0;
index_var_ = 0;
}

CaseField::~CaseField()
Expand Down
2 changes: 0 additions & 2 deletions src/pac_decl.h
Expand Up @@ -42,7 +42,6 @@ class Decl : public Object
ID *id_;
DeclType decl_type_;
AttrList *attrlist_;
ExprList *expr_list_;
AnalyzerContextDecl *analyzer_context_;

public:
Expand Down Expand Up @@ -72,7 +71,6 @@ class HelperDecl : public Decl
private:
HelperType helper_type_;
ID *context_id_;
ID *helper_id_;
EmbeddedCode *code_;

static int helper_id_seq;
Expand Down
2 changes: 1 addition & 1 deletion src/pac_exception.cc
Expand Up @@ -64,7 +64,7 @@ ExceptionPaddingError::ExceptionPaddingError(const Object* o, const char* msg)
}

ExceptionNonConstExpr::ExceptionNonConstExpr(const Expr* expr)
: Exception(expr)
: Exception(expr), expr(expr)
{
append(fmt("Expression `%s' is not constant", expr->orig()));
}
27 changes: 18 additions & 9 deletions src/pac_exttype.cc
@@ -1,21 +1,22 @@
#include "pac_exttype.h"
#include "pac_id.h"
#include "pac_decl.h"
#include "pac_output.h"

bool ExternType::DefineValueVar() const
{
return true;
}

string ExternType::DataTypeStr() const
{
{
switch ( ext_type_ )
{
case PLAIN:
case NUMBER:
return id_->Name();
return id_->Name();
case POINTER:
return string(id_->Name()) + " *";
return string(id_->Name()) + " *";
default:
ASSERT(0);
return "";
Expand All @@ -28,18 +29,26 @@ int ExternType::StaticSize(Env* env) const
return -1;
}

bool ExternType::ByteOrderSensitive() const
{
bool ExternType::ByteOrderSensitive() const
{
return false;
}

string ExternType::EvalMember(const ID *member_id) const
{
return strfmt("%s%s",
return strfmt("%s%s",
ext_type_ == POINTER ? "->" : ".",
member_id->Name());
}

void ExternType::GenInitCode(Output* out_cc, Env* env)
{
if ( IsNumericType() || IsPointerType() )
out_cc->println("%s = 0;", env->LValue(value_var()));

Type::GenInitCode(out_cc, env);
}

void ExternType::DoGenParseCode(Output* out, Env* env, const DataPtr& data, int flags)
{
ASSERT(0);
Expand All @@ -51,8 +60,8 @@ void ExternType::GenDynamicSize(Output* out, Env* env, const DataPtr& data)
}

Type *ExternType::DoClone() const
{
return new ExternType(id_->clone(), ext_type_);
{
return new ExternType(id_->clone(), ext_type_);
}

// Definitions of pre-defined external types
Expand All @@ -70,7 +79,7 @@ void ExternType::static_init()
#define EXTERNTYPE(name, ctype, exttype) \
id = new ID(#ctype); \
extern_type_##name = new ExternType(id, ExternType::exttype); \
Type::AddPredefinedType(#name, extern_type_##name);
Type::AddPredefinedType(#name, extern_type_##name);
#include "pac_externtype.def"
#undef EXTERNTYPE
}
8 changes: 5 additions & 3 deletions src/pac_exttype.h
Expand Up @@ -6,15 +6,15 @@
// ExternType represent external C++ types that are not defined in
// PAC specification (therefore they cannot appear in data layout
// spefication, e.g., in a record field). The type name is copied
// literally to the compiled code.
// literally to the compiled code.

class ExternType : public Type
{
public:
enum EXTType { PLAIN, NUMBER, POINTER };
ExternType(const ID *id, EXTType ext_type)
: Type(EXTERN),
id_(id),
: Type(EXTERN),
id_(id),
ext_type_(ext_type) {}

bool DefineValueVar() const;
Expand All @@ -26,6 +26,8 @@ class ExternType : public Type
bool IsNumericType() const { return ext_type_ == NUMBER; }
bool IsPointerType() const { return ext_type_ == POINTER; }

void GenInitCode(Output *out_cc, Env *env);

protected:
void DoGenParseCode(Output *out, Env *env, const DataPtr& data, int flags);
void GenDynamicSize(Output *out, Env *env, const DataPtr& data);
Expand Down
35 changes: 19 additions & 16 deletions src/pac_id.cc
Expand Up @@ -90,24 +90,27 @@ IDRecord::IDRecord(Env *arg_env, const ID* arg_id, IDType arg_id_type)
lvalue = "@FUNC_PARAM@";
break;
}

data_type = 0;
field = 0;
constant_set = false;
constant = constant_set = false;
macro = 0;
}

IDRecord::~IDRecord()
{
}

void IDRecord::SetConstant(int c)
{
void IDRecord::SetConstant(int c)
{
ASSERT(id_type == CONST);
constant_set = true;
constant = c;
constant = c;
}

bool IDRecord::GetConstant(int *pc) const
{
if ( constant_set )
{
if ( constant_set )
*pc = constant;
return constant_set;
}
Expand All @@ -131,8 +134,8 @@ void IDRecord::SetEvaluated(bool v)
evaluated = v;
}

void IDRecord::Evaluate(Output* out, Env* env)
{
void IDRecord::Evaluate(Output* out, Env* env)
{
if ( evaluated )
return;

Expand All @@ -141,12 +144,12 @@ void IDRecord::Evaluate(Output* out, Env* env)

if ( ! eval )
throw Exception(id, "no evaluation method");

if ( in_evaluation )
throw ExceptionCyclicDependence(id);

in_evaluation = true;
eval->GenEval(out, env);
eval->GenEval(out, env);
in_evaluation = false;

evaluated = true;
Expand Down Expand Up @@ -283,7 +286,7 @@ bool Env::Evaluated(const ID* id) const
return r->Evaluated();
else
// Assume undefined variables are already evaluated
return true;
return true;
}

void Env::SetEvaluated(const ID* id, bool v)
Expand Down Expand Up @@ -336,7 +339,7 @@ Type* Env::GetDataType(const ID* id) const
if ( r )
return r->GetDataType();
else
return 0;
return 0;
}

string Env::DataTypeStr(const ID *id) const
Expand Down Expand Up @@ -396,8 +399,8 @@ void init_builtin_identifiers()
dataunit_id = new ID("dataunit");
flow_buffer_id = new ID("flow_buffer");
element_macro_id = new ID("$element");
input_macro_id = new ID("$input");
context_macro_id = new ID("$context");
input_macro_id = new ID("$input");
context_macro_id = new ID("$context");
parsing_state_id = new ID("parsing_state");
buffering_state_id = new ID("buffering_state");

Expand Down Expand Up @@ -425,8 +428,8 @@ Env* global_env()
the_global_env->AddConstID(null_id, 0, extern_type_nullptr);

#if 0
the_global_env->AddID(null_byteseg_id,
GLOBAL_VAR,
the_global_env->AddID(null_byteseg_id,
GLOBAL_VAR,
extern_type_const_byteseg);
#endif
}
Expand Down
1 change: 0 additions & 1 deletion src/pac_paramtype.h
Expand Up @@ -52,7 +52,6 @@ class ParameterizedType : public Type
private:
ID *type_id_;
ExprList *args_;
char *data_type_;
bool checking_requires_analyzer_context_;

void DoGenParseCode(Output *out, Env *env, const DataPtr& data, int flags);
Expand Down
1 change: 1 addition & 0 deletions src/pac_record.cc
Expand Up @@ -252,6 +252,7 @@ RecordField::RecordField(FieldType tof, ID *id, Type *type)
prev_ = 0;
next_ = 0;
static_offset_ = -1;
parsing_state_seq_ = 0;
boundary_checked_ = false;
}

Expand Down
26 changes: 20 additions & 6 deletions src/pac_strtype.cc
Expand Up @@ -280,12 +280,26 @@ void StringType::DoGenParseCode(Output* out_cc, Env* env,
if ( ! anonymous_value_var() )
{
// Set the value variable
out_cc->println("// check for negative sizes");
out_cc->println("if ( %s < 0 )",
str_size.c_str());
out_cc->println(
"throw binpac::ExceptionInvalidStringLength(\"%s\", %s);",
Location(), str_size.c_str());

int len;

if ( type_ == ANYSTR && attr_length_expr_ &&
attr_length_expr_->ConstFold(env, &len) )
{
// can check for a negative length now
if ( len < 0 )
throw Exception(this, "negative &length on string");
}
else
{
out_cc->println("// check for negative sizes");
out_cc->println("if ( %s < 0 )",
str_size.c_str());
out_cc->println(
"throw binpac::ExceptionInvalidStringLength(\"%s\", %s);",
Location(), str_size.c_str());
}

out_cc->println("%s.init(%s, %s);",
env->LValue(value_var()),
data.ptr_expr(),
Expand Down
2 changes: 0 additions & 2 deletions src/pac_typedecl.h
Expand Up @@ -42,8 +42,6 @@ class TypeDecl : public Decl

ParamList *params_;
Type *type_;

bool inherits_frame_buffer_;
};

#endif // pac_typedecl_h

0 comments on commit a5700df

Please sign in to comment.