diff options
author | Håvard Pettersen <havardpe@yahooinc.com> | 2022-05-24 14:11:12 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@yahooinc.com> | 2022-05-24 14:11:12 +0000 |
commit | 46ac2a3b45e2df26e24f0ee00ec4334d5de2941a (patch) | |
tree | 3145cd15761dd5465a8ddb7c5531fc3b1df45e75 /searchlib | |
parent | 5a7992bf420c867d78e1ad6873993dd013c453c8 (diff) |
try to avoid some int64 wrap-arounds (the simple ones)
also avoid nullptr deref
Diffstat (limited to 'searchlib')
4 files changed, 26 insertions, 23 deletions
diff --git a/searchlib/src/vespa/searchlib/predicate/predicate_hash.h b/searchlib/src/vespa/searchlib/predicate/predicate_hash.h index 9000d8d509b..39ffe57745e 100644 --- a/searchlib/src/vespa/searchlib/predicate/predicate_hash.h +++ b/searchlib/src/vespa/searchlib/predicate/predicate_hash.h @@ -14,7 +14,7 @@ struct PredicateHash { } static uint64_t hash64(const void *data, uint32_t origLen) { - int64_t a, b, c; + uint64_t a, b, c; int offset; // Current offset into the entire key. const uint8_t *aKey = static_cast<const uint8_t *>(data); @@ -54,18 +54,18 @@ struct PredicateHash { ((0xffLL & aKey[offset+23])<<56)); // Mix. This arithmetic must match the mix below. - a -= b; a -= c; a ^= (((uint64_t) c)>>43); + a -= b; a -= c; a ^= (c>>43); b -= c; b -= a; b ^= (a<<9); - c -= a; c -= b; c ^= (((uint64_t) b)>>8); - a -= b; a -= c; a ^= (((uint64_t) c)>>38); + c -= a; c -= b; c ^= (b>>8); + a -= b; a -= c; a ^= (c>>38); b -= c; b -= a; b ^= (a<<23); - c -= a; c -= b; c ^= (((uint64_t) b)>>5); - a -= b; a -= c; a ^= (((uint64_t) c)>>35); + c -= a; c -= b; c ^= (b>>5); + a -= b; a -= c; a ^= (c>>35); b -= c; b -= a; b ^= (a<<49); - c -= a; c -= b; c ^= (((uint64_t) b)>>11); - a -= b; a -= c; a ^= (((uint64_t) c)>>12); + c -= a; c -= b; c ^= (b>>11); + a -= b; a -= c; a ^= (c>>12); b -= c; b -= a; b ^= (a<<18); - c -= a; c -= b; c ^= (((uint64_t) b)>>22); + c -= a; c -= b; c ^= (b>>22); // End mix. offset += 24; len -= 24; @@ -102,21 +102,21 @@ struct PredicateHash { } // Mix. This arithmetic must match the mix above. - a -= b; a -= c; a ^= (((uint64_t) c)>>43); + a -= b; a -= c; a ^= (c>>43); b -= c; b -= a; b ^= (a<<9); - c -= a; c -= b; c ^= (((uint64_t) b)>>8); - a -= b; a -= c; a ^= (((uint64_t) c)>>38); + c -= a; c -= b; c ^= (b>>8); + a -= b; a -= c; a ^= (c>>38); b -= c; b -= a; b ^= (a<<23); - c -= a; c -= b; c ^= (((uint64_t) b)>>5); - a -= b; a -= c; a ^= (((uint64_t) c)>>35); + c -= a; c -= b; c ^= (b>>5); + a -= b; a -= c; a ^= (c>>35); b -= c; b -= a; b ^= (a<<49); - c -= a; c -= b; c ^= (((uint64_t) b)>>11); - a -= b; a -= c; a ^= (((uint64_t) c)>>12); + c -= a; c -= b; c ^= (b>>11); + a -= b; a -= c; a ^= (c>>12); b -= c; b -= a; b ^= (a<<18); - c -= a; c -= b; c ^= (((uint64_t) b)>>22); + c -= a; c -= b; c ^= (b>>22); // End mix. - return static_cast<uint64_t>(c); + return c; } }; diff --git a/searchlib/src/vespa/searchlib/predicate/predicate_range_expander.h b/searchlib/src/vespa/searchlib/predicate/predicate_range_expander.h index 99d699b5bf3..79417f51f82 100644 --- a/searchlib/src/vespa/searchlib/predicate/predicate_range_expander.h +++ b/searchlib/src/vespa/searchlib/predicate/predicate_range_expander.h @@ -103,10 +103,10 @@ public: if (to < 0) { // Special case for to==-1. -X-0 means the same as -X-1, // but is more efficient. - partitionRange(label, (to == -1 ? 0 : -to), -from, arity, + partitionRange(label, (to == -1 ? 0 : -to), uint64_t(0)-from, arity, true, out); } else { - partitionRange(label, 0, -from, arity, true, out); + partitionRange(label, 0, uint64_t(0)-from, arity, true, out); partitionRange(label, 0, to, arity, false, out); } } else { diff --git a/searchlib/src/vespa/searchlib/predicate/predicate_range_term_expander.h b/searchlib/src/vespa/searchlib/predicate/predicate_range_term_expander.h index e0386dc4b44..90792912e64 100644 --- a/searchlib/src/vespa/searchlib/predicate/predicate_range_term_expander.h +++ b/searchlib/src/vespa/searchlib/predicate/predicate_range_term_expander.h @@ -30,7 +30,7 @@ public: _upper_bound(upper_bound) { uint64_t t = _upper_bound; while ((t /= _arity) > 0) ++_max_positive_levels; - t = -_lower_bound; + t = uint64_t(0)-_lower_bound; while ((t /= _arity) > 0) ++_max_negative_levels; } @@ -55,7 +55,7 @@ void PredicateRangeTermExpander::expand(const vespalib::string &key, int64_t sig uint64_t value; int max_levels; if (negative) { - value = -signed_value; + value = uint64_t(0)-signed_value; buffer[prefix_size++] = '-'; max_levels = _max_negative_levels; } else { @@ -72,7 +72,7 @@ void PredicateRangeTermExpander::expand(const vespalib::string &key, int64_t sig for (int i = 0; i < max_levels; ++i) { uint64_t start = (value / level_size) * level_size; if (negative) { - if (start + level_size - 1 > uint64_t(-LLONG_MIN)) { + if (start + level_size - 1 > (uint64_t(0)-LLONG_MIN)) { break; } size = sprintf(buffer + prefix_size, "%" PRIu64 "-%" PRIu64, diff --git a/searchlib/src/vespa/searchlib/predicate/tree_crumbs.h b/searchlib/src/vespa/searchlib/predicate/tree_crumbs.h index 41c41d317bb..a0a8ac6704f 100644 --- a/searchlib/src/vespa/searchlib/predicate/tree_crumbs.h +++ b/searchlib/src/vespa/searchlib/predicate/tree_crumbs.h @@ -34,6 +34,9 @@ public: size_t size() const { return _buffer.size(); } std::string getCrumb() const { + if (_buffer.empty()) { + return std::string(); + } return std::string(&_buffer[0], _buffer.size()); } }; |