From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by sourceware.org (Postfix) with ESMTPS id CDAFC3858000 for ; Tue, 9 Nov 2021 09:45:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CDAFC3858000 Received: by mail-lf1-x129.google.com with SMTP id f3so42949527lfu.12 for ; Tue, 09 Nov 2021 01:45:57 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=IOn+He04OCsiZU+SA7QoVPZZoWnT7sC4fGC7zyzW7eo=; b=CimMJiJPx0AaxNabdTu792avBBhVTH7lPZN5TjV4hAxz7iWpix846vGO3Jv8UqXyhs SM4FwsmFE8U6JAWEPq831aG9mLgQscVLaS7G5625uDbdvwZkBU5phqGE67fsUd3i5/NI DzLeo7DUSvxygtFDsVIkBCpSNcp/6D8ZucBjV/5Bs334SeXx0JJeXje2cYlzZ3hAGHt+ f/0kvXO90Q1Wi59X9ryBKODe8pJ5M0ywJRKVbUMT5ih/Du0qygXe7gbdWF6k1zPqeQ8u xjBM2a5tI7REPvYdVWLdN0QlZc4rqseLH8ECVXrlWUMxSo0iZK3Sk/flPggJoHWL8JAN 4fMA== X-Gm-Message-State: AOAM531NKX/+40dvckYEtJt0i8k/sRknV3opWUPhX04qET4gLuL77dCt UjvbWXn2t2vhgq71dOl/mQbOWieIDFbQmg== X-Google-Smtp-Source: ABdhPJytDEP+CK0hZkBIVijZBdkT996CemiS7Xc9kBw/okUIt0IkczExiKVMdQEShlY0+/whRgrRdw== X-Received: by 2002:ac2:5e33:: with SMTP id o19mr5367689lfg.85.1636451156752; Tue, 09 Nov 2021 01:45:56 -0800 (PST) Received: from adacore.com ([2a02:2ab8:224:2ce:72b5:e8ff:feef:ee60]) by smtp.gmail.com with ESMTPSA id p21sm2089180lfg.18.2021.11.09.01.45.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Nov 2021 01:45:56 -0800 (PST) Date: Tue, 9 Nov 2021 09:45:54 +0000 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Piotr Trojanek Subject: [Ada] Reference in Unbounded_String is almost never null Message-ID: <20211109094554.GA830178@adacore.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="jRHKVT23PllUwdXP" Content-Disposition: inline X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Nov 2021 09:45:59 -0000 --jRHKVT23PllUwdXP Content-Type: text/plain; charset=us-ascii Content-Disposition: inline The underlying reference in Unbounded_String is almost never null, so recently it was changed to a non-excluding type (to avoid runtime checks that are almost never needed). The low-level routines that modify that reference had to be adapted, but only the Deallocate routine was adapted. This patch adapts the Realloc_For_Chunk routine as well. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * libgnat/a-strunb.adb (Deallocate): Rename Reference_Copy to Old, to make the code similar to other routines in this package. (Realloc_For_Chunk): Use a temporary, deallocate the previous string using a null-allowing copy of the string reference. --jRHKVT23PllUwdXP Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="patch.diff" diff --git a/gcc/ada/libgnat/a-strunb.adb b/gcc/ada/libgnat/a-strunb.adb --- a/gcc/ada/libgnat/a-strunb.adb +++ b/gcc/ada/libgnat/a-strunb.adb @@ -506,11 +506,11 @@ package body Ada.Strings.Unbounded is if Object.Reference /= Null_String'Access then declare - Reference_Copy : String_Access := Object.Reference; + Old : String_Access := Object.Reference; -- The original reference cannot be null, so we must create a -- copy which will become null when deallocated. begin - Deallocate (Reference_Copy); + Deallocate (Old); Object.Reference := Null_Unbounded_String.Reference; end; Object.Last := 0; @@ -833,9 +833,13 @@ package body Ada.Strings.Unbounded is Tmp : constant String_Access := new String (1 .. New_Rounded_Up_Size); + Old : String_Access := Source.Reference; + -- The original reference cannot be null, so we must create a copy + -- which will become null when deallocated. + begin Tmp (1 .. Source.Last) := Source.Reference (1 .. Source.Last); - Free (Source.Reference); + Free (Old); Source.Reference := Tmp; end; end if; --jRHKVT23PllUwdXP--