[devel] [PATCH for apt 2/2 v2] Fix pointer arithmetics

Dmitry V. Levin ldv на altlinux.org
Пн Дек 9 02:21:08 MSK 2019


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?

[...]
> @@ -301,7 +302,7 @@ std::experimental::optional<map_ptrloc> DynamicMMap::Allocate(unsigned long Item
>        Pool* oldPools = Pools;
>        auto idxResult = RawAllocate(I->Count*ItemSize,ItemSize);
>        if (Pools != oldPools)
> -         I += Pools - oldPools;
> +         I = RebasePointer(I, oldPools, Pools);
>  
>        // Does the allocation failed ?
>        if (!idxResult)

In my patch RebasePointer invocation was after the idxResult check,
not before the check.

By the way, in this and other similar cases,
is there any reason for "Pools != oldPools" check?
Is RebasePointer incapable of handling this, or is it an optimization?

[...]
> diff --git a/apt/apt-pkg/rebase_pointer.h b/apt/apt-pkg/rebase_pointer.h
> new file mode 100644
> index 0000000..f6b3c15
> --- /dev/null
> +++ b/apt/apt-pkg/rebase_pointer.h
> @@ -0,0 +1,16 @@
> +#ifndef PKGLIB_REBASE_POINTER_H
> +#define PKGLIB_REBASE_POINTER_H
> +
> +template <typename T>
> +static inline T* RebasePointer(T *ptr, void *old_base, void *new_base)
> +{
> +   return reinterpret_cast<T*>(reinterpret_cast<char*>(new_base) + (reinterpret_cast<char*>(ptr) - reinterpret_cast<char*>(old_base)));
> +}
> +
> +template <typename T>
> +static inline const T* RebasePointer(const T *ptr, void *old_base, void *new_base)
> +{
> +   return reinterpret_cast<const T*>(reinterpret_cast<char*>(new_base) + (reinterpret_cast<const char*>(ptr) - reinterpret_cast<char*>(old_base)));
> +}
> +
> +#endif

Do we really need two templates here?


-- 
ldv
----------- следующая часть -----------
Было удалено вложение не в текстовом формате...
Имя     : signature.asc
Тип     : application/pgp-signature
Размер  : 801 байтов
Описание: отсутствует
Url     : <http://lists.altlinux.org/pipermail/devel/attachments/20191209/bae0b059/attachment-0001.bin>


Подробная информация о списке рассылки Devel