Skip to content

Commit 95ce9fb

Browse files
committed
[clang] Do not crash on pointer wchar_t pointer assignment.
wchar_t can be signed (thus hasSignedIntegerRepresentation() returns true), but it doesn't have an unsigned type, which would lead to a crash when trying to get it. With this fix, we special-case WideChar types in the pointer assignment code. Differential Revision: https://reviews.llvm.org/D91625
1 parent 412237d commit 95ce9fb

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎clang/lib/AST/ASTContext.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10007,6 +10007,11 @@ QualType ASTContext::getCorrespondingUnsignedType(QualType T) const {
1000710007
return UnsignedLongLongTy;
1000810008
case BuiltinType::Int128:
1000910009
return UnsignedInt128Ty;
10010+
// wchar_t is special. It is either signed or not, but when it's signed,
10011+
// there's no matching "unsigned wchar_t". Therefore we return the unsigned
10012+
// version of it's underlying type instead.
10013+
case BuiltinType::WChar_S:
10014+
return getUnsignedWCharType();
1001010015

1001110016
case BuiltinType::ShortAccum:
1001210017
return UnsignedShortAccumTy;

‎clang/test/SemaCXX/wchar_t.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %clang_cc1 -fsyntax-only -verify %s
2-
// RUN: %clang_cc1 -fsyntax-only -Wno-signed-unsigned-wchar -verify=allow-signed %s
2+
// RUN: %clang_cc1 -fsyntax-only -Wno-signed-unsigned-wchar -verify=allow-signed -DSKIP_ERROR_TESTS %s
33
// allow-signed-no-diagnostics
44
wchar_t x;
55

@@ -32,3 +32,10 @@ int t(void) {
3232
// rdar://8040728
3333
wchar_t in[] = L"\x434" "\x434"; // No warning
3434

35+
#ifndef SKIP_ERROR_TESTS
36+
// Verify that we do not crash when assigning wchar_t* to another pointer type.
37+
void assignment(wchar_t *x) {
38+
char *y;
39+
y = x; // expected-error {{incompatible pointer types assigning to 'char *' from 'wchar_t *'}}
40+
}
41+
#endif

0 commit comments

Comments
 (0)