You may know that the strlen() function computes the length of the string 
*without" the \0 char !

Example :
if buffer = "AAAAA[...]AAA" = 0x2001
buffer_size = strlen(buffer) = 0x2000.
apr_mem_remap(Map, buffer_size+1) = will map 0x3000 see apr_mem_pager().
memcpy(Map->base, buffer, buffer_size);
strlen(Map->base) = 0x2000

Regards,
Michael Vergoz

Le Jeudi 30 Juin 2005 00:41, Bruce Korb a écrit :
> Michael Vergoz wrote:
>  > Hi Bruce,
>  >
>  > Yes
>
> Thank you!
>
>  > because you must align the page to the biggest you can.
>
> ??  The problem is is that no matter how you align the thing,
> if the last byte of the file is the last byte of a page,
> then the next byte is unmapped.  Consequently, when "strlen(3C)"
> tries to determine if the next byte after the last valid byte
> is NUL or not, it faults on a bad memory reference.  There
> are several solutions:
>
> Punt mmap and just read the file into an allocated buffer.
>
> mmap the file then figure out if the last byte is the last
> byte of a page.  If so, then mmap anonymous memory into
> the fixed address following the last page of the text file.
> I do this, but it is a real pain.  I hate it.  :-)  Most
> often, I just allocate and read the file 'cuz it is easier.
>
>  > Have a look at : http://badcode.be/~descript/.apache/
>  >
>  > Into function apr_mem_pager() :
>  > if you need 0x2001 (Size).
>  > <snip>
>  >    ret = size; // ret = 0x2001
>  >    if((ret%_apr_mem_page_size) != 0) { // 0x0001
>  >            ret -= (ret%_apr_mem_page_size); // ret -= 0x0001 = 0x2000
>  >            ret += _apr_mem_page_size*map->pager; // ret += 0x1000*PAGER
>  >    }
>  > </snip>
>
> Right.  If "size" is 0x2000 exactly, the byte following the
> mapping will be unmapped and "strlen(3C)" will fault.
> That's the problem.$
>
>  > You may have a look at apr_mem_remap(). It will allow you to ask for a
>  > unaligned address. Then the engine will auto update the address
>  > alignment with a predefined pager (defined by the pager argument in
>  > apr_mem_map()).
>
> OK.  So, if I check the file size and it is a multiple of a page size,
> then I should ask for non-aligned mapping, otherwise an aligned mapping?
> Better than messing around with anonymous pages on my own, but still.....
>
> Thanks.  Regards, Bruce

Reply via email to