This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. Closed by commit rGaaaece65a80f: [clang][ExprConst] Handle 0 type size in builtin_memcpy etc. (authored by tbaeder).
Changed prior to commit: https://reviews.llvm.org/D157252?vs=557826&id=557859#toc Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D157252/new/ https://reviews.llvm.org/D157252 Files: clang/lib/AST/ExprConstant.cpp clang/test/Sema/builtin-memcpy.c Index: clang/test/Sema/builtin-memcpy.c =================================================================== --- /dev/null +++ clang/test/Sema/builtin-memcpy.c @@ -0,0 +1,22 @@ +// RUN: %clang_cc1 %s -fsyntax-only -verify=c +// RUN: %clang_cc1 -x c++ %s -fsyntax-only -verify=cxx + +// cxx-no-diagnostics + + +/// Zero-sized structs should not crash. +int b() { + struct { } a[10]; + __builtin_memcpy(&a[2], a, 2); // c-warning {{buffer has size 0, but size argument is 2}} + return 0; +} + +#ifdef __cplusplus +// FIXME: This is UB and GCC correctly diagnoses it. Clang should do the same. +constexpr int b2() { + struct { } a[10]; + __builtin_memcpy(&a[2], a, 2); + return 0; +} +static_assert(b2() == 0, ""); +#endif Index: clang/lib/AST/ExprConstant.cpp =================================================================== --- clang/lib/AST/ExprConstant.cpp +++ clang/lib/AST/ExprConstant.cpp @@ -9545,6 +9545,8 @@ // Figure out how many T's we're copying. uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity(); + if (TSize == 0) + return false; if (!WChar) { uint64_t Remainder; llvm::APInt OrigN = N;
Index: clang/test/Sema/builtin-memcpy.c =================================================================== --- /dev/null +++ clang/test/Sema/builtin-memcpy.c @@ -0,0 +1,22 @@ +// RUN: %clang_cc1 %s -fsyntax-only -verify=c +// RUN: %clang_cc1 -x c++ %s -fsyntax-only -verify=cxx + +// cxx-no-diagnostics + + +/// Zero-sized structs should not crash. +int b() { + struct { } a[10]; + __builtin_memcpy(&a[2], a, 2); // c-warning {{buffer has size 0, but size argument is 2}} + return 0; +} + +#ifdef __cplusplus +// FIXME: This is UB and GCC correctly diagnoses it. Clang should do the same. +constexpr int b2() { + struct { } a[10]; + __builtin_memcpy(&a[2], a, 2); + return 0; +} +static_assert(b2() == 0, ""); +#endif Index: clang/lib/AST/ExprConstant.cpp =================================================================== --- clang/lib/AST/ExprConstant.cpp +++ clang/lib/AST/ExprConstant.cpp @@ -9545,6 +9545,8 @@ // Figure out how many T's we're copying. uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity(); + if (TSize == 0) + return false; if (!WChar) { uint64_t Remainder; llvm::APInt OrigN = N;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits