Fix unsafe access of component metadata (change ticks) in bevy_ecs::storage::Table#23065
Fix unsafe access of component metadata (change ticks) in bevy_ecs::storage::Table#23065nils-mathieu wants to merge 3 commits intobevyengine:mainfrom
bevy_ecs::storage::Table#23065Conversation
|
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
|
Not sure why the macos build times out. Is that an issue on the runner's end? |
kfc35
left a comment
There was a problem hiding this comment.
Nice work! Appreciate the easy-to-understand PR description. Thanks for your first contribution 🙏
mockersf
left a comment
There was a problem hiding this comment.
Could this have an impact on performances? Maybe not run the whole benchmarks, but could you check the code generated for costly differences?
I would naively expect the "success" case to have the same performance as it does the same things (check row index + access), and the "failure" case the have better performance because we're no longer performing the access at all in that case. I'm not sure how to check the generated code though. That being said, I suspect that the compiler was actually doing the following:
If it was doing that, then this would technically be a regression performance-wise? I would be surprised if it's even measurable |
Objective
This pull requests simply changes three functions in
bevy_ecs/src/storage/table/mod.rswhich currently may incorrectly access an out of bound element.Currently those functions use the following pattern to guard against invalid
TableRow:The usage of
then_someis invalid here because it takes its input by value, which evaluates theget_uncheckedcall instantly even ifrow.index_u32() < self.entity_count()isfalse.Calling
get_uncheckedwith an invalid index triggers instant undefined behavior even if the value is not used.Solution
We can't really use
theninstead ofthen_somebecauseself.get_columncan fail by returningNone, so I opted for a regularifguard like this:Testing
I tried to run the CI on the whole thing but my version of clippy (nightly) is triggered by other parts of the code and I was too lazy to install Rust stable and re-compile everything again. I will fix anything that fails in CI here and re-check everything locally if it turns out to fail.
Other Notes
I'm not familiar with the codebase (well, I've been reading an poking around for a bit but I don't know everything), so there might be other places in the code with the same issue. I grep'ed
then_someand found no other instance of the same problem, so at least it doesn't appear anywhere else in this form.