void created this revision. void added reviewers: serge-sans-paille, kees, nickdesaulniers. Herald added a project: All. void requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
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. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D135989 Files: clang/lib/Sema/SemaChecking.cpp clang/test/Sema/array-bounds-ptr-arith.c Index: clang/test/Sema/array-bounds-ptr-arith.c =================================================================== --- clang/test/Sema/array-bounds-ptr-arith.c +++ clang/test/Sema/array-bounds-ptr-arith.c @@ -6,13 +6,12 @@ struct ext2_super_block{ unsigned char s_uuid[8]; // expected-note {{declared here}} }; -void* ext2_statfs (struct ext2_super_block *es,int a) -{ - return (void *)es->s_uuid + sizeof(int); // no-warning + +void* ext2_statfs (struct ext2_super_block *es,int a) { + return (void *)es->s_uuid + sizeof(int); // no-warning } -void* broken (struct ext2_super_block *es,int a) -{ - return (void *)es->s_uuid + 80; // expected-warning {{refers past the end of the array (that contains 8 elements)}} +void* broken (struct ext2_super_block *es,int a) { + return (void *)es->s_uuid + 9; // expected-warning {{the pointer incremented by 9 refers past the end of the array (that contains 8 elements)}} } // Test case reduced from PR11594 Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -16020,16 +16020,21 @@ llvm::APInt size = ArrayTy->getSize(); if (BaseType != EffectiveType) { - // Make sure we're comparing apples to apples when comparing index to size + // Make sure we're comparing apples to apples when comparing index to + // size. uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); uint64_t array_typesize = Context.getTypeSize(BaseType); - // Handle ptrarith_typesize being zero, such as when casting to void* - if (!ptrarith_typesize) ptrarith_typesize = 1; + + // Handle ptrarith_typesize being zero, such as when casting to void*. + if (!ptrarith_typesize) + ptrarith_typesize = Context.getCharWidth(); + if (ptrarith_typesize != array_typesize) { - // There's a cast to a different size type involved + // There's a cast to a different size type involved. uint64_t ratio = array_typesize / ptrarith_typesize; + // TODO: Be smarter about handling cases where array_typesize is not a - // multiple of ptrarith_typesize + // multiple of ptrarith_typesize. if (ptrarith_typesize * ratio == array_typesize) size *= llvm::APInt(size.getBitWidth(), ratio); }
Index: clang/test/Sema/array-bounds-ptr-arith.c =================================================================== --- clang/test/Sema/array-bounds-ptr-arith.c +++ clang/test/Sema/array-bounds-ptr-arith.c @@ -6,13 +6,12 @@ struct ext2_super_block{ unsigned char s_uuid[8]; // expected-note {{declared here}} }; -void* ext2_statfs (struct ext2_super_block *es,int a) -{ - return (void *)es->s_uuid + sizeof(int); // no-warning + +void* ext2_statfs (struct ext2_super_block *es,int a) { + return (void *)es->s_uuid + sizeof(int); // no-warning } -void* broken (struct ext2_super_block *es,int a) -{ - return (void *)es->s_uuid + 80; // expected-warning {{refers past the end of the array (that contains 8 elements)}} +void* broken (struct ext2_super_block *es,int a) { + return (void *)es->s_uuid + 9; // expected-warning {{the pointer incremented by 9 refers past the end of the array (that contains 8 elements)}} } // Test case reduced from PR11594 Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -16020,16 +16020,21 @@ llvm::APInt size = ArrayTy->getSize(); if (BaseType != EffectiveType) { - // Make sure we're comparing apples to apples when comparing index to size + // Make sure we're comparing apples to apples when comparing index to + // size. uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); uint64_t array_typesize = Context.getTypeSize(BaseType); - // Handle ptrarith_typesize being zero, such as when casting to void* - if (!ptrarith_typesize) ptrarith_typesize = 1; + + // Handle ptrarith_typesize being zero, such as when casting to void*. + if (!ptrarith_typesize) + ptrarith_typesize = Context.getCharWidth(); + if (ptrarith_typesize != array_typesize) { - // There's a cast to a different size type involved + // There's a cast to a different size type involved. uint64_t ratio = array_typesize / ptrarith_typesize; + // TODO: Be smarter about handling cases where array_typesize is not a - // multiple of ptrarith_typesize + // multiple of ptrarith_typesize. if (ptrarith_typesize * ratio == array_typesize) size *= llvm::APInt(size.getBitWidth(), ratio); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits