From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) by sourceware.org (Postfix) with ESMTPS id 1CB333857817 for ; Wed, 16 Jun 2021 01:48:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1CB333857817 Received: by mail-ot1-x32d.google.com with SMTP id i12-20020a05683033ecb02903346fa0f74dso880954otu.10 for ; Tue, 15 Jun 2021 18:48:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:message-id:date:user-agent :mime-version:content-language; bh=1XKXREBsCLwdNS41S0R5ZmuolOQoqzqTMxtIRAxBrHo=; b=Eehxxjr9JX9kJc6IZJllDHAEeR2QfwlJXAwLzN4afXUvF87wabcRnh3P9JYCG7XE+W V2mRJalW7mIVNtCN/ZErMkjqsK/Wn59P0ri1EKn4DKEn7YsDrTIjo6VmsQPcdCgNLRA5 GKb5UqJXmD1s+R19fL373/rR5pn3blbPUZxPO7r59Qjo8/8jz5k5dx1J1GZu73SyMS4b kPBOM6HUAw/LFCXXzPvIxoDo0XMHhYR3C48IL7Uhi3/yHksYiBHrpEpA0yG36rkSjwxe SdcGvBE4o4tznjjwvkfk5cC3SgukHFfdeeB6q2JVePwbiKWvLaAuGpi6YzhiN29tkfqN s5mA== X-Gm-Message-State: AOAM533Yky6HIIxFsffBIeUZ+Bt9RYU1Jm1pzDsuLS8ygKW4wGsGZTP9 PIMTvulnDH+Ykfg1grP2ceQ= X-Google-Smtp-Source: ABdhPJzpMATZCCYwWUUnyR+nHPrLNAAFYld1qDg4deFeLxrhgVffTxgIzpstUsuGcSnSUtrGXnvqPQ== X-Received: by 2002:a05:6830:1254:: with SMTP id s20mr1638493otp.265.1623808119508; Tue, 15 Jun 2021 18:48:39 -0700 (PDT) Received: from [192.168.0.41] (97-118-105-195.hlrn.qwest.net. [97.118.105.195]) by smtp.gmail.com with ESMTPSA id p5sm147358oip.35.2021.06.15.18.48.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 15 Jun 2021 18:48:38 -0700 (PDT) From: Martin Sebor Subject: [PATCH] make rich_location safe to copy To: gcc-patches , David Malcolm Message-ID: <88448f63-87ad-c3a5-d38b-c94dd825e8d2@gmail.com> Date: Tue, 15 Jun 2021 19:48:37 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------2DFCA44A60CF7FC44559B930" Content-Language: en-US X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Wed, 16 Jun 2021 01:48:41 -0000 This is a multi-part message in MIME format. --------------2DFCA44A60CF7FC44559B930 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit While debugging locations I noticed the semi_embedded_vec template in line-map.h doesn't declare a copy ctor or copy assignment, but is being copied in a couple of places in the C++ parser (via gcc_rich_location). It gets away with it most likely because it never grows beyond the embedded buffer. The attached patch defines the copy ctor and also copy assignment and adds the corresponding move functions. Tested on x86_64-linux. Martin --------------2DFCA44A60CF7FC44559B930 Content-Type: text/x-patch; charset=UTF-8; name="gcc-semi_embedded_vec.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="gcc-semi_embedded_vec.diff" libcpp/ChangeLog: * include/line-map.h (class semi_embedded_vec): Add copy ctor and assignment, move ctor and move assignment. diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h index 7d964172469..1d6fb0d6b00 100644 --- a/libcpp/include/line-map.h +++ b/libcpp/include/line-map.h @@ -1376,6 +1376,12 @@ class semi_embedded_vec semi_embedded_vec (); ~semi_embedded_vec (); + semi_embedded_vec (const semi_embedded_vec &); + semi_embedded_vec (semi_embedded_vec &&); + + semi_embedded_vec& operator= (const semi_embedded_vec &); + semi_embedded_vec& operator= (semi_embedded_vec &&); + unsigned int count () const { return m_num; } T& operator[] (int idx); const T& operator[] (int idx) const; @@ -1395,8 +1401,89 @@ class semi_embedded_vec template semi_embedded_vec::semi_embedded_vec () -: m_num (0), m_alloc (0), m_extra (NULL) + : m_num (0), m_alloc (0), m_extra (NULL) +{ +} + +/* Copy ctor. */ + +template +semi_embedded_vec:: +semi_embedded_vec (const semi_embedded_vec &rhs) + : m_num (rhs.m_num), m_alloc (rhs.m_alloc) { + int nemb = rhs.m_num < NUM_EMBEDDED ? rhs.m_num : NUM_EMBEDDED; + memcpy (m_embedded, rhs.m_embedded, nemb * sizeof (T)); + + if (!m_extra) + { + m_extra = NULL; + return; + } + + m_extra = XNEWVEC (T, m_alloc); + memcpy (m_extra, rhs.m_extra, m_alloc * sizeof (T)); +} + +/* Move ctor. */ + +template +semi_embedded_vec:: +semi_embedded_vec (semi_embedded_vec &&rhs) + : m_num (rhs.m_num), m_alloc (rhs.m_alloc) +{ + int nemb = rhs.m_num < NUM_EMBEDDED ? rhs.m_num : NUM_EMBEDDED; + memcpy (m_embedded, rhs.m_embedded, nemb * sizeof (T)); + + m_extra = rhs.m_extra; + rhs.m_extra = NULL; +} + +/* Copy assignment. */ + +template +semi_embedded_vec& +semi_embedded_vec::operator= (const semi_embedded_vec &rhs) +{ + int nemb = rhs.m_num < NUM_EMBEDDED ? rhs.m_num : NUM_EMBEDDED; + memcpy (m_embedded, rhs.m_embedded, nemb * sizeof (T)); + + m_num = rhs.m_num; + + if (!rhs.m_extra) + /* Don't release already allocated memory. */ + return *this; + + if (m_alloc < rhs.m_alloc) + { + m_extra = XRESIZEVEC (T, m_extra, rhs.m_alloc); + m_alloc = rhs.m_alloc; + } + + memcpy (m_extra, rhs.m_extra, m_alloc * sizeof (T)); + return *this; +} + +/* Move assignment. */ + +template +semi_embedded_vec& +semi_embedded_vec::operator= (semi_embedded_vec &&rhs) +{ + int nemb = rhs.m_num < NUM_EMBEDDED ? rhs.m_num : NUM_EMBEDDED; + memcpy (m_embedded, rhs.m_embedded, nemb * sizeof (T)); + + m_num = rhs.m_num; + + if (!rhs.m_extra) + /* Don't release already allocated memory. */ + return *this; + + m_extra = rhs.m_extra; + m_alloc = rhs.m_alloc; + + rhs.m_extra = NULL; + return *this; } /* semi_embedded_vec's dtor. Release any dynamically-allocated memory. */ --------------2DFCA44A60CF7FC44559B930--