From: Aleksei Nikiforov <darktemplar@altlinux.org>
To: devel@lists.altlinux.org
Subject: Re: [devel] [PATCH for apt 2/2 v2] Fix pointer arithmetics
Date: Fri, 13 Dec 2019 11:01:47 +0300
Message-ID: <32bfab23-32ca-9acb-d668-d1f9120646ae@altlinux.org> (raw)
In-Reply-To: <20191212224315.b5b14736ba94b86fa85a830c@altlinux.org>
12.12.2019 22:43, Andrey Savchenko пишет:
> On Wed, 11 Dec 2019 10:50:22 +0300 Aleksei Nikiforov wrote:
>> 11.12.2019 1:20, Dmitry V. Levin пишет:
>>> On Tue, Dec 10, 2019 at 01:58:17PM +0300, Aleksei Nikiforov wrote:
>>>> 10.12.2019 13:20, Dmitry V. Levin пишет:
>>>>> On Tue, Dec 10, 2019 at 11:18:06AM +0300, Aleksei Nikiforov wrote:
>>>>>> 10.12.2019 3:07, Dmitry V. Levin пишет:
>>>>>>> On Mon, Dec 09, 2019 at 10:08:42AM +0300, Aleksei Nikiforov wrote:
>>>>>>>> 09.12.2019 2:21, Dmitry V. Levin пишет:
>>>>>>>>> On Fri, Dec 06, 2019 at 06:36:55PM +0300, Aleksei Nikiforov wrote:
>>>>>>>>> [...]
>>>>>>>>>> @@ -85,11 +87,11 @@ class pkgCache::PkgIterator
>>>>>>>>>> inline unsigned long long Index() const {return Pkg - Owner->PkgP;};
>>>>>>>>>> OkState State() const;
>>>>>>>>>>
>>>>>>>>>> - void ReMap(void const * const oldMap, void const * const newMap)
>>>>>>>>>> + void ReMap(void *oldMap, void *newMap)
>>>>>>>>>
>>>>>>>>> Is there any particular reason for stripping const here and in other
>>>>>>>>> similar places?
>>>>>>>>
>>>>>>>> Yes, it's needed due to issues emerging from mixing const and non-const
>>>>>>>> pointers with new and allegedly more proper way of calculating rebased
>>>>>>>> pointers.
>>>>>>>
>>>>>>> Sorry, I don't find this argument convincing.
>>>>>>> I have experienced no const issues in my version of this fix.
>>>>>>
>>>>>> Your version is using C-style casts in C++ code. Of course, I could use
>>>>>> C-style casts or const_cast-s too to work around const correctness
>>>>>> issues (i.e. to just hide these issues), and it'd work like your
>>>>>> version. But I'd like to remind you that APT is C++ project, not a C
>>>>>> project. What might be ok for C is sometimes a dirty ugly hack in modern
>>>>>> C++.
>>>>>
>>>>> Sorry, I don't share you point of view on this matter.
>>>>> Being a C++ project allows you to use C++ constructs, that's true,
>>>>> but why do you think it prevents you from using C constructs when
>>>>> appropriate?
>>>>
>>>> I didn't say that something prevents from using C constructs when
>>>> appropriate. I'm saying that these constructs are not appropriate here.
>>>
>>> Why do you think they are not appropriate here?
>>>
>>
>> In good C++ code there is no place for const_cast.
>
> This statement is ungrounded.
>
It is grounded. Read my answer below (and links [2] and [3] to
documentation you provided).
>> Maybe there are some
>> exceptions to it, but they have to be justified. It doesn't matter that
>> you are hiding it behind C-style cast.
>
> Please read some good book on C++ like [1] or at least the official
> reference manual [2].
Same to you.
>
> Aside from C-style cast C++ supports four casts (in their safety
> order, the safest first):
> const_cast
> static_cast
> dynamic_cast
> reinterpret_cast
>
Now, this statement is ungrounded. Where did you get this list from? Do
you serously think that dynamic_cast is less safe than static_cast? And
how did you judge safety of const_cast vs safety of reinterpret_cast?
How did you get const_cast more safe than static_cast and dynamic_cast?
For your education: const_cast is indeed safe when 'T' is casted to
'const T', but when 'const T' is casted to 'T' it can lead to UB,
assuming 'T' is a non-const type.
Read your own documentation:
https://en.cppreference.com/w/cpp/language/const_cast
> One can see their preference order base on how explicit C-style
> cast is being intrepreted by the C++ compiler [3].
>
> So actually the reinterpret_cast should be avoided when it is
> possible to use more strict casts, because reinterpret_cast
> disables all safety checks aside from constness and volatileness
> one.
>
Please show me how you'd write this code without reinterpret_cast and
without C-style cast since it will just implicitly use reinterpret_cast.
C-style cast is the least strict cast in C++ since it tries to apply
every cast except for dynamic_cast. I agree with you that stricter casts
should be applied, and thus there's no place for C-style casts.
> [1] Stanley B. Lippman, Josee Lajoie, C++ Primer. Chapter 4.14
> Types Conversion.
> [2] https://en.cppreference.com
> [3] https://en.cppreference.com/w/cpp/language/explicit_cast
>
> Best regards,
> Andrew Savchenko
>
>
> _______________________________________________
> Devel mailing list
> Devel@lists.altlinux.org
> https://lists.altlinux.org/mailman/listinfo/devel
>
next prev parent reply other threads:[~2019-12-13 8:01 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-06 13:16 [devel] [PATCH for apt 1/2] Add option for debugging DynamicMMap::Allocate Aleksei Nikiforov
2019-12-06 13:16 ` [devel] [PATCH for apt 2/2] Fix pointer arithmetics Aleksei Nikiforov
2019-12-06 13:36 ` Ivan A. Melnikov
2019-12-06 15:32 ` Aleksei Nikiforov
2019-12-06 15:36 ` [devel] [PATCH for apt 2/2 v2] " Aleksei Nikiforov
2019-12-07 14:52 ` Andrey Savchenko
2019-12-08 22:56 ` Dmitry V. Levin
2019-12-09 6:54 ` Aleksei Nikiforov
2019-12-12 19:20 ` Andrey Savchenko
2019-12-13 7:58 ` Aleksei Nikiforov
2019-12-08 23:21 ` Dmitry V. Levin
2019-12-09 7:08 ` Aleksei Nikiforov
2019-12-10 0:07 ` Dmitry V. Levin
2019-12-10 8:18 ` Aleksei Nikiforov
2019-12-10 10:20 ` Dmitry V. Levin
2019-12-10 10:58 ` Aleksei Nikiforov
2019-12-10 22:20 ` Dmitry V. Levin
2019-12-11 7:50 ` Aleksei Nikiforov
2019-12-12 19:43 ` Andrey Savchenko
2019-12-13 8:01 ` Aleksei Nikiforov [this message]
2019-12-08 23:31 ` [devel] [PATCH for apt 2/2] " Dmitry V. Levin
2019-12-09 7:09 ` Aleksei Nikiforov
2019-12-09 7:16 ` [devel] [PATCH for apt 2/2 v3] " Aleksei Nikiforov
2019-12-09 23:54 ` [devel] [PATCH apt 0/3] Fix 0.5.15lorg2-alt70~9 fallout Dmitry V. Levin
2019-12-09 23:56 ` [devel] [PATCH apt 1/3] apt-pkg/cacheiterators.h: revert #include <set> Dmitry V. Levin
2019-12-09 23:56 ` [devel] [PATCH apt 2/3] apt-pkg/contrib/mmap.cc: revert confusing change of Flags to this->Flags Dmitry V. Levin
2019-12-09 23:56 ` [devel] [PATCH apt 3/3] Fix UB in pointer arithmetic Dmitry V. Levin
2019-12-10 8:18 ` Aleksei Nikiforov
2019-12-08 22:50 ` [devel] [PATCH for apt 1/2] Add option for debugging DynamicMMap::Allocate Dmitry V. Levin
2019-12-09 6:58 ` Aleksei Nikiforov
2019-12-09 10:24 ` Dmitry V. Levin
2019-12-09 11:03 ` [devel] [PATCH for apt 1/2 v3] Add Debug::DynamicMMap::Allocate option Aleksei Nikiforov
2019-12-09 22:59 ` Dmitry V. Levin
2019-12-09 7:00 ` [devel] [PATCH for apt 1/2 v2] Add option for debugging Debug::DynamicMMap::Allocate Aleksei Nikiforov
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=32bfab23-32ca-9acb-d668-d1f9120646ae@altlinux.org \
--to=darktemplar@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