@@ -135,20 +135,6 @@ static inline int check_header(uoffset_t end, uoffset_t base, uoffset_t offset)
135135 return k > base && k + offset_size <= end && !(k & (offset_size - 1 ));
136136}
137137
138- static inline int check_aligned_header (uoffset_t end , uoffset_t base , uoffset_t offset , uint16_t align )
139- {
140- uoffset_t k = base + offset ;
141-
142- if (uoffset_size <= voffset_size && k + offset_size < k ) {
143- return 0 ;
144- }
145- /* Alignment refers to element 0 and header must also be aligned. */
146- align = align < uoffset_size ? uoffset_size : align ;
147-
148- /* Note to self: the builder can also use the mask OR trick to propagate `min_align`. */
149- return k > base && k + offset_size <= end && !((k + offset_size ) & ((offset_size - 1 ) | (align - 1u )));
150- }
151-
152138static inline int verify_struct (uoffset_t end , uoffset_t base , uoffset_t offset , uoffset_t size , uint16_t align )
153139{
154140 /* Structs can have zero size so `end` is a valid value. */
@@ -276,22 +262,18 @@ static inline int verify_string(const void *buf, uoffset_t end, uoffset_t base,
276262 */
277263static inline int verify_vector (const void * buf , uoffset_t end , uoffset_t base , uoffset_t offset , uoffset_t elem_size , uint16_t align , uoffset_t max_count )
278264{
279- uoffset_t n ;
265+ verify (check_header (end , base , offset ), flatcc_verify_error_vector_header_out_of_range_or_unaligned );
266+ base += offset ;
267+
268+ uoffset_t n = read_uoffset (buf , base );
269+ base += offset_size ;
280270
281271#if FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS
282- base += offset ;
283- verify (end - base >= sizeof (n ), flatcc_verify_error_vector_header_out_of_range_or_unaligned );
284- n = read_uoffset (buf , base );
285- if (n == 0 ) {
286- return flatcc_verify_ok ;
287- }
288- verify (check_aligned_header (end , base - offset , offset , align ), flatcc_verify_error_vector_header_out_of_range_or_unaligned );
289- #else
290- verify (check_aligned_header (end , base , offset , align ), flatcc_verify_error_vector_header_out_of_range_or_unaligned );
291- base += offset ;
292- n = read_uoffset (buf , base );
272+ align = n == 0 ? uoffset_size : align ;
293273#endif
294- base += offset_size ;
274+ align = align < uoffset_size ? uoffset_size : align ;
275+ verify (!(base & (align - 1u )),flatcc_verify_error_vector_header_out_of_range_or_unaligned );
276+
295277 /* `n * elem_size` can overflow uncontrollably otherwise. */
296278 verify (n <= max_count , flatcc_verify_error_vector_count_exceeds_representable_vector_size );
297279 verify (end - base >= n * elem_size , flatcc_verify_error_vector_out_of_range );
0 commit comments