Skip to content

Commit

Permalink
TOLERATE_MISALIGNED -> ENFORCE_ALIGNED
Browse files Browse the repository at this point in the history
  • Loading branch information
bkietz committed Aug 19, 2024
1 parent f86387b commit 1d4de83
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
13 changes: 7 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()


Expand Down
9 changes: 5 additions & 4 deletions include/flatcc/flatcc_rtconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/*
Expand Down
6 changes: 2 additions & 4 deletions src/runtime/verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1d4de83

Please sign in to comment.