From patchwork Tue Sep 6 07:15:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 1020 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp505390wrt; Tue, 6 Sep 2022 00:23:39 -0700 (PDT) X-Google-Smtp-Source: AA6agR79mjsqt3oCMHM2ncWkB8EdC2T7Xr0JH7wHLiv3kUyFQc1nlqonou0muHPt3Lw4XCCONjbJ X-Received: by 2002:a17:907:b13:b0:73f:d86a:6e3c with SMTP id h19-20020a1709070b1300b0073fd86a6e3cmr33711383ejl.132.1662449019039; Tue, 06 Sep 2022 00:23:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662449019; cv=none; d=google.com; s=arc-20160816; b=mBk/dO3zAcwn4YxcXuX8A34C6j8Fet4+f/yBi/pCuk3vYZj9X2hW2IXxBIHk0f/dLA /wgZh6AcoaVeZu3JWQFBvrehKP0qY3yBlhlQzuqNUFZkD/LSp5Pdjbh6j4dt0z31mNsA /gtPI2hifb0eE0xA0MnVtbEtMw/u4Np5TQWUhsSqOmSCeLp39r6pi2twjuTh98XzdiLw 8RgV7uqre7+K9D4ECEfRVGAcls7BBDMsTp6n7OsUF9cztlWJg0UmHNLm6I+eWdU0Ah29 Xc53wV7eyHioXBzyh8iNzUboZJOg3NxrA0oS1eTSVS8IvYV1peks6rRCCMY8Knzx3xKP UkbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=X71zypNT5EIQGAxUqUjb7HFwN7kv8NX9I7qgMp2Q5LM=; b=SCUqR8jBuJMied0pO0kwHB65UU97cyaADGv8CuZs89Dp12Sr9Pw/X//oNSsJFKuxgT +AvIqD8gDa/mgkPOJHGgEt4kkMF7vQlsLYnCez85u4JIZHBKL7fjn8FKXl81r1KVLM/t TYACza/qBG218UOXtn3bAYXV89L8JWkbDN74ckY7efrdGW0MjLpxQ4zh/8XdrjqYyI7M F1JbgFO4Mmi+vzQiJEkoDCSbKZDZxA4HYayOTa5B5x45YyiJDJ9tnfsnLGzDa6UaKlKU GErA+FNncp8vdiW26L6KD+TqlKeGMsxvUFnXks5zAixfwTv8l1mJgNDsIgix6e85tk3z Iybg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=CP4kLchP; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id gt20-20020a1709072d9400b007314bba0e9bsi1665301ejc.353.2022.09.06.00.23.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Sep 2022 00:23:39 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=CP4kLchP; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 192CB3954C61 for ; Tue, 6 Sep 2022 07:19:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 192CB3954C61 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662448773; bh=X71zypNT5EIQGAxUqUjb7HFwN7kv8NX9I7qgMp2Q5LM=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=CP4kLchPirIXPQrErPPbMz+hyz3SczMhdGdGac5v6h20r1xQeil9Bijm/N/cRA2Zf VeI2hCfqONCz2FQQPUF/vOBCMtKAwlV54/9nrp0/XR10aVZqMTGOoJ0tR2u4rygB/R F7GTQxgjZcz+QNv6rEe9EGSa3rI31U0ZJarRl0lU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id 0CC3A3850841 for ; Tue, 6 Sep 2022 07:15:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0CC3A3850841 Received: by mail-wr1-x430.google.com with SMTP id bz13so10605989wrb.2 for ; Tue, 06 Sep 2022 00:15:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date; bh=X71zypNT5EIQGAxUqUjb7HFwN7kv8NX9I7qgMp2Q5LM=; b=zHdnpyRpmpAI1kTuCfZNPSAXHDkXfpFLd9ICa9wC3MWJy3tquCD/NTEUXSpVueu1gI LzOE5NF3eXfPYwTfiWun1MizeT24myy1XeXHOaOJItp1DcCCUIhR480OdS1LTdXGxQOl TOqkr3s1EEZj7v83irmBCUdDBIlWOvpY9el2KfdEFkPLAHUD5u9JgwUIqBp/V5xzJ+0R 2UQ3qa8iV36r+yShU2eeA2lT4zLxPAqsh59hX7M7KN0Ux6vJrdx4VH7yRSfX71aWKcBc wx4LRGtpkE7KBKDp7Bz/Z87s9R4S5BGxXAq+A/tKBWstAcSamgVaDZBhQQiRf3SupFtj OnwQ== X-Gm-Message-State: ACgBeo2LURY2UGk5dR8+nFbQ9W70dwnBH4KDtvO2siMGsF2bg8m/v6Eu dkkfvIcjgB7yciKU2tf1njk//oTWCFOE/g== X-Received: by 2002:a05:6000:1a87:b0:222:2c85:2f5b with SMTP id f7-20020a0560001a8700b002222c852f5bmr27021269wry.654.1662448556857; Tue, 06 Sep 2022 00:15:56 -0700 (PDT) Received: from poulhies-Precision-5550 (static-176-191-105-132.ftth.abo.bbox.fr. [176.191.105.132]) by smtp.gmail.com with ESMTPSA id n15-20020a05600c4f8f00b003adf0ae0048sm10008051wmq.45.2022.09.06.00.15.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Sep 2022 00:15:56 -0700 (PDT) Date: Tue, 6 Sep 2022 09:15:55 +0200 To: gcc-patches@gcc.gnu.org Subject: [Ada] Slice length computation bug in Generic_Bounded_Length generics Message-ID: <20220906071555.GA1280436@poulhies-Precision-5550> MIME-Version: 1.0 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, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: Marc =?iso-8859-1?q?Poulhi=E8s?= Cc: Steve Baird Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1743204142750785608?= X-GMAIL-MSGID: =?utf-8?q?1743204142750785608?= In some cases involving null slices, the Slice subprograms (both the function and the procedure) in each of the three Generic_Bounded_Length generic packages (for String, Wide_String, and Wide_Wide_String) could raise Constraint_Error in cases where this is incorrect. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * libgnat/a-strsup.adb, libgnat/a-stwisu.adb, libgnat/a-stzsup.adb (Super_Slice function and procedure): fix slice length computation. diff --git a/gcc/ada/libgnat/a-strsup.adb b/gcc/ada/libgnat/a-strsup.adb --- a/gcc/ada/libgnat/a-strsup.adb +++ b/gcc/ada/libgnat/a-strsup.adb @@ -1651,10 +1651,9 @@ package body Ada.Strings.Superbounded with SPARK_Mode is raise Index_Error; end if; - if High >= Low then - Result.Data (1 .. High - Low + 1) := Source.Data (Low .. High); - Result.Current_Length := High - Low + 1; - end if; + Result.Current_Length := (if Low > High then 0 else High - Low + 1); + Result.Data (1 .. Result.Current_Length) := + Source.Data (Low .. High); end return; end Super_Slice; @@ -1671,12 +1670,8 @@ package body Ada.Strings.Superbounded with SPARK_Mode is raise Index_Error; end if; - if High >= Low then - Target.Data (1 .. High - Low + 1) := Source.Data (Low .. High); - Target.Current_Length := High - Low + 1; - else - Target.Current_Length := 0; - end if; + Target.Current_Length := (if Low > High then 0 else High - Low + 1); + Target.Data (1 .. Target.Current_Length) := Source.Data (Low .. High); end Super_Slice; ---------------- diff --git a/gcc/ada/libgnat/a-stwisu.adb b/gcc/ada/libgnat/a-stwisu.adb --- a/gcc/ada/libgnat/a-stwisu.adb +++ b/gcc/ada/libgnat/a-stwisu.adb @@ -1497,7 +1497,7 @@ package body Ada.Strings.Wide_Superbounded is raise Index_Error; end if; - Result.Current_Length := High - Low + 1; + Result.Current_Length := (if Low > High then 0 else High - Low + 1); Result.Data (1 .. Result.Current_Length) := Source.Data (Low .. High); end return; end Super_Slice; @@ -1513,10 +1513,10 @@ package body Ada.Strings.Wide_Superbounded is or else High > Source.Current_Length then raise Index_Error; - else - Target.Current_Length := High - Low + 1; - Target.Data (1 .. Target.Current_Length) := Source.Data (Low .. High); end if; + + Target.Current_Length := (if Low > High then 0 else High - Low + 1); + Target.Data (1 .. Target.Current_Length) := Source.Data (Low .. High); end Super_Slice; ---------------- diff --git a/gcc/ada/libgnat/a-stzsup.adb b/gcc/ada/libgnat/a-stzsup.adb --- a/gcc/ada/libgnat/a-stzsup.adb +++ b/gcc/ada/libgnat/a-stzsup.adb @@ -1498,11 +1498,11 @@ package body Ada.Strings.Wide_Wide_Superbounded is or else High > Source.Current_Length then raise Index_Error; - else - Result.Current_Length := High - Low + 1; - Result.Data (1 .. Result.Current_Length) := - Source.Data (Low .. High); end if; + + Result.Current_Length := (if Low > High then 0 else High - Low + 1); + Result.Data (1 .. Result.Current_Length) := + Source.Data (Low .. High); end return; end Super_Slice; @@ -1517,10 +1517,10 @@ package body Ada.Strings.Wide_Wide_Superbounded is or else High > Source.Current_Length then raise Index_Error; - else - Target.Current_Length := High - Low + 1; - Target.Data (1 .. Target.Current_Length) := Source.Data (Low .. High); end if; + + Target.Current_Length := (if Low > High then 0 else High - Low + 1); + Target.Data (1 .. Target.Current_Length) := Source.Data (Low .. High); end Super_Slice; ----------------