sanitizer: do not ICE for pointer cmp/sub
authorMartin Liska <mliska@suse.cz>
Wed, 9 Dec 2020 15:21:18 +0000 (16:21 +0100)
committerMartin Liska <mliska@suse.cz>
Mon, 14 Dec 2020 09:49:27 +0000 (10:49 +0100)
gcc/c/ChangeLog:

PR sanitizer/98204
* c-typeck.c (pointer_diff): Do not emit a top-level
sanitization.
(build_binary_op): Likewise.

gcc/testsuite/ChangeLog:

PR sanitizer/98204
* c-c++-common/asan/pr98204.c: New test.

gcc/c/c-typeck.c
gcc/testsuite/c-c++-common/asan/pr98204.c [new file with mode: 0644]

index 138af07392554b80c1ef5729f6cf3f44eebb8ce4..7d58e8de342b03d5d8d72187b96bd86d70a6880c 100644 (file)
@@ -3949,10 +3949,9 @@ pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
     pedwarn (loc, OPT_Wpointer_arith,
             "pointer to a function used in subtraction");
 
-  if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
+  if (current_function_decl != NULL_TREE
+      && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
     {
-      gcc_assert (current_function_decl != NULL_TREE);
-
       op0 = save_expr (op0);
       op1 = save_expr (op1);
 
@@ -12324,6 +12323,7 @@ build_binary_op (location_t location, enum tree_code code,
        }
 
       if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
+         && current_function_decl != NULL_TREE
          && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
        {
          op0 = save_expr (op0);
diff --git a/gcc/testsuite/c-c++-common/asan/pr98204.c b/gcc/testsuite/c-c++-common/asan/pr98204.c
new file mode 100644 (file)
index 0000000..7532646
--- /dev/null
@@ -0,0 +1,6 @@
+/* PR sanitizer/98204 */
+/* { dg-options "-fsanitize=address,pointer-subtract,pointer-compare" } */
+
+struct{int c;}v;
+static long i=((char*)&(v.c)-(char*)&v);
+static long i2=((char*)&(v.c)<(char*)&v);