From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2424 invoked by alias); 22 Apr 2018 19:02:36 -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 2413 invoked by uid 89); 22 Apr 2018 19:02:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.2 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Something, theyre, clearer, bound X-HELO: mx1.redhat.com Received: from mx3-rdu2.redhat.com (HELO mx1.redhat.com) (66.187.233.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 22 Apr 2018 19:02:34 +0000 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 08E408DC3E; Sun, 22 Apr 2018 19:02:33 +0000 (UTC) Received: from [127.0.0.1] (ovpn04.gateway.prod.ext.ams2.redhat.com [10.39.146.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8408F10AF9F6; Sun, 22 Apr 2018 19:02:32 +0000 (UTC) Subject: Re: [RFA 6/8] Use function_view in cli-script.c To: Tom Tromey , gdb-patches@sourceware.org References: <20180419191539.661-1-tom@tromey.com> <20180419191539.661-7-tom@tromey.com> From: Pedro Alves Message-ID: Date: Sun, 22 Apr 2018 19:02:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <20180419191539.661-7-tom@tromey.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-SW-Source: 2018-04/txt/msg00437.txt.bz2 On 04/19/2018 08:15 PM, Tom Tromey wrote: > -/* The mi_read_next_line consults these variable to return successive > - command lines. While it would be clearer to use a closure pointer, > - it is not expected that any future code will use read_command_lines_1, > - therefore no point of overengineering. */ Ahahahah :-) > void > mi_cmd_break_commands (const char *command, char **argv, int argc) > { > @@ -509,15 +492,24 @@ mi_cmd_break_commands (const char *command, char **argv, int argc) > if (b == NULL) > error (_("breakpoint %d not found."), bnum); > > - mi_command_line_array = argv; > - mi_command_line_array_ptr = 1; > - mi_command_line_array_cnt = argc; > + int count = 1; > + gdb::function_view reader > + = [&] () > + { > + const char *result = nullptr; > + if (count < argc) > + result = argv[count++]; > + return result; > + }; This is incorrect -- The 'reader' function_view is storing an address to a closure whose lifetime ends right after the function_view ctor is run. This a similar issue with assigning a string_view to a temporary std::string, like: std::string_view view = std::string ("temp"); C++ doesn't yet have a mechanism that allows making string_view and types like it extend the lifetime of temporaries they're bound to, like const references can. You can fix the issue at hand here easily by not using function_view at all: auto reader = [&] () { const char *result = nullptr; if (count < argc) result = argv[count++]; return result; }; Now "reader" is the lambda closure itself. > - cmd = read_command_lines (str.c_str (), from_tty, 1, > - (is_tracepoint (b) > - ? check_tracepoint_command : 0), > - b); > + gdb::function_view validator; > + if (is_tracepoint (b)) > + validator = [=] (const char *line) > + { > + validate_actionline (line, b); > + }; > + > + cmd = read_command_lines (str.c_str (), from_tty, 1, validator); > } > } Similar problem here. Something like this would fix it: auto validator = [=] (const char *line) { validate_actionline (line, b); }; decltype (callback) null_callback; cmd = read_command_lines (str.c_str (), from_tty, 1, is_tracepoint (b) ? validator : null_callback); or use if/else: if (is_tracepoint (b)) { cmd = read_command_lines (str.c_str (), from_tty, 1, [=] (const char *line) { validate_actionline (line, b); }; } else cmd = read_command_lines (str.c_str (), from_tty, 1, nullptr); Thanks, Pedro Alves