This is an archive of the discontinued LLVM Phabricator instance.

[clang][Sema] Use size of char for void types
ClosedPublic

Authored by void on Oct 14 2022, 2:00 PM.

Details

Summary

The extension that allows for pointer arithmetic on 'void' types treats
the 'void' as a 'char'. We should use the 'char' size instead of one in
this case to allow warning when pointer arithmetic would go out of
bounds.

Diff Detail

Event Timeline

void created this revision.Oct 14 2022, 2:00 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 14 2022, 2:00 PM
void requested review of this revision.Oct 14 2022, 2:00 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 14 2022, 2:00 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
clang/test/Sema/array-bounds-ptr-arith.c
14

I'm fine with that change, but I don't understand how it relates to that commit ;-)

void added inline comments.Oct 14 2022, 2:12 PM
clang/test/Sema/array-bounds-ptr-arith.c
14

The original size 80 was large enough to emit a warning. However, 9 *didn't* emit a warning, but should. (Note that 8 won't emit a warning because of pointer arithmetic voodoo.)

clang/test/Sema/array-bounds-ptr-arith.c
14

It's slightly confusing, but the previous increment of 80 masked the issue with a smaller increment that was still out of bounds, IIUC. I think the previous code had a bytes vs bits bug?

void added inline comments.Oct 14 2022, 2:20 PM
clang/test/Sema/array-bounds-ptr-arith.c
14

Yes, that's pretty much what way happening. The comparison would be 80 (index) vs 64 (array length). But when the ptrarith_typesize is one, the comparison is 9 vs 64.

void updated this revision to Diff 467912.Oct 14 2022, 2:23 PM

Fix up testcase.

nickdesaulniers accepted this revision.Oct 14 2022, 2:31 PM

LGTM; please consider appending to the commit message something along the lines of "operations on the result of getTypeSize() are in bits, not bytes. Using 1 as the value for void* is the correct number of bytes, but we're doing arithmetic in bits." or something to denote this is a unit-of-measure related bug.

This revision is now accepted and ready to land.Oct 14 2022, 2:31 PM
void added a comment.Oct 14 2022, 2:46 PM

LGTM; please consider appending to the commit message something along the lines of "operations on the result of getTypeSize() are in bits, not bytes. Using 1 as the value for void* is the correct number of bytes, but we're doing arithmetic in bits." or something to denote this is a unit-of-measure related bug.

Done. Thanks!

This revision was landed with ongoing or failed builds.Oct 14 2022, 2:46 PM
This revision was automatically updated to reflect the committed changes.

Thanks for clarifying!