PVOID WINAPI
AlignRpcPtr(PVOID pBuffer, PDWORD pcbBuffer)
{
+ ASSERT(pcbBuffer);
+
// Align down the buffer size in pcbBuffer to a 4-byte boundary.
*pcbBuffer -= *pcbBuffer % sizeof(DWORD);
* The original unaligned buffer, which you input as pBuffer to AlignRpcPtr.
* The data from pSourceBuffer is copied into this buffer before pSourceBuffer is freed.
* If AlignRpcPtr did not allocate a buffer, pDestinationBuffer equals pSourceBuffer and no memory is copied or freed.
- * This parameter may be NULL if pSourceBuffer is NULL.
+ * This parameter may be NULL if pSourceBuffer is NULL or cbBuffer is 0.
*
* @param pSourceBuffer
* The aligned buffer, which is returned by AlignRpcPtr.
PDWORD WINAPI
UndoAlignRpcPtr(PVOID pDestinationBuffer, PVOID pSourceBuffer, DWORD cbBuffer, PDWORD pcbNeeded)
{
+ // pDestinationBuffer is accessed unless pSourceBuffer equals pDestinationBuffer or cbBuffer is 0.
+ ASSERT(pDestinationBuffer || pSourceBuffer == pDestinationBuffer || cbBuffer == 0);
+
// If pSourceBuffer is given, and source and destination pointers don't match,
// we assume that pSourceBuffer is the buffer allocated by AlignRpcPtr.
if (pSourceBuffer && pSourceBuffer != pDestinationBuffer)
ok(pOutputBuffer != pInputBuffer, "pOutputBuffer == pInputBuffer\n");
ok(cbBuffer == 4, "cbBuffer is %lu\n", cbBuffer);
+ // Prove that AlignRpcPtr also works with a NULL buffer. The size should be aligned down.
+ cbBuffer = 6;
+ ok(!AlignRpcPtr(NULL, &cbBuffer), "AlignRpcPtr returns something\n");
+ ok(cbBuffer == 4, "cbBuffer is %lu\n", cbBuffer);
+
// We can also test all parameters of UndoAlignRpcPtr here.
// Because pOutputBuffer != pInputBuffer, it copies the given 4 bytes from (aligned) pOutputBuffer to (unaligned) pInputBuffer
// while aligning up the given 7 bytes in our passed &cbBuffer.
// &cbBuffer is also returned.
strcpy(pOutputBuffer, "abc");
strcpy(pInputBuffer, "XXXXXXXXX");
- cbBuffer = 7;
+ cbBuffer = 5;
pcbBuffer = UndoAlignRpcPtr(pInputBuffer, pOutputBuffer, 4, &cbBuffer);
ok(strcmp(pInputBuffer, "abc") == 0, "pInputBuffer is %s\n", pInputBuffer);
ok(pcbBuffer == &cbBuffer, "pcbBuffer != &cbBuffer\n");
// Prove that UndoAlignRpcPtr works without any parameters and doesn't try to copy data from NULL pointers.
ok(!UndoAlignRpcPtr(NULL, NULL, 0, NULL), "UndoAlignRpcPtr returns something\n");
- ok(!UndoAlignRpcPtr(NULL, NULL, 4, NULL), "UndoAlignRpcPtr returns something\n");
+ ok(!UndoAlignRpcPtr(NULL, NULL, 6, NULL), "UndoAlignRpcPtr returns something\n");
// Prove that UndoAlignRpcPtr doesn't access source and destination memory at all when they are equal.
// If it did, it should crash here, because I'm giving invalid memory addresses.
ok(!UndoAlignRpcPtr((PVOID)1, (PVOID)1, 4, NULL), "UndoAlignRpcPtr returns something\n");
- // Prove that the pcbNeeded parameter of UndoAlignRpcPtr works independently and aligns up everything up to a DWORD.
- cbBuffer = 0xFFFFFFFF;
+ // Prove that the pcbNeeded parameter of UndoAlignRpcPtr works independently and aligns up to a DWORD.
+ cbBuffer = 0xFFFFFFFD;
pcbBuffer = UndoAlignRpcPtr(NULL, NULL, 0, &cbBuffer);
ok(pcbBuffer == &cbBuffer, "pcbBuffer != &cbBuffer\n");
ok(cbBuffer == 0, "cbBuffer is %lu\n", cbBuffer);