Fix a number of cases in the parser where segfaults were possible with malformed programs.
This commit is contained in:
45
parse.yy
45
parse.yy
@@ -305,10 +305,14 @@ cast_expression
|
|||||||
// uniform float x = 1. / (float)y;
|
// uniform float x = 1. / (float)y;
|
||||||
// don't issue an error due to (float)y being inadvertently
|
// don't issue an error due to (float)y being inadvertently
|
||||||
// and undesirably-to-the-user "varying"...
|
// and undesirably-to-the-user "varying"...
|
||||||
|
if ($2 == NULL || $4 == NULL || $4->GetType() == NULL)
|
||||||
|
$$ = NULL;
|
||||||
|
else {
|
||||||
if ($4->GetType()->IsUniformType())
|
if ($4->GetType()->IsUniformType())
|
||||||
$2 = $2->GetAsUniformType();
|
$2 = $2->GetAsUniformType();
|
||||||
$$ = new TypeCastExpr($2, $4, @1);
|
$$ = new TypeCastExpr($2, $4, @1);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
multiplicative_expression
|
multiplicative_expression
|
||||||
@@ -459,6 +463,7 @@ declaration_specifiers
|
|||||||
| storage_class_specifier declaration_specifiers
|
| storage_class_specifier declaration_specifiers
|
||||||
{
|
{
|
||||||
DeclSpecs *ds = (DeclSpecs *)$2;
|
DeclSpecs *ds = (DeclSpecs *)$2;
|
||||||
|
if (ds != NULL) {
|
||||||
if (ds->storageClass != SC_NONE)
|
if (ds->storageClass != SC_NONE)
|
||||||
Error(@1, "Multiple storage class specifiers in a declaration are illegal. "
|
Error(@1, "Multiple storage class specifiers in a declaration are illegal. "
|
||||||
"(Have provided both \"%s\" and \"%s\".)",
|
"(Have provided both \"%s\" and \"%s\".)",
|
||||||
@@ -466,6 +471,7 @@ declaration_specifiers
|
|||||||
lGetStorageClassString($1));
|
lGetStorageClassString($1));
|
||||||
else
|
else
|
||||||
ds->storageClass = $1;
|
ds->storageClass = $1;
|
||||||
|
}
|
||||||
$$ = ds;
|
$$ = ds;
|
||||||
}
|
}
|
||||||
| soa_width_specifier
|
| soa_width_specifier
|
||||||
@@ -477,10 +483,12 @@ declaration_specifiers
|
|||||||
| soa_width_specifier declaration_specifiers
|
| soa_width_specifier declaration_specifiers
|
||||||
{
|
{
|
||||||
DeclSpecs *ds = (DeclSpecs *)$2;
|
DeclSpecs *ds = (DeclSpecs *)$2;
|
||||||
|
if (ds != NULL) {
|
||||||
if (ds->soaWidth != 0)
|
if (ds->soaWidth != 0)
|
||||||
Error(@1, "soa<> qualifier supplied multiple times in declaration.");
|
Error(@1, "soa<> qualifier supplied multiple times in declaration.");
|
||||||
else
|
else
|
||||||
ds->soaWidth = $1;
|
ds->soaWidth = $1;
|
||||||
|
}
|
||||||
$$ = ds;
|
$$ = ds;
|
||||||
}
|
}
|
||||||
| type_specifier
|
| type_specifier
|
||||||
@@ -496,9 +504,11 @@ declaration_specifiers
|
|||||||
| type_specifier declaration_specifiers
|
| type_specifier declaration_specifiers
|
||||||
{
|
{
|
||||||
DeclSpecs *ds = (DeclSpecs *)$2;
|
DeclSpecs *ds = (DeclSpecs *)$2;
|
||||||
|
if (ds != NULL) {
|
||||||
if (ds->baseType != NULL)
|
if (ds->baseType != NULL)
|
||||||
Error(@1, "Multiple types provided for declaration.");
|
Error(@1, "Multiple types provided for declaration.");
|
||||||
ds->baseType = $1;
|
ds->baseType = $1;
|
||||||
|
}
|
||||||
$$ = ds;
|
$$ = ds;
|
||||||
}
|
}
|
||||||
| type_qualifier
|
| type_qualifier
|
||||||
@@ -508,6 +518,7 @@ declaration_specifiers
|
|||||||
| type_qualifier declaration_specifiers
|
| type_qualifier declaration_specifiers
|
||||||
{
|
{
|
||||||
DeclSpecs *ds = (DeclSpecs *)$2;
|
DeclSpecs *ds = (DeclSpecs *)$2;
|
||||||
|
if (ds != NULL)
|
||||||
ds->typeQualifier |= $1;
|
ds->typeQualifier |= $1;
|
||||||
$$ = ds;
|
$$ = ds;
|
||||||
}
|
}
|
||||||
@@ -523,6 +534,7 @@ init_declarator_list
|
|||||||
| init_declarator_list ',' init_declarator
|
| init_declarator_list ',' init_declarator
|
||||||
{
|
{
|
||||||
std::vector<Declarator *> *dl = (std::vector<Declarator *> *)$1;
|
std::vector<Declarator *> *dl = (std::vector<Declarator *> *)$1;
|
||||||
|
if (dl != NULL && $3 != NULL)
|
||||||
dl->push_back($3);
|
dl->push_back($3);
|
||||||
$$ = $1;
|
$$ = $1;
|
||||||
}
|
}
|
||||||
@@ -530,7 +542,12 @@ init_declarator_list
|
|||||||
|
|
||||||
init_declarator
|
init_declarator
|
||||||
: declarator
|
: declarator
|
||||||
| declarator '=' initializer { $1->initExpr = $3; $$ = $1; }
|
| declarator '=' initializer
|
||||||
|
{
|
||||||
|
if ($1 != NULL)
|
||||||
|
$1->initExpr = $3;
|
||||||
|
$$ = $1;
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
storage_class_specifier
|
storage_class_specifier
|
||||||
@@ -637,12 +654,14 @@ struct_declaration_list
|
|||||||
: struct_declaration
|
: struct_declaration
|
||||||
{
|
{
|
||||||
std::vector<StructDeclaration *> *sdl = new std::vector<StructDeclaration *>;
|
std::vector<StructDeclaration *> *sdl = new std::vector<StructDeclaration *>;
|
||||||
|
if (sdl != NULL && $1 != NULL)
|
||||||
sdl->push_back($1);
|
sdl->push_back($1);
|
||||||
$$ = sdl;
|
$$ = sdl;
|
||||||
}
|
}
|
||||||
| struct_declaration_list struct_declaration
|
| struct_declaration_list struct_declaration
|
||||||
{
|
{
|
||||||
std::vector<StructDeclaration *> *sdl = (std::vector<StructDeclaration *> *)$1;
|
std::vector<StructDeclaration *> *sdl = (std::vector<StructDeclaration *> *)$1;
|
||||||
|
if (sdl != NULL && $2 != NULL)
|
||||||
sdl->push_back($2);
|
sdl->push_back($2);
|
||||||
$$ = $1;
|
$$ = $1;
|
||||||
}
|
}
|
||||||
@@ -659,6 +678,7 @@ specifier_qualifier_list
|
|||||||
| short_vec_specifier
|
| short_vec_specifier
|
||||||
| type_qualifier specifier_qualifier_list
|
| type_qualifier specifier_qualifier_list
|
||||||
{
|
{
|
||||||
|
if ($2 != NULL) {
|
||||||
if ($1 == TYPEQUAL_UNIFORM)
|
if ($1 == TYPEQUAL_UNIFORM)
|
||||||
$$ = $2->GetAsUniformType();
|
$$ = $2->GetAsUniformType();
|
||||||
else if ($1 == TYPEQUAL_VARYING)
|
else if ($1 == TYPEQUAL_VARYING)
|
||||||
@@ -681,6 +701,9 @@ specifier_qualifier_list
|
|||||||
UNIMPLEMENTED;
|
UNIMPLEMENTED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
$$ = NULL;
|
||||||
|
}
|
||||||
/* K&R--implicit int type--e.g. "static foo" -> foo is an int */
|
/* K&R--implicit int type--e.g. "static foo" -> foo is an int */
|
||||||
/* | type_qualifier { UNIMPLEMENTED; }*/
|
/* | type_qualifier { UNIMPLEMENTED; }*/
|
||||||
;
|
;
|
||||||
@@ -690,12 +713,14 @@ struct_declarator_list
|
|||||||
: struct_declarator
|
: struct_declarator
|
||||||
{
|
{
|
||||||
std::vector<Declarator *> *sdl = new std::vector<Declarator *>;
|
std::vector<Declarator *> *sdl = new std::vector<Declarator *>;
|
||||||
|
if ($1 != NULL)
|
||||||
sdl->push_back($1);
|
sdl->push_back($1);
|
||||||
$$ = sdl;
|
$$ = sdl;
|
||||||
}
|
}
|
||||||
| struct_declarator_list ',' struct_declarator
|
| struct_declarator_list ',' struct_declarator
|
||||||
{
|
{
|
||||||
std::vector<Declarator *> *sdl = (std::vector<Declarator *> *)$1;
|
std::vector<Declarator *> *sdl = (std::vector<Declarator *> *)$1;
|
||||||
|
if (sdl != NULL && $3 != NULL)
|
||||||
sdl->push_back($3);
|
sdl->push_back($3);
|
||||||
$$ = $1;
|
$$ = $1;
|
||||||
}
|
}
|
||||||
@@ -757,8 +782,9 @@ direct_declarator
|
|||||||
| direct_declarator '[' constant_expression ']'
|
| direct_declarator '[' constant_expression ']'
|
||||||
{
|
{
|
||||||
Expr *size = $3;
|
Expr *size = $3;
|
||||||
if (size) size = size->TypeCheck();
|
if (size)
|
||||||
if (size) {
|
size = size->TypeCheck();
|
||||||
|
if (size && $1 != NULL) {
|
||||||
size = size->Optimize();
|
size = size->Optimize();
|
||||||
llvm::Constant *cval = size->GetConstant(size->GetType());
|
llvm::Constant *cval = size->GetConstant(size->GetType());
|
||||||
if (!cval) {
|
if (!cval) {
|
||||||
@@ -780,20 +806,24 @@ direct_declarator
|
|||||||
}
|
}
|
||||||
| direct_declarator '[' ']'
|
| direct_declarator '[' ']'
|
||||||
{
|
{
|
||||||
|
if ($1 != NULL)
|
||||||
$1->AddArrayDimension(-1); // unsized
|
$1->AddArrayDimension(-1); // unsized
|
||||||
$$ = $1;
|
$$ = $1;
|
||||||
}
|
}
|
||||||
| direct_declarator '(' parameter_type_list ')'
|
| direct_declarator '(' parameter_type_list ')'
|
||||||
{
|
{
|
||||||
Declarator *d = (Declarator *)$1;
|
Declarator *d = (Declarator *)$1;
|
||||||
|
if (d != NULL) {
|
||||||
d->isFunction = true;
|
d->isFunction = true;
|
||||||
d->functionArgs = $3;
|
d->functionArgs = $3;
|
||||||
|
}
|
||||||
$$ = d;
|
$$ = d;
|
||||||
}
|
}
|
||||||
/* K&R? | direct_declarator '(' identifier_list ')' */
|
/* K&R? | direct_declarator '(' identifier_list ')' */
|
||||||
| direct_declarator '(' ')'
|
| direct_declarator '(' ')'
|
||||||
{
|
{
|
||||||
Declarator *d = (Declarator *)$1;
|
Declarator *d = (Declarator *)$1;
|
||||||
|
if (d != NULL)
|
||||||
d->isFunction = true;
|
d->isFunction = true;
|
||||||
$$ = d;
|
$$ = d;
|
||||||
}
|
}
|
||||||
@@ -844,6 +874,7 @@ parameter_declaration
|
|||||||
}
|
}
|
||||||
| declaration_specifiers declarator '=' initializer
|
| declaration_specifiers declarator '=' initializer
|
||||||
{
|
{
|
||||||
|
if ($2 != NULL)
|
||||||
$2->initExpr = $4;
|
$2->initExpr = $4;
|
||||||
$$ = new Declaration($1, $2);
|
$$ = new Declaration($1, $2);
|
||||||
|
|
||||||
@@ -900,11 +931,15 @@ initializer_list
|
|||||||
{ $$ = new ExprList($1, @1); }
|
{ $$ = new ExprList($1, @1); }
|
||||||
| initializer_list ',' initializer
|
| initializer_list ',' initializer
|
||||||
{
|
{
|
||||||
|
if ($1 == NULL)
|
||||||
|
$$ = NULL;
|
||||||
|
else {
|
||||||
ExprList *exprList = dynamic_cast<ExprList *>($1);
|
ExprList *exprList = dynamic_cast<ExprList *>($1);
|
||||||
assert(exprList);
|
assert(exprList);
|
||||||
exprList->exprs.push_back($3);
|
exprList->exprs.push_back($3);
|
||||||
$$ = exprList;
|
$$ = exprList;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
statement
|
statement
|
||||||
@@ -960,6 +995,7 @@ statement_list
|
|||||||
}
|
}
|
||||||
| statement_list statement
|
| statement_list statement
|
||||||
{
|
{
|
||||||
|
if ($1 != NULL)
|
||||||
((StmtList *)$1)->Add($2);
|
((StmtList *)$1)->Add($2);
|
||||||
$$ = $1;
|
$$ = $1;
|
||||||
}
|
}
|
||||||
@@ -1084,6 +1120,7 @@ external_declaration
|
|||||||
| TOKEN_EXTERN TOKEN_STRING_LITERAL '{' declaration '}' // FIXME: make sure string=="C"
|
| TOKEN_EXTERN TOKEN_STRING_LITERAL '{' declaration '}' // FIXME: make sure string=="C"
|
||||||
| declaration
|
| declaration
|
||||||
{
|
{
|
||||||
|
if ($1 != NULL)
|
||||||
for (unsigned int i = 0; i < $1->declarators.size(); ++i)
|
for (unsigned int i = 0; i < $1->declarators.size(); ++i)
|
||||||
m->AddGlobal($1->declSpecs, $1->declarators[i]);
|
m->AddGlobal($1->declSpecs, $1->declarators[i]);
|
||||||
}
|
}
|
||||||
@@ -1127,6 +1164,8 @@ lAddFunctionParams(Declarator *decl) {
|
|||||||
if (decl->functionArgs) {
|
if (decl->functionArgs) {
|
||||||
for (unsigned int i = 0; i < decl->functionArgs->size(); ++i) {
|
for (unsigned int i = 0; i < decl->functionArgs->size(); ++i) {
|
||||||
Declaration *pdecl = (*decl->functionArgs)[i];
|
Declaration *pdecl = (*decl->functionArgs)[i];
|
||||||
|
if (pdecl == NULL)
|
||||||
|
continue;
|
||||||
assert(pdecl->declarators.size() == 1);
|
assert(pdecl->declarators.size() == 1);
|
||||||
Symbol *sym = pdecl->declarators[0]->sym;
|
Symbol *sym = pdecl->declarators[0]->sym;
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
|
|||||||
Reference in New Issue
Block a user