From: Tom Tromey Date: Mon, 14 Aug 2023 19:45:26 +0000 (-0600) Subject: C++-ify minidebug.c X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=e76d241a64cc02accc1717b5df9624a9dbb31598;p=binutils-gdb.git C++-ify minidebug.c I noticed minidebug.c was still using explicit malloc and free, where a vector would be more automatic. Reviewed-by: John Baldwin --- 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;