summaryrefslogtreecommitdiffstats
path: root/eval
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@yahooinc.com>2022-05-25 14:54:09 +0000
committerHåvard Pettersen <havardpe@yahooinc.com>2022-05-25 14:55:00 +0000
commit4f657a53503ef52c57de618087ebc6f8a22d0130 (patch)
tree29993d615dde4c9a22837e920bd9e491c7da01e4 /eval
parent5912287f8d1cb92e484d7f7578dbe18900fd8147 (diff)
fix undefined behavior in eval unit tests
Diffstat (limited to 'eval')
-rw-r--r--eval/src/tests/eval/compile_cache/compile_cache_test.cpp4
-rw-r--r--eval/src/tests/eval/compiled_function/compiled_function_test.cpp2
-rw-r--r--eval/src/tests/eval/gbdt/gbdt_test.cpp2
-rw-r--r--eval/src/vespa/eval/eval/array_array_map.h2
-rw-r--r--eval/src/vespa/eval/eval/fast_addr_map.h2
-rw-r--r--eval/src/vespa/eval/eval/fast_value.hpp4
-rw-r--r--eval/src/vespa/eval/eval/vm_forest.cpp16
7 files changed, 18 insertions, 14 deletions
diff --git a/eval/src/tests/eval/compile_cache/compile_cache_test.cpp b/eval/src/tests/eval/compile_cache/compile_cache_test.cpp
index c9d56de897c..2688dc7b4f5 100644
--- a/eval/src/tests/eval/compile_cache/compile_cache_test.cpp
+++ b/eval/src/tests/eval/compile_cache/compile_cache_test.cpp
@@ -270,9 +270,9 @@ struct CompileCheck : test::EvalSpec::EvalTest {
for (const Entry &entry: list) {
auto fun = entry.fun->get().get_function();
if (std::isnan(entry.expect)) {
- EXPECT_TRUE(std::isnan(fun(&entry.params[0])));
+ EXPECT_TRUE(std::isnan(fun(entry.params.data())));
} else {
- EXPECT_EQUAL(fun(&entry.params[0]), entry.expect);
+ EXPECT_EQUAL(fun(entry.params.data()), entry.expect);
}
}
}
diff --git a/eval/src/tests/eval/compiled_function/compiled_function_test.cpp b/eval/src/tests/eval/compiled_function/compiled_function_test.cpp
index 45e0c6d08fc..1d612826ab2 100644
--- a/eval/src/tests/eval/compiled_function/compiled_function_test.cpp
+++ b/eval/src/tests/eval/compiled_function/compiled_function_test.cpp
@@ -108,7 +108,7 @@ struct MyEvalTest : test::EvalSpec::EvalTest {
CompiledFunction cfun(*function, PassParams::ARRAY);
auto fun = cfun.get_function();
ASSERT_EQUAL(cfun.num_params(), param_values.size());
- double result = fun(&param_values[0]);
+ double result = fun(param_values.data());
if (is_same(expected_result, result)) {
print_pass && fprintf(stderr, "verifying: %s -> %g ... PASS\n",
as_string(param_names, param_values, expression).c_str(),
diff --git a/eval/src/tests/eval/gbdt/gbdt_test.cpp b/eval/src/tests/eval/gbdt/gbdt_test.cpp
index 09d4a081ac4..582bb484f0e 100644
--- a/eval/src/tests/eval/gbdt/gbdt_test.cpp
+++ b/eval/src/tests/eval/gbdt/gbdt_test.cpp
@@ -188,7 +188,7 @@ struct DummyForest2 : public Forest {
size_t num_trees;
explicit DummyForest2(size_t num_trees_in) : num_trees(num_trees_in) {}
static double eval(const Forest *forest, const double *) {
- const DummyForest1 &self = *((const DummyForest1 *)forest);
+ const DummyForest2 &self = *((const DummyForest2 *)forest);
return double(self.num_trees);
}
static Optimize::Result optimize(const ForestStats &stats,
diff --git a/eval/src/vespa/eval/eval/array_array_map.h b/eval/src/vespa/eval/eval/array_array_map.h
index e3601644f3e..4deb5e17a9f 100644
--- a/eval/src/vespa/eval/eval/array_array_map.h
+++ b/eval/src/vespa/eval/eval/array_array_map.h
@@ -42,7 +42,7 @@ public:
bool valid() const { return (id != npos()); }
};
- ConstArrayRef<K> get_keys(Tag tag) const { return {&_keys[tag.id * _keys_per_entry], _keys_per_entry}; }
+ ConstArrayRef<K> get_keys(Tag tag) const { return {_keys.data() + (tag.id * _keys_per_entry), _keys_per_entry}; }
ArrayRef<V> get_values(Tag tag) { return {&_values[tag.id * _values_per_entry], _values_per_entry}; }
ConstArrayRef<V> get_values(Tag tag) const { return {&_values[tag.id * _values_per_entry], _values_per_entry}; }
diff --git a/eval/src/vespa/eval/eval/fast_addr_map.h b/eval/src/vespa/eval/eval/fast_addr_map.h
index b9bc39ad619..3d6f272a252 100644
--- a/eval/src/vespa/eval/eval/fast_addr_map.h
+++ b/eval/src/vespa/eval/eval/fast_addr_map.h
@@ -65,7 +65,7 @@ public:
LabelView(size_t num_mapped_dims, const StringIdVector &labels_in)
: addr_size(num_mapped_dims), labels(labels_in) {}
ConstArrayRef<string_id> get_addr(size_t idx) const {
- return {&labels[idx * addr_size], addr_size};
+ return {labels.data() + (idx * addr_size), addr_size};
}
};
diff --git a/eval/src/vespa/eval/eval/fast_value.hpp b/eval/src/vespa/eval/eval/fast_value.hpp
index a40314430c7..42bf459ae3e 100644
--- a/eval/src/vespa/eval/eval/fast_value.hpp
+++ b/eval/src/vespa/eval/eval/fast_value.hpp
@@ -214,7 +214,9 @@ void
FastCells<T>::reallocate(size_t need) {
capacity = roundUp2inN(size + need);
alloc::Alloc new_memory = alloc::Alloc::alloc(elem_size * capacity);
- memcpy(new_memory.get(), memory.get(), elem_size * size);
+ if (memory.get()) {
+ memcpy(new_memory.get(), memory.get(), elem_size * size);
+ }
memory = std::move(new_memory);
}
diff --git a/eval/src/vespa/eval/eval/vm_forest.cpp b/eval/src/vespa/eval/eval/vm_forest.cpp
index 9b9e6ee1124..9a6f4730a5e 100644
--- a/eval/src/vespa/eval/eval/vm_forest.cpp
+++ b/eval/src/vespa/eval/eval/vm_forest.cpp
@@ -36,17 +36,19 @@ constexpr uint32_t INVERTED = 3;
// Note: We need to use double for set membership checks (IN) due to
// string hashing.
-const double *as_double_ptr(const uint32_t *pos) {
- return reinterpret_cast<const double*>(pos);
+double read_double(const uint32_t *pos) {
+ double value;
+ memcpy(&value, pos, sizeof(value));
+ return value;
}
const float *as_float_ptr(const uint32_t *pos) {
return reinterpret_cast<const float*>(pos);
}
-bool find_in(double value, const double *set, const double *end) {
- for (; set < end; ++set) {
- if (value == *set) {
+bool find_in(double value, const uint32_t *set, const uint32_t *end) {
+ for (; set < end; set += 2) {
+ if (value == read_double(set)) {
return true;
}
}
@@ -82,8 +84,8 @@ double general_find_leaf(const double *input, const uint32_t *pos, uint32_t node
return *as_float_ptr(pos);
}
} else if (node_type == IN) {
- if (find_in(input[pos[0] >> 12], as_double_ptr(pos + 2),
- as_double_ptr(pos + 2 + (2 * (pos[1] & 0xff)))))
+ if (find_in(input[pos[0] >> 12], pos + 2,
+ pos + 2 + (2 * (pos[1] & 0xff))))
{
node_type = (pos[0] & 0xf0) >> 4;
pos += 2 + (2 * (pos[1] & 0xff));