[PATCH] D22334: Fix for Bug 28172 : clang crashes on invalid code (with too few arguments to __builtin_signbit) without any proper diagnostics.

Mayur Pandey via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 17 22:47:27 PST 2016


mayurpandey updated this revision to Diff 78471.
mayurpandey added a comment.

Hi,

Updated the patch. Can you please commit it on my behalf as I don't have commit access.

Thanks,
Mayur


https://reviews.llvm.org/D22334

Files:
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Sema/SemaChecking.cpp
  test/Sema/builtins.c
  test/SemaCXX/builtins.cpp


Index: test/SemaCXX/builtins.cpp
===================================================================
--- test/SemaCXX/builtins.cpp
+++ test/SemaCXX/builtins.cpp
@@ -44,3 +44,10 @@
   __noop(1); // expected-error {{use of undeclared}}
   __debugbreak(); // expected-error {{use of undeclared}}
 }
+
+template <typename T>
+int test_signbit(T t) { return __builtin_signbit(t); } // expected-error {{floating point type is required}}
+
+int test_signbit_call () {
+return test_signbit("1"); // expected-note {{instantiation of function template specialization}} 
+}
Index: test/Sema/builtins.c
===================================================================
--- test/Sema/builtins.c
+++ test/Sema/builtins.c
@@ -248,3 +248,11 @@
 
     return buf;
 }
+
+int test21(double a) {
+  return __builtin_signbit();  // expected-error {{too few arguments}}
+}
+
+int test22(void) {
+  return __builtin_signbit("1");  // expected-error {{floating point type is required}}
+}
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -98,6 +98,22 @@
   return false;
 }
 
+static bool SemaBuiltinSignbit(Sema &S, CallExpr *TheCall) {
+  if (checkArgCount(S, TheCall, 1))
+    return true;
+
+  // Argument should be an float, double or long double.
+  Expr *ValArg = TheCall->getArg(0);
+  QualType Ty = ValArg->getType();
+  if (!Ty->isRealFloatingType()) {
+    S.Diag(ValArg->getLocStart(), diag::err_typecheck_cond_expect_float)
+      << Ty << ValArg->getSourceRange();
+    return true;
+  }
+
+  return false;
+}
+
 /// Check that the argument to __builtin_addressof is a glvalue, and set the
 /// result type to the corresponding pointer type.
 static bool SemaBuiltinAddressof(Sema &S, CallExpr *TheCall) {
@@ -762,6 +778,10 @@
     }
     break;
   }
+  case Builtin::BI__builtin_signbit:
+    if (SemaBuiltinSignbit(*this, TheCall))
+      return ExprError();
+    break;
   case Builtin::BI__builtin_isgreater:
   case Builtin::BI__builtin_isgreaterequal:
   case Builtin::BI__builtin_isless:
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -6845,6 +6845,8 @@
   "(passed in %0)">;
 def err_typecheck_cond_expect_int_float : Error<
   "used type %0 where integer or floating point type is required">;
+def err_typecheck_cond_expect_float : Error<
+  "used type %0 where floating point type is required">;
 def err_typecheck_cond_expect_scalar : Error<
   "used type %0 where arithmetic or pointer type is required">;
 def err_typecheck_cond_expect_nonfloat : Error<


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22334.78471.patch
Type: text/x-patch
Size: 2742 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161118/0c57ff67/attachment-0001.bin>


More information about the cfe-commits mailing list