This is an archive of the discontinued LLVM Phabricator instance.

[Diagnostics] Silence -Wsizeof-array-div for character buffers
ClosedPublic

Authored by jrtc27 on Oct 4 2019, 4:26 PM.

Details

Summary

Character buffers are sometimes used to represent a pool of memory that
contains non-character objects, due to them being synonymous with a stream of
bytes on almost all modern architectures. Often, when interacting with hardware
devices, byte buffers are therefore used as an intermediary and so we can end
Character buffers are sometimes used to represent a pool of memory that
contains non-character objects, due to them being synonymous with a stream of
bytes on almost all modern architectures. Often, when interacting with hardware
devices, byte buffers are therefore used as an intermediary and so we can end
up generating lots of false-positives.

Moreover, due to the ability of character pointers to alias non-character
pointers, the strict aliasing violations that would generally be implied by the
calculations caught by the warning (if the calculation itself is in fact
correct) do not apply here, and so although the length calculation may be
wrong, that is the only possible issue.

Diff Detail

Event Timeline

jrtc27 created this revision.Oct 4 2019, 4:26 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 4 2019, 4:26 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
xbolva00 accepted this revision.Oct 4 2019, 5:52 PM

I think this is reasonable change.

This revision is now accepted and ready to land.Oct 4 2019, 5:52 PM
jrtc27 added a comment.Oct 6 2019, 5:56 AM

Having said that, the one warning this raised in FreeBSD (at least for our configuration) was a completely unnecessary use of a character buffer and could be cleaned up, so maybe this case is a useful one to catch. Does anyone else have an opinion on this?

There's not a single word in the patch's description.

jrtc27 added a comment.Oct 6 2019, 7:29 AM

There's not a single word in the patch's description.

If I were to add a description, it would be something like:

Character buffers are sometimes used to represent a pool of memory that
contains non-character objects, due to them being synonymous with a stream of
bytes on almost all modern architectures. Often, when interacting with hardware
devices, byte buffers are therefore used as an intermediary and so we can end
Character buffers are sometimes used to represent a pool of memory that
contains non-character objects, due to them being synonymous with a stream of
bytes on almost all modern architectures. Often, when interacting with hardware
devices, byte buffers are therefore used as an intermediary and so we can end
up generating lots of false-positives.

Moreover, due to the ability of character pointers to alias non-character
pointers, the strict aliasing violations that would generally be implied by the
calculations caught by the warning (if the calculation itself is in fact
correct) do not apply here, and so although the length calculation may be
wrong, that is the only possible issue.

But it seems that this first premise is actually wrong; at least in FreeBSD, and using a quick grep through Linux for obvious names (sizeof.*buf) /, sizeof.*buffer) / and sizeof.*data) /) I couldn't find one instance of an array where the types didn't match, but that was for u64 vs u32 so isn't even covered by this. Maybe it was true in the past, but these days people seem to be diligent about using the right types, with proper unions when needed.

The aliasing point still applies (in that this case is slightly less dangerous), but given it draws attention to code that may be able to be written more nicely with better types, I'd be inclined to abandon this. Unless people come screaming that we're spewing too many warnings for their code, that is, of course.

I think I saw some false positives with char buffers, so I am not against this patch personally.

@tkanis wrote in post commit review:

“What do you think about also not emitting the warning if the lhs sizeof is an array of signed or unsigned char? The warning wants the rhs sizeof to be sizeof(char) which is 1, and dividing by that doesn't really make sense. ”

I agree with him. I think you can land this patch.

thakis accepted this revision.Oct 7 2019, 11:27 AM
thakis added a subscriber: thakis.

Thanks, I think this is a good change. It fixes a false positive in wayland and one somewhere in Chromium's windows sandbox. See the commit thread of r371605 for examples.

jrtc27 edited the summary of this revision. (Show Details)Oct 7 2019, 7:48 PM
jrtc27 updated this revision to Diff 223823.Oct 8 2019, 4:22 AM

Rebased

This revision was automatically updated to reflect the committed changes.