c-family: ICE with -Wconversion and A ?: B [PR101030]
authorMarek Polacek <polacek@redhat.com>
Tue, 29 Mar 2022 18:36:55 +0000 (14:36 -0400)
committerMarek Polacek <polacek@redhat.com>
Wed, 30 Mar 2022 14:23:06 +0000 (10:23 -0400)
This patch fixes a crash in conversion_warning on a null expression.
It is null because the testcase uses the GNU A ?: B extension.  We
could also use op0 instead of op1 in this case, but it doesn't seem
to be necessary.

PR c++/101030

gcc/c-family/ChangeLog:

* c-warn.cc (conversion_warning) <case COND_EXPR>: Don't call
conversion_warning when OP1 is null.

gcc/testsuite/ChangeLog:

* g++.dg/ext/cond5.C: New test.

gcc/c-family/c-warn.cc
gcc/testsuite/g++.dg/ext/cond5.C [new file with mode: 0644]

index 9025fc1..f24ac5d 100644 (file)
@@ -1300,7 +1300,7 @@ conversion_warning (location_t loc, tree type, tree expr, tree result)
        tree op1 = TREE_OPERAND (expr, 1);
        tree op2 = TREE_OPERAND (expr, 2);
 
-       return (conversion_warning (loc, type, op1, result)
+       return ((op1 && conversion_warning (loc, type, op1, result))
                || conversion_warning (loc, type, op2, result));
       }
 
diff --git a/gcc/testsuite/g++.dg/ext/cond5.C b/gcc/testsuite/g++.dg/ext/cond5.C
new file mode 100644 (file)
index 0000000..a92f289
--- /dev/null
@@ -0,0 +1,13 @@
+// PR c++/101030
+// { dg-do compile { target { c++11 } } }
+// { dg-options "-Wconversion" }
+
+template <int N>
+struct jj {
+    int ii[N ?: 1];
+    char c = N ?: 1; // { dg-warning "conversion from .int. to .char. changes value from .300. to " }
+};
+
+int main() {
+    jj<300> kk;
+}