Skip to content

Commit

Permalink
Merge pull request #1314 from xzyfer/fix/1283
Browse files Browse the repository at this point in the history
Fix hash collisions with complex values
  • Loading branch information
xzyfer committed Jul 8, 2015
2 parents f45e14b + a6d3c61 commit 27e9980
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
25 changes: 19 additions & 6 deletions ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@
namespace Sass {
using namespace std;

// from boost (functional/hash):
// http://www.boost.org/doc/libs/1_35_0/doc/html/hash/combine.html
// Boost Software License - Version 1.0
// http://www.boost.org/users/license.html
template <typename T>
void hash_combine (std::size_t& seed, const T& val)
{
seed ^= std::hash<T>()(val) + 0x9e3779b9
+ (seed<<6) + (seed>>2);
}

//////////////////////////////////////////////////////////
// Abstract base class for all abstract syntax tree nodes.
//////////////////////////////////////////////////////////
Expand Down Expand Up @@ -137,7 +148,7 @@ namespace std {
{
bool operator()( Sass::Expression* lhs, Sass::Expression* rhs) const
{
return *lhs == *rhs;
return lhs->hash() == rhs->hash();
}
};
}
Expand Down Expand Up @@ -768,7 +779,7 @@ namespace Sass {
hash_ = std::hash<string>()(separator() == COMMA ? "comma" : "space");

for (size_t i = 0, L = length(); i < L; ++i)
hash_ ^= (elements()[i])->hash();
hash_combine(hash_, (elements()[i])->hash());

return hash_;
}
Expand Down Expand Up @@ -819,8 +830,10 @@ namespace Sass {
{
if (hash_ > 0) return hash_;

for (auto key : keys())
hash_ ^= key->hash() ^ at(key)->hash();
for (auto key : keys()) {
hash_combine(hash_, key->hash());
hash_combine(hash_, at(key)->hash());
}

return hash_;
}
Expand Down Expand Up @@ -1085,7 +1098,7 @@ namespace Sass {

hash_ = std::hash<string>()(name());
for (auto argument : arguments()->elements())
hash_ ^= argument->hash();
hash_combine(hash_, argument->hash());

return hash_;
}
Expand Down Expand Up @@ -1347,7 +1360,7 @@ namespace Sass {
if (hash_ > 0) return hash_;

for (auto string : elements())
hash_ ^= string->hash();
hash_combine(hash_, string->hash());

return hash_;
}
Expand Down
1 change: 1 addition & 0 deletions debugger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ inline void debug_ast(AST_Node* node, string ind = "", Env* env = 0)
" [delayed: " << expression->is_delayed() << "] " <<
" [interpolant: " << expression->is_interpolant() << "] " <<
" [arglist: " << expression->is_arglist() << "] " <<
" [hash: " << expression->hash() << "] " <<
endl;
for(auto i : expression->elements()) { debug_ast(i, ind + " ", env); }
} else if (dynamic_cast<Content*>(node)) {
Expand Down

0 comments on commit 27e9980

Please sign in to comment.