public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Prevent overflow in rl_set_screen_size
@ 2018-10-27  4:56 Saagar Jha
  2019-02-15  1:52 ` Kevin Buettner
  0 siblings, 1 reply; 12+ messages in thread
From: Saagar Jha @ 2018-10-27  4:56 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 581 bytes --]

Hi,

I was running GDB under the undefined behavior sanitizer and I found a signed integer overflow in set_screen_size. I’ve attached a (IMO slightly clumsy, but I couldn’t think of a nicer way to solve this) patch that solves this issue. I couldn’t figure out how to formally test this code, but I can compile and run this on my computer running macOS Mojave 10.14.1. Would someone mind taking a look at this? This is my first set of contributions to GDB, so if there’s anything wrong (or you have general feedback) I’d love to hear about it!

Regards,
Saagar Jha

[-- Attachment #2: Prevent-overflow-in-rl_set_screen_size.patch --]
[-- Type: application/octet-stream, Size: 1600 bytes --]

From 9bf98bc3963d822e33f485067907b191420bb8e4 Mon Sep 17 00:00:00 2001
From: Saagar Jha <saagar@saagarjha.com>
Date: Tue, 22 May 2018 04:08:40 -0700
Subject: [PATCH 1/5] Prevent overflow in rl_set_screen_size

GDB calls rl_set_screen_size in readline with the current screen size,
measured in rows and columns. To represent "infinite" sizes, GDB passes
in INT_MAX; however, since rl_set_screen_size internally multiplies the
number of rows and columns, this causes a signed integer overflow. To
prevent this we can instead pass in the approximate square root of
INT_MAX (which is still reasonably large), so that even when the number
of rows and columns is "infinite" we don't overflow.

gdb/ChangeLog:
2018-05-22  Saagar Jha  <saagar@saagarjha.com>

	* utils.c: Reduce "infinite" rows and columns before calling
	rl_set_screen_size.
---
 gdb/utils.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/gdb/utils.c b/gdb/utils.c
index 8d4a744e71..56257c35cf 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -1377,11 +1377,13 @@ set_screen_size (void)
   int rows = lines_per_page;
   int cols = chars_per_line;
 
+  // Use approximately sqrt(INT_MAX) instead of INT_MAX so that we don't
+  // overflow in rl_set_screen_size, which multiplies rows and columns
   if (rows <= 0)
-    rows = INT_MAX;
+    rows = INT_MAX >> (sizeof(int) * 8 / 2);
 
   if (cols <= 0)
-    cols = INT_MAX;
+    cols = INT_MAX >> (sizeof(int) * 8 / 2);
 
   /* Update Readline's idea of the terminal size.  */
   rl_set_screen_size (rows, cols);
-- 
2.19.1


[-- Attachment #3: Type: text/plain, Size: 1 bytes --]



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2019-02-27 18:52 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-27  4:56 [PATCH] Prevent overflow in rl_set_screen_size Saagar Jha
2019-02-15  1:52 ` Kevin Buettner
2019-02-15  9:40   ` Pedro Alves
2019-02-15 10:52     ` Saagar Jha
2019-02-15 20:19     ` Tom Tromey
2019-02-20 15:54       ` Pedro Alves
2019-02-20 17:22         ` Pedro Alves
2019-02-20 17:37           ` [PATCH 2/2] Make 'show width/height' display "unlimited" when capped, for readline (Re: [PATCH] Prevent overflow in rl_set_screen_size) Pedro Alves
2019-02-20 21:04             ` Tom Tromey
2019-02-20 23:02               ` Kevin Buettner
2019-02-27 18:51                 ` Pedro Alves
2019-02-27 18:52                   ` [PATCH] Test "set width/height -1" (Re: [PATCH 2/2] Make 'show width/height' display "unlimited" when capped for readline) Pedro Alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).