From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21594 invoked by alias); 29 Oct 2015 08:21:05 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 21582 invoked by uid 89); 29 Oct 2015 08:21:04 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00,KAM_ASCII_DIVIDERS,RP_MATCHES_RCVD,SPF_PASS autolearn=no version=3.3.2 X-HELO: mx2.suse.de Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (CAMELLIA256-SHA encrypted) ESMTPS; Thu, 29 Oct 2015 08:21:03 +0000 Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 70BB5AC0B for ; Thu, 29 Oct 2015 08:21:30 +0000 (UTC) Date: Thu, 29 Oct 2015 08:21:00 -0000 From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix PR56956 Message-ID: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-SW-Source: 2015-10/txt/msg03128.txt.bz2 This avoids introducing undefined overflow by not folding unsigned conditional negation to ABS_EXPR. IMHO we want a well-defined ABS_EXPR with unsigned result at some point, but that also needs target support (or auditing at least if we want to keep the existing abs expanders). Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to trunk. Richard. 2015-10-29 Richard Biener PR middle-end/56956 * fold-const.c (fold_cond_expr_with_comparison): Do not fold unsigned conditonal negation to ABS_EXPR. * c-c++-common/ubsan/pr56956.c: New testcase. Index: gcc/fold-const.c =================================================================== *** gcc/fold-const.c (revision 229481) --- gcc/fold-const.c (working copy) *************** fold_cond_expr_with_comparison (location *** 4993,5000 **** case GE_EXPR: case GT_EXPR: if (TYPE_UNSIGNED (TREE_TYPE (arg1))) ! arg1 = fold_convert_loc (loc, signed_type_for ! (TREE_TYPE (arg1)), arg1); tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1); return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem)); case UNLE_EXPR: --- 4973,4979 ---- case GE_EXPR: case GT_EXPR: if (TYPE_UNSIGNED (TREE_TYPE (arg1))) ! break; tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1); return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem)); case UNLE_EXPR: *************** fold_cond_expr_with_comparison (location *** 5004,5011 **** case LE_EXPR: case LT_EXPR: if (TYPE_UNSIGNED (TREE_TYPE (arg1))) ! arg1 = fold_convert_loc (loc, signed_type_for ! (TREE_TYPE (arg1)), arg1); tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1); return negate_expr (fold_convert_loc (loc, type, tem)); default: --- 4983,4989 ---- case LE_EXPR: case LT_EXPR: if (TYPE_UNSIGNED (TREE_TYPE (arg1))) ! break; tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1); return negate_expr (fold_convert_loc (loc, type, tem)); default: Index: gcc/testsuite/c-c++-common/ubsan/pr56956.c =================================================================== *** gcc/testsuite/c-c++-common/ubsan/pr56956.c (revision 0) --- gcc/testsuite/c-c++-common/ubsan/pr56956.c (working copy) *************** *** 0 **** --- 1,15 ---- + /* { dg-do run } */ + /* { dg-options "-fsanitize=undefined -fsanitize-undefined-trap-on-error" } */ + + unsigned int __attribute__((noinline,noclone)) + foo (unsigned int x) + { + return x <= __INT_MAX__ ? x : -x; + } + + int + main () + { + volatile unsigned int tem = foo (-__INT_MAX__ - 1); + return 0; + }