* [PATCH] Fix malloc allocation size sanity check
@ 2020-08-12 19:17 Luis Machado
2020-08-12 19:31 ` Tom Tromey
0 siblings, 1 reply; 5+ messages in thread
From: Luis Machado @ 2020-08-12 19:17 UTC (permalink / raw)
To: gdb-patches
During debugging of PR26362, it was noticed that the malloc size check
in check_type_length_before_alloc wasn't detecting an allocation attempt
of a huge amount of bytes, making GDB run into an internal error.
This happens because we're using an int to store a type's length. When the
type length is large enough, the int will overflow and the max_value_size
check won't work anymore.
The following patch fixes this by making the length variable a size_t.
Printing statements were also updated to show the correct number of bytes.
gdb/ChangeLog:
YYYY-MM-DD Luis Machado <luis.machado@linaro.org>
* value.c (check_type_length_before_alloc): Use size_t to store a
type's length.
Use %s and pulongest to print the length.
---
gdb/value.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/gdb/value.c b/gdb/value.c
index aac9baaaf5..4efd75fa25 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -997,16 +997,16 @@ show_max_value_size (struct ui_file *file, int from_tty,
static void
check_type_length_before_alloc (const struct type *type)
{
- unsigned int length = TYPE_LENGTH (type);
+ size_t length = TYPE_LENGTH (type);
if (max_value_size > -1 && length > max_value_size)
{
if (type->name () != NULL)
- error (_("value of type `%s' requires %u bytes, which is more "
- "than max-value-size"), type->name (), length);
+ error (_("value of type `%s' requires %s bytes, which is more "
+ "than max-value-size"), type->name (), pulongest (length));
else
- error (_("value requires %u bytes, which is more than "
- "max-value-size"), length);
+ error (_("value requires %s bytes, which is more than "
+ "max-value-size"), pulongest (length));
}
}
--
2.17.1
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] Fix malloc allocation size sanity check
2020-08-12 19:17 [PATCH] Fix malloc allocation size sanity check Luis Machado
@ 2020-08-12 19:31 ` Tom Tromey
2020-08-12 19:32 ` Tom Tromey
2020-08-12 19:46 ` Luis Machado
0 siblings, 2 replies; 5+ messages in thread
From: Tom Tromey @ 2020-08-12 19:31 UTC (permalink / raw)
To: Luis Machado via Gdb-patches
>>>>> "Luis" == Luis Machado via Gdb-patches <gdb-patches@sourceware.org> writes:
Luis> The following patch fixes this by making the length variable a size_t.
main_type::length is a ULONGEST, so maybe that would be better?
Tom
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Fix malloc allocation size sanity check
2020-08-12 19:31 ` Tom Tromey
@ 2020-08-12 19:32 ` Tom Tromey
2020-08-12 19:46 ` Luis Machado
1 sibling, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2020-08-12 19:32 UTC (permalink / raw)
To: Tom Tromey; +Cc: Luis Machado via Gdb-patches
Tom> main_type::length is a ULONGEST, so maybe that would be better?
Oops, I mean type::length.
Tom
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Fix malloc allocation size sanity check
2020-08-12 19:31 ` Tom Tromey
2020-08-12 19:32 ` Tom Tromey
@ 2020-08-12 19:46 ` Luis Machado
2020-08-17 11:01 ` Gary Benson
1 sibling, 1 reply; 5+ messages in thread
From: Luis Machado @ 2020-08-12 19:46 UTC (permalink / raw)
To: Tom Tromey, Luis Machado via Gdb-patches
On 8/12/20 4:31 PM, Tom Tromey wrote:
>>>>>> "Luis" == Luis Machado via Gdb-patches <gdb-patches@sourceware.org> writes:
>
> Luis> The following patch fixes this by making the length variable a size_t.
>
> main_type::length is a ULONGEST, so maybe that would be better?
Yeah, I guess. I'll change it.
I noticed xzalloc takes a size_t though.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Fix malloc allocation size sanity check
2020-08-12 19:46 ` Luis Machado
@ 2020-08-17 11:01 ` Gary Benson
0 siblings, 0 replies; 5+ messages in thread
From: Gary Benson @ 2020-08-17 11:01 UTC (permalink / raw)
To: Luis Machado; +Cc: Tom Tromey, Luis Machado via Gdb-patches
gdb-patches wrote:
> On 8/12/20 4:31 PM, Tom Tromey wrote:
> > > > > > > "Luis" == Luis Machado via Gdb-patches <gdb-patches@sourceware.org> writes:
> >
> > Luis> The following patch fixes this by making the length variable a size_t.
> >
> > main_type::length is a ULONGEST, so maybe that would be better?
>
> Yeah, I guess. I'll change it.
>
> I noticed xzalloc takes a size_t though.
We're allocating a buffer on the host, to hold a value from the
target. size_t is for sizes on the host, so it's appropriate
for xzalloc, and main_type::length came from the the target, so
ULONGEST is appropriate there. The boundary stops things being
neat :(
Cheers,
Gary
--
Gary Benson - he / him / his
Principal Software Engineer, Red Hat
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-08-17 11:01 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-12 19:17 [PATCH] Fix malloc allocation size sanity check Luis Machado
2020-08-12 19:31 ` Tom Tromey
2020-08-12 19:32 ` Tom Tromey
2020-08-12 19:46 ` Luis Machado
2020-08-17 11:01 ` Gary Benson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox