From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 108958 invoked by alias); 13 Mar 2019 18:06:18 -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 108949 invoked by uid 89); 13 Mar 2019 18:06:18 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=admit X-HELO: mail-wm1-f68.google.com Received: from mail-wm1-f68.google.com (HELO mail-wm1-f68.google.com) (209.85.128.68) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 13 Mar 2019 18:06:17 +0000 Received: by mail-wm1-f68.google.com with SMTP id x10so203895wmg.2 for ; Wed, 13 Mar 2019 11:06:16 -0700 (PDT) Return-Path: Received: from ?IPv6:2001:8a0:f913:f700:56ee:75ff:fe8d:232b? ([2001:8a0:f913:f700:56ee:75ff:fe8d:232b]) by smtp.gmail.com with ESMTPSA id w10sm21348577wrn.32.2019.03.13.11.06.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Mar 2019 11:06:14 -0700 (PDT) Subject: Re: [RFC 8.3 3/3] Avoid a crash in source_cache::extract_lines To: Tom Tromey References: <20190308210433.32683-1-tromey@adacore.com> <20190308210433.32683-4-tromey@adacore.com> <87va0mpu7o.fsf@tromey.com> Cc: gdb-patches@sourceware.org From: Pedro Alves Message-ID: Date: Wed, 13 Mar 2019 18:06:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <87va0mpu7o.fsf@tromey.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-SW-Source: 2019-03/txt/msg00277.txt.bz2 On 03/13/2019 05:20 PM, Tom Tromey wrote: >>>>>> "Pedro" == Pedro Alves writes: > > Pedro> Can you clarify how can first_pos end up as npos? Is that a bug in the > Pedro> caller, or is it normal? The documentation doesn't seem to allow for that: > > Pedro> /* Get the source text for the source file in symtab S. FIRST_LINE > Pedro> and LAST_LINE are the first and last lines to return; line > Pedro> numbers are 1-based. If the file cannot be read, false is > Pedro> returned. Otherwise, LINES_OUT is set to the desired text. The > Pedro> returned text may include ANSI terminal escapes. */ > > I think you're just confusing first_pos and first_line here. > first_pos is a local variable that's used to track the position where > the first line starts: > > std::string::size_type first_pos = std::string::npos; > [...] > if (lineno == first_line) > first_pos = pos; > > It can still be npos if first_line is greater than the number of lines > in the file. Oh, I see now, now that I actually look at the code, rather than just the patch. Sorry about that. And now that I look, I admit it took me a bit to grok the function, but I got it. IIUC, the function can never really return false, right? Since get_source_lines already validates input. If you made extract_lines return std::string instead of using an output parameter, then you could conveniently write: if (first_pos == std::string::npos) return {}; for this case, which might be a little clearer than the resulting "npos - npos" with your patch. Anyhow, not that important. Patch LGTM. Thanks, Pedro Alves