diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f97d231..d4127f59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,10 +78,11 @@ option (FLATCC_TRACE_VERIFY "assert on verify failure in runtime lib" OFF) # Some producers allow empty vectors to be misaligned. -# The following setting will cause the verifier to check for an -# empty vector before checking alignment of the vector's elements. -option (FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS - "don't fail verification if empty vectors are misaligned" OFF) +# The following setting will cause the verifier to require the index 0 +# position to be element aligned even if the vector is empty (otherwise that +# position is only required to be aligned to the preceding size field). +option (FLATCC_ENFORCE_ALIGNED_EMPTY_VECTORS + "verify includes full alignment check for empty vectors" OFF) # Reflection is the compilers ability to generate binary schema output # (.bfbs files). This requires using generated code from @@ -147,8 +148,8 @@ if (FLATCC_TRACE_VERIFY) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_TRACE_VERIFY=1") endif() -if (FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS=1") +if (FLATCC_ENFORCE_ALIGNED_EMPTY_VECTORS) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_ENFORCE_ALIGNED_EMPTY_VECTORS=1") endif() diff --git a/include/flatcc/flatcc_rtconfig.h b/include/flatcc/flatcc_rtconfig.h index 55a79024..32b80daa 100644 --- a/include/flatcc/flatcc_rtconfig.h +++ b/include/flatcc/flatcc_rtconfig.h @@ -65,11 +65,12 @@ extern "C" { /* * Some producers allow empty vectors to be misaligned. - * The following setting will cause the verifier to check for an - * empty vector before checking alignment of the vector's elements. + * The following setting will cause the verifier to require the index 0 + * position to be element aligned even if the vector is empty (otherwise that + * position is only required to be aligned to the preceding size field). */ -#if !defined(FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS) -#define FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS 0 +#if !defined(FLATCC_ENFORCE_ALIGNED_EMPTY_VECTORS) +#define FLATCC_ENFORCE_ALIGNED_EMPTY_VECTORS 0 #endif /* diff --git a/src/runtime/verifier.c b/src/runtime/verifier.c index 45ed8229..14003c06 100644 --- a/src/runtime/verifier.c +++ b/src/runtime/verifier.c @@ -270,13 +270,11 @@ static inline int verify_vector(const void *buf, uoffset_t end, uoffset_t base, n = read_uoffset(buf, base); base += offset_size; -#if FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS +#if !FLATCC_ENFORCE_ALIGNED_EMPTY_VECTORS /* This is due to incorrect buffers from other builders than cannot easily be ignored. */ align = n == 0 ? uoffset_size : align; #endif - align = align < uoffset_size ? uoffset_size : align; - verify(!(base & (align - 1u)),flatcc_verify_error_vector_header_out_of_range_or_unaligned); - + verify(!(base & ((align - 1u) | (uoffset_size - 1u))), flatcc_verify_error_vector_header_out_of_range_or_unaligned); /* `n * elem_size` can overflow uncontrollably otherwise. */ verify(n <= max_count, flatcc_verify_error_vector_count_exceeds_representable_vector_size); verify(end - base >= n * elem_size, flatcc_verify_error_vector_out_of_range);