From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 111655 invoked by alias); 6 Sep 2018 21:13:28 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 111408 invoked by uid 89); 6 Sep 2018 21:13:27 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.4 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=rows, 17057 X-HELO: gateway31.websitewelcome.com Received: from gateway31.websitewelcome.com (HELO gateway31.websitewelcome.com) (192.185.143.46) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 06 Sep 2018 21:13:25 +0000 Received: from cm10.websitewelcome.com (cm10.websitewelcome.com [100.42.49.4]) by gateway31.websitewelcome.com (Postfix) with ESMTP id EE96B103E8 for ; Thu, 6 Sep 2018 16:13:22 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id y1aSfYHZyBcCXy1aXf0Kgl; Thu, 06 Sep 2018 16:13:22 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Oh3JQcH+2DtbaOEpbzECv7fT+PBnWcOfJA9XPF4WktE=; b=KQbAnVjLoq1w1KPEWUAZhmySqI +EmjKxYC9p9Pv/GM62WrSPXvKn5lTMXRz6ElXR1LAhboFp8vSf8/X/gcwWMieBq7EwF0Oa/SLMObs 8jp/b7+V0YSv1xTIGdI+u6ZgN; Received: from 75-166-85-72.hlrn.qwest.net ([75.166.85.72]:34550 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1fy1aS-0013U4-0D; Thu, 06 Sep 2018 16:13:12 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFC 1/8] Change wrap buffering to use a std::string Date: Thu, 06 Sep 2018 21:13:00 -0000 Message-Id: <20180906211303.11029-2-tom@tromey.com> In-Reply-To: <20180906211303.11029-1-tom@tromey.com> References: <20180906211303.11029-1-tom@tromey.com> X-SW-Source: 2018-09/txt/msg00109.txt.bz2 Currently wrap buffering is implemented by allocating a string that is the same width as the window, and then writing characters into it. However, if gdb emits terminal escapes, then these could possibly overflow the buffer. To prevent this, change the wrap buffer to be a std::string and update the various uses. gdb/ChangeLog 2018-09-06 Tom Tromey * utils.c (filter_initalized): New global. (wrap_buffer): Now a std::string. (wrap_pointer): Remove. (filtered_printing_initialized, set_width, wrap_here) (fputs_maybe_filtered): Update. --- gdb/ChangeLog | 8 ++++++++ gdb/utils.c | 47 ++++++++++++++++------------------------------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/gdb/utils.c b/gdb/utils.c index 7a8c80c64ed..1982fa20e64 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -1268,13 +1268,11 @@ static bool pagination_disabled_for_command; the end of the line, we spit out a newline, the indent, and then the buffered output. */ -/* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which - are waiting to be output (they have already been counted in chars_printed). - When wrap_buffer[0] is null, the buffer is empty. */ -static char *wrap_buffer; +static bool filter_initalized = false; -/* Pointer in wrap_buffer to the next character to fill. */ -static char *wrap_pointer; +/* Contains characters which are waiting to be output (they have + already been counted in chars_printed). */ +static std::string wrap_buffer; /* String to indent by if the wrap occurs. Must not be NULL if wrap_column is non-zero. */ @@ -1347,7 +1345,7 @@ init_page_info (void) int filtered_printing_initialized (void) { - return wrap_buffer != NULL; + return filter_initalized; } set_batch_flag_and_restore_page_info::set_batch_flag_and_restore_page_info () @@ -1387,8 +1385,7 @@ set_screen_size (void) rl_set_screen_size (rows, cols); } -/* Reinitialize WRAP_BUFFER according to the current value of - CHARS_PER_LINE. */ +/* Reinitialize WRAP_BUFFER. */ static void set_width (void) @@ -1396,14 +1393,8 @@ set_width (void) if (chars_per_line == 0) init_page_info (); - if (!wrap_buffer) - { - wrap_buffer = (char *) xmalloc (chars_per_line + 2); - wrap_buffer[0] = '\0'; - } - else - wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2); - wrap_pointer = wrap_buffer; /* Start it at the beginning. */ + wrap_buffer.clear (); + filter_initalized = true; } static void @@ -1546,17 +1537,13 @@ void wrap_here (const char *indent) { /* This should have been allocated, but be paranoid anyway. */ - if (!wrap_buffer) + if (!filter_initalized) internal_error (__FILE__, __LINE__, _("failed internal consistency check")); - if (wrap_buffer[0]) - { - *wrap_pointer = '\0'; - fputs_unfiltered (wrap_buffer, gdb_stdout); - } - wrap_pointer = wrap_buffer; - wrap_buffer[0] = '\0'; + if (!wrap_buffer.empty ()) + fputs_unfiltered (wrap_buffer.c_str (), gdb_stdout); + wrap_buffer.clear (); if (chars_per_line == UINT_MAX) /* No line overflow checking. */ { wrap_column = 0; @@ -1693,7 +1680,7 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream, if (*lineptr == '\t') { if (wrap_column) - *wrap_pointer++ = '\t'; + wrap_buffer.push_back ('\t'); else fputc_unfiltered ('\t', stream); /* Shifting right by 3 produces the number of tab stops @@ -1705,7 +1692,7 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream, else { if (wrap_column) - *wrap_pointer++ = *lineptr; + wrap_buffer.push_back (*lineptr); else fputc_unfiltered (*lineptr, stream); chars_printed++; @@ -1735,8 +1722,7 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream, if (wrap_column) { fputs_unfiltered (wrap_indent, stream); - *wrap_pointer = '\0'; /* Null-terminate saved stuff, */ - fputs_unfiltered (wrap_buffer, stream); /* and eject it. */ + fputs_unfiltered (wrap_buffer.c_str (), stream); /* FIXME, this strlen is what prevents wrap_indent from containing tabs. However, if we recurse to print it and count its chars, we risk trouble if wrap_indent is @@ -1745,8 +1731,7 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream, if we are printing a long string. */ chars_printed = strlen (wrap_indent) + (save_chars - wrap_column); - wrap_pointer = wrap_buffer; /* Reset buffer */ - wrap_buffer[0] = '\0'; + wrap_buffer.clear (); wrap_column = 0; /* And disable fancy wrap */ } } -- 2.13.6