From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gproxy4-pub.mail.unifiedlayer.com (gproxy4-pub.mail.unifiedlayer.com [69.89.23.142]) by sourceware.org (Postfix) with ESMTPS id 744AB3858C3A for ; Mon, 14 Aug 2023 19:48:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 744AB3858C3A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw15.mail.unifiedlayer.com (unknown [10.0.90.130]) by progateway6.mail.pro1.eigbox.com (Postfix) with ESMTP id C9F4810047D68 for ; Mon, 14 Aug 2023 19:48:16 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id VdY0qkikvC5NcVdY0qvCt5; Mon, 14 Aug 2023 19:48:16 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=cqleL30i c=1 sm=1 tr=0 ts=64da8500 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=OWjo9vPv0XrRhIrVQ50Ab3nP57M=:19 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=UttIx32zK-AA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=_vKxW_w4nxET39AsqekA:9 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=aZdraInRrRo6tL8CVTWPwNboGswinh8wJuulsQvZEJ4=; b=eg/fiQSQuheAFhryfeCY5aVDo+ +A2xakqqMY2XPpwf1GzH5kvN/fznUQ6AWjeRlyc2kbUMgYNrBpPmu1R3c7ht+8WrmwO6LhMfcDhtw bTKKN2FhNOAcqs1skE70mwVvH; Received: from 75-166-142-177.hlrn.qwest.net ([75.166.142.177]:42504 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1qVdY0-000iNe-1o; Mon, 14 Aug 2023 13:48:16 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] C++-ify minidebug.c Date: Mon, 14 Aug 2023 13:48:07 -0600 Message-ID: <20230814194807.63066-1-tom@tromey.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 75.166.142.177 X-Source-L: No X-Exim-ID: 1qVdY0-000iNe-1o X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-142-177.hlrn.qwest.net (localhost.localdomain) [75.166.142.177]:42504 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3024.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,JMQ_SPF_NEUTRAL,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: I noticed minidebug.c was still using explicit malloc and free, where a vector would be more automatic. --- gdb/minidebug.c | 71 +++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 43 deletions(-) diff --git a/gdb/minidebug.c b/gdb/minidebug.c index a3ab0e3a5c2..979e569e639 100644 --- a/gdb/minidebug.c +++ b/gdb/minidebug.c @@ -60,22 +60,22 @@ static lzma_allocator gdb_lzma_allocator = { alloc_lzma, free_lzma, NULL }; struct gdb_lzma_stream { /* Section of input BFD from which we are decoding data. */ - asection *section; + asection *section = nullptr; /* lzma library decompression state. */ - lzma_index *index; + lzma_index *index = nullptr; /* Currently decoded block. */ - bfd_size_type data_start; - bfd_size_type data_end; - gdb_byte *data; + bfd_size_type data_start = 0; + bfd_size_type data_end = 0; + gdb::byte_vector data; }; /* bfd_openr_iovec OPEN_P implementation for find_separate_debug_file_in_section. OPEN_CLOSURE is 'asection *' of the section to decompress. - Return 'struct gdb_lzma_stream *' must be freed by caller by xfree, + Return 'struct gdb_lzma_stream *' must be freed by caller by delete, together with its INDEX lzma data. */ static void * @@ -85,7 +85,6 @@ lzma_open (struct bfd *nbfd, void *open_closure) bfd_size_type size, offset; lzma_stream_flags options; gdb_byte footer[LZMA_STREAM_HEADER_SIZE]; - gdb_byte *indexdata; lzma_index *index; uint64_t memlimit = UINT64_MAX; struct gdb_lzma_stream *lstream; @@ -105,24 +104,23 @@ lzma_open (struct bfd *nbfd, void *open_closure) } offset -= options.backward_size; - indexdata = (gdb_byte *) xmalloc (options.backward_size); + gdb::byte_vector indexdata (options.backward_size); index = NULL; pos = 0; if (bfd_seek (section->owner, offset, SEEK_SET) != 0 - || bfd_read (indexdata, options.backward_size, section->owner) + || bfd_read (indexdata.data (), options.backward_size, section->owner) != options.backward_size || lzma_index_buffer_decode (&index, &memlimit, &gdb_lzma_allocator, - indexdata, &pos, options.backward_size) + indexdata.data (), &pos, + options.backward_size) != LZMA_OK || lzma_index_size (index) != options.backward_size) { - xfree (indexdata); bfd_set_error (bfd_error_wrong_format); return NULL; } - xfree (indexdata); - lstream = XCNEW (struct gdb_lzma_stream); + lstream = new struct gdb_lzma_stream; lstream->section = section; lstream->index = index; @@ -140,7 +138,6 @@ lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes, struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream; bfd_size_type chunk_size; lzma_index_iter iter; - gdb_byte *compressed, *uncompressed; file_ptr block_offset; lzma_filter filters[LZMA_FILTERS_MAX + 1]; lzma_block block; @@ -150,7 +147,7 @@ lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes, res = 0; while (nbytes > 0) { - if (lstream->data == NULL + if (lstream->data.empty () || lstream->data_start > offset || offset >= lstream->data_end) { asection *section = lstream->section; @@ -159,54 +156,43 @@ lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes, if (lzma_index_iter_locate (&iter, offset)) break; - compressed = (gdb_byte *) xmalloc (iter.block.total_size); + gdb::byte_vector compressed (iter.block.total_size); block_offset = section->filepos + iter.block.compressed_file_offset; if (bfd_seek (section->owner, block_offset, SEEK_SET) != 0 - || bfd_read (compressed, iter.block.total_size, section->owner) - != iter.block.total_size) - { - xfree (compressed); - break; - } + || bfd_read (compressed.data (), iter.block.total_size, + section->owner) != iter.block.total_size) + break; - uncompressed = (gdb_byte *) xmalloc (iter.block.uncompressed_size); + gdb::byte_vector uncompressed (iter.block.uncompressed_size); memset (&block, 0, sizeof (block)); block.filters = filters; block.header_size = lzma_block_header_size_decode (compressed[0]); - if (lzma_block_header_decode (&block, &gdb_lzma_allocator, compressed) + if (lzma_block_header_decode (&block, &gdb_lzma_allocator, + compressed.data ()) != LZMA_OK) - { - xfree (compressed); - xfree (uncompressed); - break; - } + break; compressed_pos = block.header_size; uncompressed_pos = 0; if (lzma_block_buffer_decode (&block, &gdb_lzma_allocator, - compressed, &compressed_pos, + compressed.data (), &compressed_pos, iter.block.total_size, - uncompressed, &uncompressed_pos, + uncompressed.data (), + &uncompressed_pos, iter.block.uncompressed_size) != LZMA_OK) - { - xfree (compressed); - xfree (uncompressed); - break; - } - - xfree (compressed); + break; - xfree (lstream->data); - lstream->data = uncompressed; + lstream->data = std::move (uncompressed); lstream->data_start = iter.block.uncompressed_file_offset; lstream->data_end = (iter.block.uncompressed_file_offset + iter.block.uncompressed_size); } chunk_size = std::min (nbytes, (file_ptr) lstream->data_end - offset); - memcpy (buf, lstream->data + offset - lstream->data_start, chunk_size); + memcpy (buf, lstream->data.data () + offset - lstream->data_start, + chunk_size); buf = (gdb_byte *) buf + chunk_size; offset += chunk_size; nbytes -= chunk_size; @@ -227,8 +213,7 @@ lzma_close (struct bfd *nbfd, struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream; lzma_index_end (lstream->index, &gdb_lzma_allocator); - xfree (lstream->data); - xfree (lstream); + delete lstream; /* Zero means success. */ return 0; -- 2.41.0