Re: [RFAv2 2/2] gmp-utils: protect gdb_mpz exports against out-of-range values

Simon Marchi
2020-12-05T23:26:22Z

On 2020-12-05 3:10 a.m., Joel Brobecker wrote:
> @@ -81,13 +133,26 @@ gdb_mpz::write (gdb::array_view<gdb_byte> buf, enum bfd_endian byte_order,
> mpz_add (exported_val.val, exported_val.val, neg_offset.val);
> }
>
> + /* Do the export into a buffer allocated by GMP itself; that way,
> + we can detect cases where BUF is not large enough to export
> + our value, and thus avoid a buffer overlow. Normally, this should
> + never happen, since we verified earlier that the buffer is large
> + enough to accomodate our value, but doing this allows us to be
> + extra safe with the export.
> +
> + After verification that the export behaved as expected, we will
> + copy the data over to BUF. */
> +
> + size_t word_countp;
> + gdb::unique_xmalloc_ptr<void> exported
> + (mpz_export (NULL, &word_countp, -1 /* order */, buf.size () /* size */,
> + endian, 0 /* nails */, exported_val.val));
> +
> + gdb_assert (word_countp == 1);
> +
> /* Start by clearing the buffer, as mpz_export only writes as many
> - bytes as it needs (including none, if the value to export is zero. */

That comment is now stale.

Otherwise, that LGTM.

Simon