This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: how to get mmap page size?


Jay Foad wrote on Friday, January 16, 2009 12:24 PM::

> I have an application that wants to use mmap() to read a file, but
> only if it can guarantee that this will leave one or more zero bytes
> after the end of the contents of the file in memory:
> 
>   if ((filesize & (pagesize - 1) != 0)
>     use_mmap();
>   else
>     use_read();
> 

FYI, this is not good practice (although I fully understand why you
might want to do it).  From the mmap man page on an OSF1 UNIX box at 
work:

| If the len parameter is not a multiple of the page size returned by
| the sysconf(_SC_PAGE_SIZE) call, then the result of any reference to
| an address between the end of the region and the end of the page 
| containing the end of the region is undefined.

and:

|  +  If the end of the mapped file region is beyond the end of the 
|     file, the result of any reference to an address in the mapped 
|     file region corresponding to an offset beyond the end of the 
|     file is unspecified.

In other words, you're relying on system dependent behaviour if you
reference memory beyond the end of the file.  Your requirement
"only if it can guarantee that this will leave one or more zero bytes
after the end of the contents" cannot be satisfied if system
independence is required (or may be in future).  If you ever build 
your code on other systems, any bugs due to this would probably be 
really tough to track down, may be quite subtle, and may pose a
security risk.


Phil
-- 


This email has been scanned by Ascribe PLC using Microsoft Antigen for Exchange.

--
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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]