From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7489 invoked by alias); 26 Jun 2003 00:59:59 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 2904 invoked from network); 25 Jun 2003 22:56:43 -0000 Received: from unknown (HELO slinky.cs.nyu.edu) (128.122.20.14) by sources.redhat.com with SMTP; 25 Jun 2003 22:56:43 -0000 Received: from localhost (pechtcha@localhost) by slinky.cs.nyu.edu (8.11.7+Sun/8.11.7) with ESMTP id h5PMuhS27746 for ; Wed, 25 Jun 2003 18:56:43 -0400 (EDT) X-Authentication-Warning: slinky.cs.nyu.edu: pechtcha owned process doing -bs Date: Thu, 26 Jun 2003 05:06:00 -0000 From: Igor Pechtchanski Reply-To: cygwin@cygwin.com To: cygwin@cygwin.com Subject: Re: assertion "ptr != MAP_FAILED" failed while using mmap In-Reply-To: Message-ID: Importance: Normal MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-SW-Source: 2003-06/txt/msg01196.txt.bz2 On Wed, 25 Jun 2003, Elfyn McBratney wrote: > On Wed, 25 Jun 2003, Ronald Landheer-Cieslak wrote: > > > On Wed, 25 Jun 2003, Elfyn McBratney wrote: > > > > > On Wed, 25 Jun 2003, Alex Vinokur wrote: > > > > > > > =========================================== > > > > Windows 2000 > > > > CYGWIN_NT-5.0 1.3.22(0.78/3/2) > > > > GNU gcc version 3.2 20020927 (prerelease) > > > > =========================================== > > > > > > > > Here is some 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); > > > > > > > > assert (ptr != MAP_FAILED); // Here assertion failed > > > > if (ptr != MAP_FAILED) > > > > { > > > > string str(ptr, ptr+sz); > > > > munmap(ptr, sz); > > > > } > > > > > > > > close(fd); > > > > } > > > > -------------------------------------- > > > > > > > > Assertion "ptr != MAP_FAILED)" failed. > > > > What might cause that? > > > > > > This is just a stab in the dark, of course, but surely `ptr != MAP_FAILED' > > > would indicate that the mmap did not fail? Assertions (assert()) are based on > > > true or false, so the above assert is false in that `ptr != MAP_FAILED'. > > > > > > Elfyn > > > > Ehm.. > > > > If ptr != MAP_FAILED is not true, that means ptr == MAP_FAILED. > > assert(ptr != MAP_FAILED) thus fails if mmap fails.. > > > > unless I'm missing something.. > > But what's the point in 'assert (ptr != NULL)'? Surely the best way to test for > failure is the other way round (s/!=/==/), if ptr == MAP_FAILED assert, or > continue...No? > > Elfyn 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 -- http://cs.nyu.edu/~pechtcha/ |\ _,,,---,,_ pechtcha@cs.nyu.edu ZZZzz /,`.-'`' -. ;-;;,_ igor@watson.ibm.com |,4- ) )-,_. ,\ ( `'-' Igor Pechtchanski, Ph.D. '---''(_/--' `-'\_) fL a.k.a JaguaR-R-R-r-r-r-.-.-. Meow! "I have since come to realize that being between your mentor and his route to the bathroom is a major career booster." -- Patrick Naughton -- 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/