Skip to content

Issues with IIS ReadFileChunk #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
keesspoelstra opened this issue Jan 7, 2013 · 3 comments
Closed

Issues with IIS ReadFileChunk #28

keesspoelstra opened this issue Jan 7, 2013 · 3 comments

Comments

@keesspoelstra
Copy link

Typing this off the top of my head :), so mind any typos.

IIS function HRESULT CMyHttpModule::ReadFileChunk(HTTP_DATA_CHUNK *chunk, char *buf)
fails in the following cases:

  • If the datachunk has an offset it will copy the first bytes of the block read instead of the bytes after the offset. This is behaviour seen in ARR, the cache will store headers and content in one file.
    Fix:
    memcpy(buf, pIoBuffer, bytesRead);
    to
    memcpy(buf, pIoBuffer+dwDataStartOffset, bytesRead);
  • Bytestotal is not honored in byte ranges, files with a byte range ending before EOF will copy to a whole page extra in buf, possibly overwriting other data (small risk if buf is allocated with a multiple of pagesize) bytesRead should be adjusted before the memcpy to stop it from copying the extra bytes.
    The guarding statement checking bytesRead against the length will not work with multiple page reads.
    Fix:
    if (bytesRead > chunk->FromFileHandle.ByteRange.Length.QuadPart) { bytesRead = (DWORD)chunk->FromFileHandle.ByteRange.Length.QuadPart; }
    if ((bytesTotal+bytesRead) > chunk->FromFileHandle.ByteRange.Length.QuadPart) { bytesRead = chunk->FromFileHandle.ByteRange.Length.QuadPart-bytesTotal; }
  • Allocation of the page is implicit, explicitly specifying the pagesize will make the code more robust.

Regards,
Kees

@gwroblew
Copy link
Contributor

Good bugs, I added the fixes, except for the page size. AFAIK the pattern we use right now, with checking the page size through system info, is the robust one, but I'll double check.

@keesspoelstra
Copy link
Author

Ok, I think that will work.
The problem with that one is that memory pagesize is not necessarely the same as the optimal disk page-sector size
Since windows 2008 there is a function GetFileInformationEx which can give the needed sector size for a given file handle.
I don´t think you will encounter many file systems which have a bigger sector size than memory pagesize.

@gwroblew
Copy link
Contributor

That piece of code is used in IIS modules that ship with the server or one of its components and it is quite old.

With SSDs becoming more popular and RAM still getting cheaper, would it make sense to allocate a multiply of the page size?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants