ALT Linux Team development discussions
 help / color / mirror / Atom feed
From: Ivan Zakharyaschev <imz@altlinux.org>
To: ALT Linux Team development discussions <devel@lists.altlinux.org>
Subject: Re: [devel] [APT PATCH] rpmSingle{Pkg, Src}Index::ArchiveURI(): avoid cases with undefined behavior
Date: Tue, 18 Feb 2020 04:55:33 +0300 (MSK)
Message-ID: <alpine.LFD.2.20.2002180448260.6363@imap.altlinux.org> (raw)
In-Reply-To: <alpine.LFD.2.20.2002161649060.6363@imap.altlinux.org>

On Mon, 17 Feb 2020, Ivan Zakharyaschev wrote:

> 
> On Sun, 16 Feb 2020, Dmitry V. Levin wrote:
> 
> > On Sun, Feb 16, 2020 at 04:09:14AM +0300, Ivan Zakharyaschev wrote:
> 
> > > * File.length() < 2. Since File was a non-const string,
> > >   File[File.length()] might be UB before C++11. Now, File is const, and
> > >   it is guaranteed that File[File.length()] == 0.
> > 
> > We can safely assume C++11, but I don't think we have an UB here even
> > before C++11.
> 
> FWIW, after thinkng over the corresponding piece[1] which you cited (a 
> third time):
> 
> [1]: https://en.cppreference.com/w/cpp/string/basic_string/operator_at
> 
> > reference       operator[]( size_type pos );(1)
> > const_reference operator[]( size_type pos ) const;(2)
> > 
> > Returns a reference to the character at specified location pos. [...]
> > 
> > 1) If pos == size(), the behavior is undefined.
> > 2) If pos == size(), a reference to the character with value CharT() 
> > (the null character) is returned.
> > 
> > (until C++11)
> 
> I can see a reason to agree with you: whether the object is const might 
> not be the thing that determines which variant of the method is used.
> 
> The question was whether variant (1) or (2) was used here in
> (File[1] == '/') if File was not const.
> 
> (If (1) is used, it is stated that it would be UB until C++11; if (2), 
> then not.)
> 
> I doubt I'm able remember all the details of how the choice is done 
> between overloaded methods, but common sense gives a hint: only methods 
> labelled "const" can be invoked on a const object whereas all methods 
> (both labelled "const" and not) can be invoked on a non-const object.
> 
> Under this view, we see that the methods labelled "const" are equally well 
> available for non-const objects. So, another factor must make the choice 
> between (1) and (2).
> 
> And here the only remaining factor seems to be the expected result 
> type/constness in the expression where it is used (although that might 
> seem weird because of its "implicitness"): a const reference will do for 
> (_ == '/'), so (2) is probably inferred under the hood as the variant to 
> use here.
> 
> Of course, I might be wrong in my reasoning, and it's not quite important 
> for this place in APT's code anymore anyway (as there was another reason 
> to rewrite it), but I wished to share this curiosity regarding how the 
> meaning of C++ code can be non-obvious to mere mortals (like me).

No, this reasoning must have been false, as pointed out by darktemplar@,  
by giving a link to the following explanations.

https://stackoverflow.com/a/164208/94687 :

The compiler does not take into account how you are using the return value 
in its determination; that's not part of the rules. It doesn't know if 
you're doing

    std::string name = b->Name();

or

    b->Name() = "me";

It has to choose the version that works in both cases.

https://stackoverflow.com/a/164130/94687 :

> There it noted that member functions may be overloaded merely by their 
> const attribute. In those cases, the compiler will use the member function 
> matching most closely the const-qualification of the object

-- 
Best regards,
Ivan


      reply	other threads:[~2020-02-18  1:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-16  1:09 Ivan Zakharyaschev
2020-02-16 11:17 ` Dmitry V. Levin
2020-02-16 17:25   ` [devel] [nitpick] " Michael Shigorin
2020-02-17  0:39   ` [devel] " Ivan Zakharyaschev
2020-02-18  1:55     ` Ivan Zakharyaschev [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LFD.2.20.2002180448260.6363@imap.altlinux.org \
    --to=imz@altlinux.org \
    --cc=devel@lists.altlinux.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

ALT Linux Team development discussions

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/devel/0 devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 devel devel/ http://lore.altlinux.org/devel \
		devel@altlinux.org devel@altlinux.ru devel@lists.altlinux.org devel@lists.altlinux.ru devel@linux.iplabs.ru mandrake-russian@linuxteam.iplabs.ru sisyphus@linuxteam.iplabs.ru
	public-inbox-index devel

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git