Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bytes_terminate_multi(): rewrite to a less error-prone algorithm
After discovering that the existing implementation might index `src` out of bounds if `src` is shorter than `term`, I realized that the whole approach is prone to such errors and it would be wise to change the algorithm. It's not the first time I've discovered out-of-bounds indexing, the first time was in kaitai-io/kaitai_struct_java_runtime@deb426e. The main change is that the outer loop (now actually the only loop) ensures that we never index `src` out of bounds. Ensuring that we don't index `term` out of bounds is now a bit harder, but doable. We have to treat the empty `term` as a special case (since the loop assumes that `i_term = 0` is a valid index). We increment `i_term` only once each time and immediately check if it is equal to `unit_size` (`term.length()`). If so, we return from the function, so `term.length()` acts as as an exclusive upper bound for `i_term` and thus we don't get any out-of-bounds reads.
- Loading branch information