Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [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

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