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>
Cc: darktemplar@altlinux.org
Subject: Re: [devel] re APT patch with impossible error on "Can't allocate an item of size zero"
Date: Thu, 13 Feb 2020 05:43:42 +0300 (MSK)
Message-ID: <alpine.LFD.2.20.2002130540580.6363@imap.altlinux.org> (raw)
In-Reply-To: <alpine.LFD.2.20.2002130502010.6363@imap.altlinux.org>

[-- Attachment #1: Type: text/plain, Size: 4248 bytes --]


On Thu, 13 Feb 2020, Ivan Zakharyaschev wrote:

> В ветке sisyphus_one_more_time в
> git://git.altlinux.org/people/darktemplar/packages/apt.git в коммите
> 
> commit f53c1af17e2997ff6d2e23139bc61f5d5c82a547

> есть такой hunk касательно DynamicMMap::RawAllocate():

> Во-вторых, я подумал, что это такой класс ошибок, который не возникает
> в run-time из-за плохих данных или отказа ОС (например, выделить
> память или прочитать файл) -- т.е. не что-то заранее непредсказуемое
> для программиста и поэтому требующее специальной обработки в run-time.
> 
> Если програмист написал всё так, как он хотел, то он не передаст
> неправильный аргумент (0) в функцию. (Да и понятно, что в реальном
> коде APT это не ожидается: там обычно этот аргумент заполняется
> константой sizeof(...).)
> 
> Я бы предложил такие требования (имеющие характер спецификации того,
> что программист задумал реализовать) оформлять просто с помощью
> assert.

Отразил это в коммите у себя в ветке, которая должна будет 
конвергироваться с sisyphus_one_more_time

commit ab3d7b79f9a582d4b01efad5e9fdf1a0455f735f
Author: Ivan Zakharyaschev <imz@altlinux.org>
Date:   Wed Jan 29 21:40:53 2020 +0300

    DynamicMMap::RawAllocate(): assert that a zero-size item is never allocated
    
    Actually, we are always called with sizeof(...)
    compile-time non-zero constant as the argument:
    
    The only invocation of Allocate() that can be found with:
    
    git grep -nH -Ee '(^|[^[:alpha:]])Allocate\('
    
    is the following function:
    
    std::experimental::optional<map_ptrloc> pkgCacheGenerator::AllocateInMap(unsigned long size) {/*{{{*/
       void *oldMap = Map.Data();
       const auto index = Map.Allocate(size);
       if (index)
          ReMap(oldMap, Map.Data());
       return index;
    }
    
    Now, look how AllocateInMap() is used:
    
    git --no-pager grep -nH -Ee '(^|[^[:alpha:]])AllocateInMap\('
    apt/apt-pkg/pkgcachegen.cc:167:std::experimental::optional<map_ptrloc> pkgCacheGenerator::AllocateInMap(unsigned long size) {/*{{{*/
    apt/apt-pkg/pkgcachegen.cc:449:   const auto Package = AllocateInMap(sizeof(pkgCache::Package));
    apt/apt-pkg/pkgcachegen.cc:502:   const auto VerFile = AllocateInMap(sizeof(pkgCache::VerFile));
    apt/apt-pkg/pkgcachegen.cc:533:   const auto Version = AllocateInMap(sizeof(pkgCache::Version));
    apt/apt-pkg/pkgcachegen.cc:561:   const auto Dependency = Owner->AllocateInMap(sizeof(pkgCache::Dependency));
    apt/apt-pkg/pkgcachegen.cc:639:   const auto Provides = Owner->AllocateInMap(sizeof(pkgCache::Provides));
    apt/apt-pkg/pkgcachegen.cc:686:   const auto idxFile = AllocateInMap(sizeof(*CurrentFile));
    apt/apt-pkg/pkgcachegen.cc:744:   const auto Item = AllocateInMap(sizeof(pkgCache::StringItem));
    apt/apt-pkg/pkgcachegen.h:45:   std::experimental::optional<map_ptrloc> AllocateInMap(unsigned long size);
    
    The argument is always a sizeof(...) constant expression.
    
    Picked the idea of this single change from:
    
        commit 6d5e6a689d07de8feef2cbecb24bc42d5994861b
        Author: Aleksei Nikiforov <darktemplar@altlinux.org>
        Date:   Tue Jun 11 14:53:47 2019 +0300
    
        apt-pkg/pkgcachegen.{cc,h} changes
    
        make the used MMap moveable (and therefore dynamic resizeable) by
        applying (some) mad pointer magic
    
        Ported from Apt from Debian
    
    but rewritten with an assert statement.

diff --git a/apt/apt-pkg/contrib/mmap.cc b/apt/apt-pkg/contrib/mmap.cc
index 77aad3656..e0373365a 100644
--- a/apt/apt-pkg/contrib/mmap.cc
+++ b/apt/apt-pkg/contrib/mmap.cc
@@ -37,6 +37,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <cstring>
+#include <cassert>
    									/*}}}*/
 
 // MMap::MMap - Constructor						/*{{{*/
@@ -226,6 +227,10 @@ std::experimental::optional<unsigned long> DynamicMMap::RawAllocate(unsigned lon
    size in the file. */
 std::experimental::optional<unsigned long> DynamicMMap::Allocate(unsigned long ItemSize)
 {
+   assert(ItemSize != 0); /* Actually, we are always called with sizeof(...)
+                             compile-time non-zero constant as the argument.
+                          */
+
    // Look for a matching pool entry
    Pool *I;
    Pool *Empty = 0;

  reply	other threads:[~2020-02-13  2:43 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-13  2:08 Ivan Zakharyaschev
2020-02-13  2:43 ` Ivan Zakharyaschev [this message]
2020-02-13 12:59 ` Aleksei Nikiforov
2020-02-13 14:10   ` Ivan Zakharyaschev
2020-02-13 17:05     ` Ivan Zakharyaschev
2020-02-17  8:41     ` Ivan Zakharyaschev
2020-02-17 14:05       ` Andrey Savchenko
2020-02-17 17:48         ` Ivan Zakharyaschev
2020-02-17 21:35       ` Dmitry V. Levin
2020-02-17 21:58         ` Ivan Zakharyaschev
2020-02-13 14:51   ` Ivan Zakharyaschev

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.2002130540580.6363@imap.altlinux.org \
    --to=imz@altlinux.org \
    --cc=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