analyze: memcpy rewrite improvements #1176
Merged
+209
−79
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This branch has several improvements to the rewriting of
memcpy
andmemset
:memcpy
onQuantity::Single
references (&T
/&mut T
, as opposed to&[T]
/&mut [T]
). The reference is automatically converted to a 1-element slice. Thecopy_from_slice
call will panic with an out-of-bounds error if the number of elements to copy is not 0 or 1.memcpy
onOption
references. If the source or destination pointer isNone
, then this will panic if the element count is nonzero, and otherwise will do nothing.mem::size_of::<T>()
instead of the original size ofT
for convertingmemcpy
/memset
byte counts to element counts.The
size_of
change is a bit subtle. In C, ifsizeof(struct foo) == 16
, it's legal tomemcpy
an array ofstruct foo
usingn * 16
as the byte length (orn * SIZE_OF_FOO
, whereSIZE_OF_FOO
is#define
'd to be 16) instead ofn * sizeof(struct foo)
. This presents a problem for us becausec2rust-analyze
may change the size offoo
when rewriting its pointer fields. After rewriting, then * 16
version computes a byte length based on the original size forstruct foo
, while then * sizeof(struct foo)
computes it using the rewritten size. For converting the byte length to an element count, we previously used the original size, which works for then * 16
version but not forn * sizeof(struct foo)
. This branch changes the element count calculation to divide by the rewritten size instead, which works forn * sizeof(struct foo)
but not forn * 16
. The hope is thatn * sizeof(struct foo)
is more common, since it's usually preferred in C for portability reasons.