delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2003/06/26/01:07:12

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
X-Injected-Via-Gmane: http://gmane.org/
To: cygwin AT cygwin DOT com
From: "Alex Vinokur" <alexvn AT connect DOT to>
Subject: Re: assertion "ptr != MAP_FAILED" failed while using mmap
Date: Thu, 26 Jun 2003 07:00:31 +0300
Lines: 59
Message-ID: <bddr4g$on$1@main.gmane.org>
References: <Pine DOT CYG DOT 4 DOT 55 DOT 0306251825330 DOT 3408 AT ellixia> <Pine DOT GSO DOT 4 DOT 44 DOT 0306251849140 DOT 22307-100000 AT slinky DOT cs DOT nyu DOT edu>
X-Complaints-To: usenet AT main DOT gmane DOT org
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200

"Igor Pechtchanski" <pechtcha AT cs DOT nyu DOT edu> wrote in message news:Pine DOT GSO DOT 4 DOT 44 DOT 0306251849140 DOT 22307-100000 AT slinky DOT cs DOT nyu DOT edu...
[snip]
>
> Umm, Elfyn, the semantics of "assert()" is that you assert some predicate
> to be true.  If the predicate is indeed true, the program continues
> normally.  If the predicate is false, the program fails.
> The predicate in this case is "ptr != MAP_FAILED".  Thus, the predicate
> was false when the assertion failed, and ptr == MAP_FAILED.
>
> To the OP: this means that mmap() did fail for some reason.  It should
> have set errno to indicate why.  You should check that instead of
> asserting -- mmap does fail occasionally.  Also FYI, once you assert, the
> following test for "ptr != MAP_FAILED" is redundant -- the program will
> not get there otherwise.
> Igor
[snip]


Here is updated function.
--------------------------------------
void read_file (char* filename_i)
{
int fd = open(filename_i, O_RDONLY);
  assert (fd > 2);

off_t sz = lseek(fd, 0, SEEK_END);
char* ptr = (char*)mmap(0, sz, PROT_READ, 0, fd, 0);

   errno = 0;
  if (ptr != MAP_FAILED)
  {
    string str(ptr, ptr+sz);
    munmap(ptr, sz);
  }
  else
  {
    assert (ptr == MAP_FAILED);
    printf ("=== Error : %u %s ===\n", errno, strerror (errno));
  }
  assert (ptr != MAP_FAILED);  // Here assertion failed

  close(fd);
}
--------------------------------------

The program prints:
=== Error : 0 No error ===

Regards,
   =====================================
   Alex Vinokur
     mailto:alexvn AT connect DOT to
     http://mathforum.org/library/view/10978.html
   =====================================






--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019