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 <jhb@FreeBSD.org>
This commit is contained in:
Tom Tromey 2023-08-14 13:45:26 -06:00
parent 1f572864da
commit e76d241a64

View File

@ -60,22 +60,22 @@ static lzma_allocator gdb_lzma_allocator = { alloc_lzma, free_lzma, NULL };
struct gdb_lzma_stream struct gdb_lzma_stream
{ {
/* Section of input BFD from which we are decoding data. */ /* Section of input BFD from which we are decoding data. */
asection *section; asection *section = nullptr;
/* lzma library decompression state. */ /* lzma library decompression state. */
lzma_index *index; lzma_index *index = nullptr;
/* Currently decoded block. */ /* Currently decoded block. */
bfd_size_type data_start; bfd_size_type data_start = 0;
bfd_size_type data_end; bfd_size_type data_end = 0;
gdb_byte *data; gdb::byte_vector data;
}; };
/* bfd_openr_iovec OPEN_P implementation for /* bfd_openr_iovec OPEN_P implementation for
find_separate_debug_file_in_section. OPEN_CLOSURE is 'asection *' find_separate_debug_file_in_section. OPEN_CLOSURE is 'asection *'
of the section to decompress. 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. */ together with its INDEX lzma data. */
static void * static void *
@ -85,7 +85,6 @@ lzma_open (struct bfd *nbfd, void *open_closure)
bfd_size_type size, offset; bfd_size_type size, offset;
lzma_stream_flags options; lzma_stream_flags options;
gdb_byte footer[LZMA_STREAM_HEADER_SIZE]; gdb_byte footer[LZMA_STREAM_HEADER_SIZE];
gdb_byte *indexdata;
lzma_index *index; lzma_index *index;
uint64_t memlimit = UINT64_MAX; uint64_t memlimit = UINT64_MAX;
struct gdb_lzma_stream *lstream; struct gdb_lzma_stream *lstream;
@ -105,24 +104,23 @@ lzma_open (struct bfd *nbfd, void *open_closure)
} }
offset -= options.backward_size; offset -= options.backward_size;
indexdata = (gdb_byte *) xmalloc (options.backward_size); gdb::byte_vector indexdata (options.backward_size);
index = NULL; index = NULL;
pos = 0; pos = 0;
if (bfd_seek (section->owner, offset, SEEK_SET) != 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 != options.backward_size
|| lzma_index_buffer_decode (&index, &memlimit, &gdb_lzma_allocator, || lzma_index_buffer_decode (&index, &memlimit, &gdb_lzma_allocator,
indexdata, &pos, options.backward_size) indexdata.data (), &pos,
options.backward_size)
!= LZMA_OK != LZMA_OK
|| lzma_index_size (index) != options.backward_size) || lzma_index_size (index) != options.backward_size)
{ {
xfree (indexdata);
bfd_set_error (bfd_error_wrong_format); bfd_set_error (bfd_error_wrong_format);
return NULL; return NULL;
} }
xfree (indexdata);
lstream = XCNEW (struct gdb_lzma_stream); lstream = new struct gdb_lzma_stream;
lstream->section = section; lstream->section = section;
lstream->index = index; 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; struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
bfd_size_type chunk_size; bfd_size_type chunk_size;
lzma_index_iter iter; lzma_index_iter iter;
gdb_byte *compressed, *uncompressed;
file_ptr block_offset; file_ptr block_offset;
lzma_filter filters[LZMA_FILTERS_MAX + 1]; lzma_filter filters[LZMA_FILTERS_MAX + 1];
lzma_block block; lzma_block block;
@ -150,7 +147,7 @@ lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
res = 0; res = 0;
while (nbytes > 0) while (nbytes > 0)
{ {
if (lstream->data == NULL if (lstream->data.empty ()
|| lstream->data_start > offset || offset >= lstream->data_end) || lstream->data_start > offset || offset >= lstream->data_end)
{ {
asection *section = lstream->section; 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)) if (lzma_index_iter_locate (&iter, offset))
break; 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; block_offset = section->filepos + iter.block.compressed_file_offset;
if (bfd_seek (section->owner, block_offset, SEEK_SET) != 0 if (bfd_seek (section->owner, block_offset, SEEK_SET) != 0
|| bfd_read (compressed, iter.block.total_size, section->owner) || bfd_read (compressed.data (), iter.block.total_size,
!= iter.block.total_size) section->owner) != iter.block.total_size)
{
xfree (compressed);
break; break;
}
uncompressed = (gdb_byte *) xmalloc (iter.block.uncompressed_size); gdb::byte_vector uncompressed (iter.block.uncompressed_size);
memset (&block, 0, sizeof (block)); memset (&block, 0, sizeof (block));
block.filters = filters; block.filters = filters;
block.header_size = lzma_block_header_size_decode (compressed[0]); 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) != LZMA_OK)
{
xfree (compressed);
xfree (uncompressed);
break; break;
}
compressed_pos = block.header_size; compressed_pos = block.header_size;
uncompressed_pos = 0; uncompressed_pos = 0;
if (lzma_block_buffer_decode (&block, &gdb_lzma_allocator, if (lzma_block_buffer_decode (&block, &gdb_lzma_allocator,
compressed, &compressed_pos, compressed.data (), &compressed_pos,
iter.block.total_size, iter.block.total_size,
uncompressed, &uncompressed_pos, uncompressed.data (),
&uncompressed_pos,
iter.block.uncompressed_size) iter.block.uncompressed_size)
!= LZMA_OK) != LZMA_OK)
{
xfree (compressed);
xfree (uncompressed);
break; break;
}
xfree (compressed); lstream->data = std::move (uncompressed);
xfree (lstream->data);
lstream->data = uncompressed;
lstream->data_start = iter.block.uncompressed_file_offset; lstream->data_start = iter.block.uncompressed_file_offset;
lstream->data_end = (iter.block.uncompressed_file_offset lstream->data_end = (iter.block.uncompressed_file_offset
+ iter.block.uncompressed_size); + iter.block.uncompressed_size);
} }
chunk_size = std::min (nbytes, (file_ptr) lstream->data_end - offset); 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; buf = (gdb_byte *) buf + chunk_size;
offset += chunk_size; offset += chunk_size;
nbytes -= chunk_size; nbytes -= chunk_size;
@ -227,8 +213,7 @@ lzma_close (struct bfd *nbfd,
struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream; struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
lzma_index_end (lstream->index, &gdb_lzma_allocator); lzma_index_end (lstream->index, &gdb_lzma_allocator);
xfree (lstream->data); delete lstream;
xfree (lstream);
/* Zero means success. */ /* Zero means success. */
return 0; return 0;