From 4f7f62b6250b769540b1936e0d8b148272140639 Mon Sep 17 00:00:00 2001 From: David Roazen Date: Tue, 25 Nov 2014 14:59:12 -0500 Subject: [PATCH] VariantBuilderMultiSampleVector: add debug-mode bounds checking Bounds checking in production builds was deemed too expensive for VariantBuilderMultiSampleVector's set_sample_value(s) functions, so I've added these debug-mode-only asserts to catch out-of-bounds accesses during development. Resolves #390 --- gamgee/variant/variant_builder_multi_sample_vector.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gamgee/variant/variant_builder_multi_sample_vector.h b/gamgee/variant/variant_builder_multi_sample_vector.h index edf320c57..629efaeae 100644 --- a/gamgee/variant/variant_builder_multi_sample_vector.h +++ b/gamgee/variant/variant_builder_multi_sample_vector.h @@ -3,6 +3,7 @@ #include #include +#include namespace gamgee { @@ -78,10 +79,13 @@ class VariantBuilderMultiSampleVector { * @note MUCH more efficient than set_sample_values() below, since it doesn't require a vector * construction/destruction for each call. * - * @warning NO bounds checking is performed (for the sake of performance), so be sure that sample_index + * @warning Bounds checking is performed only in debug builds (for the sake of performance), so be sure that sample_index * is < num_samples and value_index < max_values_per_sample */ inline void set_sample_value(const uint32_t sample_index, const uint32_t value_index, const ELEMENT_TYPE value) { + assert(sample_index < m_num_samples); + assert(value_index < m_max_values_per_sample); + m_multi_sample_values[sample_index * m_max_values_per_sample + value_index] = value; } @@ -94,10 +98,13 @@ class VariantBuilderMultiSampleVector { * @note LESS efficient than setting one value at a time using set_sample_value(), since this function * involves creating/destroying a vector for each sample. * - * @warning NO bounds checking is performed (for the sake of performance), so be sure that sample_index + * @warning Bounds checking is performed only in debug builds (for the sake of performance), so be sure that sample_index * is < num_samples and values.size() <= max_values_per_sample */ inline void set_sample_values(const uint32_t sample_index, const std::vector& values) { + assert(sample_index < m_num_samples); + assert(values.size() <= m_max_values_per_sample); + const auto sample_start = sample_index * m_max_values_per_sample; for ( auto value_index = 0u; value_index < values.size(); ++value_index ) { m_multi_sample_values[sample_start + value_index] = values[value_index];