Skip to content

Commit f6a100e

Browse files
authored
Merge pull request #2335 from mgreter/code-hardening
Code hardening
2 parents 4eeb891 + ead7513 commit f6a100e

22 files changed

+128
-106
lines changed

src/ast.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -961,13 +961,13 @@ namespace Sass {
961961

962962
// do some magic we inherit from node and extend
963963
Node node = subweave(lhsNode, rhsNode);
964-
Selector_List_Ptr result = SASS_MEMORY_NEW(Selector_List, pstate());
964+
Selector_List_Obj result = SASS_MEMORY_NEW(Selector_List, pstate());
965965
NodeDequePtr col = node.collection(); // move from collection to list
966966
for (NodeDeque::iterator it = col->begin(), end = col->end(); it != end; it++)
967967
{ result->append(nodeToComplexSelector(Node::naiveTrim(*it))); }
968968

969969
// only return if list has some entries
970-
return result->length() ? result : 0;
970+
return result->length() ? result.detach() : 0;
971971

972972
}
973973

@@ -1688,31 +1688,31 @@ namespace Sass {
16881688
void Arguments::adjust_after_pushing(Argument_Obj a)
16891689
{
16901690
if (!a->name().empty()) {
1691-
if (/* has_rest_argument_ || */ has_keyword_argument_) {
1691+
if (has_keyword_argument()) {
16921692
error("named arguments must precede variable-length argument", a->pstate());
16931693
}
1694-
has_named_arguments_ = true;
1694+
has_named_arguments(true);
16951695
}
16961696
else if (a->is_rest_argument()) {
1697-
if (has_rest_argument_) {
1697+
if (has_rest_argument()) {
16981698
error("functions and mixins may only be called with one variable-length argument", a->pstate());
16991699
}
17001700
if (has_keyword_argument_) {
17011701
error("only keyword arguments may follow variable arguments", a->pstate());
17021702
}
1703-
has_rest_argument_ = true;
1703+
has_rest_argument(true);
17041704
}
17051705
else if (a->is_keyword_argument()) {
1706-
if (has_keyword_argument_) {
1706+
if (has_keyword_argument()) {
17071707
error("functions and mixins may only be called with one keyword argument", a->pstate());
17081708
}
1709-
has_keyword_argument_ = true;
1709+
has_keyword_argument(true);
17101710
}
17111711
else {
1712-
if (has_rest_argument_) {
1712+
if (has_rest_argument()) {
17131713
error("ordinal arguments must precede variable-length arguments", a->pstate());
17141714
}
1715-
if (has_named_arguments_) {
1715+
if (has_named_arguments()) {
17161716
error("ordinal arguments must precede named arguments", a->pstate());
17171717
}
17181718
}
@@ -1867,7 +1867,7 @@ namespace Sass {
18671867
// maybe convert to other unit
18681868
// easier implemented on its own
18691869
try { convert(prefered, strict); }
1870-
catch (incompatibleUnits& err)
1870+
catch (Exception::IncompatibleUnits& err)
18711871
{ error(err.what(), pstate()); }
18721872
catch (...) { throw; }
18731873

src/ast.hpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,11 @@ namespace Sass {
358358
void reset_duplicate_key() { duplicate_key_ = 0; }
359359
virtual void adjust_after_pushing(std::pair<Expression_Obj, Expression_Obj> p) { }
360360
public:
361-
Hashed(size_t s = 0) : elements_(ExpressionMap(s)), list_(std::vector<Expression_Obj>())
362-
{ elements_.reserve(s); list_.reserve(s); reset_duplicate_key(); }
361+
Hashed(size_t s = 0)
362+
: elements_(ExpressionMap(s)),
363+
list_(std::vector<Expression_Obj>()),
364+
hash_(0), duplicate_key_(NULL)
365+
{ elements_.reserve(s); list_.reserve(s); }
363366
virtual ~Hashed();
364367
size_t length() const { return list_.size(); }
365368
bool empty() const { return list_.empty(); }
@@ -1036,9 +1039,13 @@ namespace Sass {
10361039
class Content : public Statement {
10371040
ADD_PROPERTY(Media_Block_Ptr, media_block)
10381041
public:
1039-
Content(ParserState pstate) : Statement(pstate)
1042+
Content(ParserState pstate)
1043+
: Statement(pstate),
1044+
media_block_(NULL)
10401045
{ statement_type(CONTENT); }
1041-
Content(const Content* ptr) : Statement(ptr)
1046+
Content(const Content* ptr)
1047+
: Statement(ptr),
1048+
media_block_(ptr->media_block_)
10421049
{ statement_type(CONTENT); }
10431050
ATTACH_AST_OPERATIONS(Content)
10441051
ATTACH_OPERATIONS()
@@ -2217,22 +2224,22 @@ namespace Sass {
22172224
void adjust_after_pushing(Parameter_Obj p)
22182225
{
22192226
if (p->default_value()) {
2220-
if (has_rest_parameter_) {
2227+
if (has_rest_parameter()) {
22212228
error("optional parameters may not be combined with variable-length parameters", p->pstate());
22222229
}
2223-
has_optional_parameters_ = true;
2230+
has_optional_parameters(true);
22242231
}
22252232
else if (p->is_rest_parameter()) {
2226-
if (has_rest_parameter_) {
2233+
if (has_rest_parameter()) {
22272234
error("functions and mixins cannot have more than one variable-length parameter", p->pstate());
22282235
}
2229-
has_rest_parameter_ = true;
2236+
has_rest_parameter(true);
22302237
}
22312238
else {
2232-
if (has_rest_parameter_) {
2239+
if (has_rest_parameter()) {
22332240
error("required parameters must precede variable-length parameters", p->pstate());
22342241
}
2235-
if (has_optional_parameters_) {
2242+
if (has_optional_parameters()) {
22362243
error("required parameters must precede optional parameters", p->pstate());
22372244
}
22382245
}
@@ -2326,13 +2333,15 @@ namespace Sass {
23262333
: AST_Node(pstate),
23272334
contents_(c),
23282335
connect_parent_(true),
2329-
media_block_(NULL)
2336+
media_block_(NULL),
2337+
hash_(0)
23302338
{ }
23312339
Selector_Schema(const Selector_Schema* ptr)
23322340
: AST_Node(ptr),
23332341
contents_(ptr->contents_),
23342342
connect_parent_(ptr->connect_parent_),
2335-
media_block_(ptr->media_block_)
2343+
media_block_(ptr->media_block_),
2344+
hash_(ptr->hash_)
23362345
{ }
23372346
virtual bool has_parent_ref() const;
23382347
virtual bool has_real_parent_ref() const;

src/b64/encode.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ namespace base64
2424

2525
encoder(int buffersize_in = BUFFERSIZE)
2626
: _buffersize(buffersize_in)
27-
{}
27+
{
28+
base64_init_encodestate(&_state);
29+
}
2830

2931
int encode(char value_in)
3032
{

src/backtrace.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ namespace Sass {
6060

6161
size_t depth()
6262
{
63-
size_t d = 0;
63+
size_t d = std::string::npos;
6464
Backtrace* p = parent;
6565
while (p) {
6666
++d;
6767
p = p->parent;
6868
}
69-
return d-1;
69+
return d;
7070
}
7171

7272
};

src/context.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ namespace Sass {
7272
sheets(),
7373
subset_map(),
7474
import_stack(),
75+
callee_stack(),
76+
c_compiler(NULL),
7577

7678
c_headers (std::vector<Sass_Importer_Entry>()),
7779
c_importers (std::vector<Sass_Importer_Entry>()),

src/cssize.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,6 @@ namespace Sass {
476476
ss->tabs(ss->tabs() + node->tabs());
477477
ss->group_end(node->group_end());
478478

479-
if (!ss) continue;
480-
481479
Block_Obj bb = SASS_MEMORY_NEW(Block,
482480
children->pstate(),
483481
children->length(),
@@ -584,10 +582,11 @@ namespace Sass {
584582
}
585583

586584
Media_Query_Ptr mm = SASS_MEMORY_NEW(Media_Query,
587-
588-
mq1->pstate(), 0,
589-
mq1->length() + mq2->length(), mod == "not", mod == "only"
590-
);
585+
mq1->pstate(),
586+
0,
587+
mq1->length() + mq2->length(),
588+
mod == "not",
589+
mod == "only");
591590

592591
if (!type.empty()) {
593592
mm->media_type(SASS_MEMORY_NEW(String_Quoted, mq1->pstate(), type));

src/error_handling.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ namespace Sass {
111111
}
112112

113113
IncompatibleUnits::IncompatibleUnits(const Number& lhs, const Number& rhs)
114-
: lhs(lhs), rhs(rhs)
115114
{
116115
msg = "Incompatible units: '";
117116
msg += rhs.unit();
@@ -120,6 +119,15 @@ namespace Sass {
120119
msg += "'.";
121120
}
122121

122+
IncompatibleUnits::IncompatibleUnits(const UnitType lhs, const UnitType rhs)
123+
{
124+
msg = "Incompatible units: '";
125+
msg += unit_to_string(rhs);
126+
msg += "' and '";
127+
msg += unit_to_string(lhs);
128+
msg += "'.";
129+
}
130+
123131
AlphaChannelsNotEqual::AlphaChannelsNotEqual(Expression_Ptr_Const lhs, Expression_Ptr_Const rhs, const std::string& op)
124132
: lhs(lhs), rhs(rhs), op(op)
125133
{

src/error_handling.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <sstream>
66
#include <stdexcept>
77
#include "position.hpp"
8+
#include "ast_fwd_decl.hpp"
9+
#include "sass/functions.h"
810

911
namespace Sass {
1012

@@ -136,10 +138,11 @@ namespace Sass {
136138

137139
class IncompatibleUnits : public OperationError {
138140
protected:
139-
const Number& lhs;
140-
const Number& rhs;
141+
// const Sass::UnitType lhs;
142+
// const Sass::UnitType rhs;
141143
public:
142144
IncompatibleUnits(const Number& lhs, const Number& rhs);
145+
IncompatibleUnits(const UnitType lhs, const UnitType rhs);
143146
virtual ~IncompatibleUnits() throw() {};
144147
};
145148

src/eval.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,20 +1597,19 @@ namespace Sass {
15971597

15981598
if (ltype == Expression::NULL_VAL) throw Exception::InvalidNullOperation(&lhs, &rhs, sass_op_to_name(op));
15991599
if (rtype == Expression::NULL_VAL) throw Exception::InvalidNullOperation(&lhs, &rhs, sass_op_to_name(op));
1600-
if (op == Sass_OP::MOD) throw Exception::UndefinedOperation(&lhs, &rhs, sass_op_to_name(op));
1601-
if (op == Sass_OP::MUL) throw Exception::UndefinedOperation(&lhs, &rhs, sass_op_to_name(op));
16021600
std::string sep;
16031601
switch (op) {
16041602
case Sass_OP::SUB: sep = "-"; break;
16051603
case Sass_OP::DIV: sep = "/"; break;
1606-
case Sass_OP::MUL: sep = "*"; break;
1607-
case Sass_OP::MOD: sep = "%"; break;
1604+
// cases are already handled above
16081605
case Sass_OP::EQ: sep = "=="; break;
16091606
case Sass_OP::NEQ: sep = "!="; break;
16101607
case Sass_OP::LT: sep = "<"; break;
16111608
case Sass_OP::GT: sep = ">"; break;
16121609
case Sass_OP::LTE: sep = "<="; break;
16131610
case Sass_OP::GTE: sep = ">="; break;
1611+
case Sass_OP::MUL: throw Exception::UndefinedOperation(&lhs, &rhs, sass_op_to_name(op));
1612+
case Sass_OP::MOD: throw Exception::UndefinedOperation(&lhs, &rhs, sass_op_to_name(op));
16141613
default: break;
16151614
}
16161615

src/extend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,8 +2074,8 @@ namespace Sass {
20742074
if (it.first) sel = it.first->first();
20752075
if (it.second) ext = it.second;
20762076
if (ext && (ext->extended() || ext->is_optional())) continue;
2077-
std::string str_sel(sel->to_string({ NESTED, 5 }));
2078-
std::string str_ext(ext->to_string({ NESTED, 5 }));
2077+
std::string str_sel(sel ? sel->to_string({ NESTED, 5 }) : "NULL");
2078+
std::string str_ext(ext ? ext->to_string({ NESTED, 5 }) : "NULL");
20792079
// debug_ast(sel, "sel: ");
20802080
// debug_ast(ext, "ext: ");
20812081
error("\"" + str_sel + "\" failed to @extend \"" + str_ext + "\".\n"

0 commit comments

Comments
 (0)