From patchwork Tue Mar 12 08:30:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1910889 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=GGYoFHtt; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tv6Kd4Y5xz1yWt for ; Tue, 12 Mar 2024 19:30:45 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2DC4D3858011 for ; Tue, 12 Mar 2024 08:30:43 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id AC8E2385829A for ; Tue, 12 Mar 2024 08:30:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AC8E2385829A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AC8E2385829A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710232217; cv=none; b=BrQ6QxB2FaoaHEIdYHGUu65MYUI9EQrx8iSnTxTR77rCLxcPfOxG7+WvhNAFBPdxe4p2aASi4+NZNvhIK/neT+/H3+xLpFVWV5S80fCQx3IkNfAaG3UmnGZCe6C0xrwjAWdPBmzjvVtBkqFqbrewtPOwgC2zJvABT4QVrmNyaBc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710232217; c=relaxed/simple; bh=eRxTFZYDRomNsldrOdCzSw1uhy2KvLvuq0d5bppwksw=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=ptvdyGrFb5Z8EUxntZrLXApKRg9jP9sLCiXDAf3mgDmnUazjvs1a4rL7/OoXFKVu4p1EYvBpwVRNZDYa9mI+uctq7sNVnf8tw6RAbz94rEuABC4bJc+tiLoU0JTS5q7n+szkwIdZ0zEryJ2hggLLwKoUzkcVnlnKTgyNTCX63BU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710232214; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=fBw1FF0Ayja3Xqb8+dj40t2/shMTeNnJGvkujWW8TJQ=; b=GGYoFHttSKmWktNwSDWt4gS6sw3Q88zQgVljitYQFC57iIOWizM9OhAYibFIvqh4LIngyT Du/5+TacSkZNDUx194avoHpXybrDUI3JH1JGQkMs/Vh+k/QzF4V6PXR9fti9vCDWgwGnht D0FF0gfvecIVU9KA1am/Rk2qUuzSkiw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-370-A7Z1SybSPfy-bV3MQoQL2A-1; Tue, 12 Mar 2024 04:30:11 -0400 X-MC-Unique: A7Z1SybSPfy-bV3MQoQL2A-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C099A101A58F; Tue, 12 Mar 2024 08:30:10 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.225.36]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 85D2DC04223; Tue, 12 Mar 2024 08:30:10 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 42C8U9iq3184382 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 12 Mar 2024 09:30:09 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 42C8U8g03184381; Tue, 12 Mar 2024 09:30:08 +0100 Date: Tue, 12 Mar 2024 09:30:08 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] strlen: Fix another spot that can create invalid ranges [PR114293] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! This PR is similar to PR110603 fixed with r14-8487, except in a different spot. From the memset with -1 size of non-zero value we determine minimum of (size_t) -1 and the code uses PTRDIFF_MAX - 2 (not really sure I understand why it is - 2 and not - 1, e.g. heap allocated array with PTRDIFF_MAX char elements which contain '\0' in the last element should be fine, no? One can still represent arr[PTRDIFF_MAX] - arr[0] and arr[0] - arr[PTRDIFF_MAX] in ptrdiff_t and strlen (arr) == PTRDIFF_MAX - 1) as the maximum, so again invalid range. As in the other case, it is just UB that can lead to that, and we have choice to only keep the min and use +inf for max, or only keep max and use 0 for min, or not set the range at all, or use [min, min] or [max, max] etc. The following patch uses [min, +inf]. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-03-12 Jakub Jelinek PR tree-optimization/114293 * tree-ssa-strlen.cc (strlen_pass::handle_builtin_strlen): If max is smaller than min, set max to ~(size_t)0. * gcc.dg/pr114293.c: New test. Jakub --- gcc/tree-ssa-strlen.cc.jj 2024-01-30 09:57:58.361809262 +0100 +++ gcc/tree-ssa-strlen.cc 2024-03-11 10:35:04.305650520 +0100 @@ -2341,6 +2341,8 @@ strlen_pass::handle_builtin_strlen () wide_int min = wi::to_wide (old); wide_int max = wi::to_wide (TYPE_MAX_VALUE (ptrdiff_type_node)) - 2; + if (wi::gtu_p (min, max)) + max = wi::to_wide (TYPE_MAX_VALUE (TREE_TYPE (lhs))); set_strlen_range (lhs, min, max); } else --- gcc/testsuite/gcc.dg/pr114293.c.jj 2024-03-11 10:41:04.375708877 +0100 +++ gcc/testsuite/gcc.dg/pr114293.c 2024-03-11 10:40:50.355895196 +0100 @@ -0,0 +1,10 @@ +/* PR tree-optimization/114293 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -w" } */ + +int +foo (int x) +{ + __builtin_memset (&x, 5, -1); + return __builtin_strlen ((char *) &x); +}