I am observing an interesting problem with Microsoft strncat implementation. It touches 1 byte behind the source buffer. Consider the following code:
#include <stdio.h> #include <stdlib.h> #include <memory.h> #include <string.h> void main() { char dstBuf[1024]; char* src = malloc(112); memset(src, 'a', 112); dstBuf[0] = 0; strncat(dstBuf, src, 112); }
strncat reads 1 byte after a 112-byte block. Therefore, if you are unlucky to get highlighted on an invalid page border, your application will crash. Large applications may intermittently interrupt in such places. (Note that this condition can be modeled using the gflags PageHeap parameter, the block size must be divided by the size of the pointer for proper alignment.)
Is this the expected behavior or error? Any links confirming this? (I read several strncat descriptions, but they can be interpreted in both directions depending on your initial mindset ...)
Refresh (to answer questions about evidence): I apologize if it is not clear from the text above, but this is an experimental fact. I observe intermittent crashes in the application at strncat read address src + srcBufSize. In this small example, it starts with gflags. PageHeap fails to play sequentially (100%). Therefore, as far as I can tell, the evidence is very strong.
Update2 (information about the compiler) MS Visual Studio 2005 Version 8.0.50727.867. Build Platform: 64-bit (no 32 bit playback). OS used to reproduce the failure: Windows Server 2008 R2.
Update 3 The problem is also reproduced using the binary code built into MS Visual Studio 2012 11.0.50727.1
Update 4 Link to a question in Microsoft Connect ; link to discussion on MSDN forums
Update 5 The problem will be fixed in the next version of VS. No fixes are planned for older versions. See the "Microsoft Connect" link above.
c pageheap
glagolig Aug 30 '13 at 3:36 on 2013-08-30 03:36
source share