[PATCH] D116342: [clang][AST] Fix crash when printing error

Ellis Hoag via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 28 13:32:02 PST 2021


ellis created this revision.
ellis added a reviewer: dblaikie.
ellis published this revision for review.
ellis added inline comments.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.


================
Comment at: clang/lib/AST/TypePrinter.cpp:242
     case Type::ObjCObjectPointer:
       CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() ||
         T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType();
----------------
Hi @dblaikie, I'm not familiar with this code but I'm wondering if these uses of `T` should actually be `UnderlyingType`. Will this cause a crash similar to what I found?


Clang will crash if it tries to compile the following code. This commit
fixes it.

  $ cat foo.c
  void foo(_Nullable int *ptr) {
      __auto_type _Nonnull a = ptr;
  };
  $ clang foo.c -c -Wnullable-to-nonnull-conversion


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116342

Files:
  clang/lib/AST/TypePrinter.cpp
  clang/test/Sema/nullability.c


Index: clang/test/Sema/nullability.c
===================================================================
--- clang/test/Sema/nullability.c
+++ clang/test/Sema/nullability.c
@@ -125,6 +125,7 @@
   int *a = ptr; // okay
   _Nonnull int *b = ptr; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
   b = ptr; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
+  __auto_type _Nonnull c = ptr; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nullable _Nonnull'}}
 
   accepts_nonnull_1(ptr); // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
 }
Index: clang/lib/AST/TypePrinter.cpp
===================================================================
--- clang/lib/AST/TypePrinter.cpp
+++ clang/lib/AST/TypePrinter.cpp
@@ -280,7 +280,7 @@
     case Type::Attributed: {
       // We still want to print the address_space before the type if it is an
       // address_space attribute.
-      const auto *AttrTy = cast<AttributedType>(T);
+      const auto *AttrTy = cast<AttributedType>(UnderlyingType);
       CanPrefixQualifiers = AttrTy->getAttrKind() == attr::AddressSpace;
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116342.396426.patch
Type: text/x-patch
Size: 1389 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211228/06f2ec07/attachment.bin>


More information about the cfe-commits mailing list