Author: cfinck Date: Sun Apr 16 10:36:16 2017 New Revision: 74323
URL: http://svn.reactos.org/svn/reactos?rev=74323&view=rev Log: [SPOOLSS] Add ASSERTs, improve documentation and the variety of tests for AlignRpcPtr/UndoAlignRpcPtr. Based on comments by Serge Gautherie.
Modified: trunk/reactos/win32ss/printing/base/spoolss/memory.c trunk/rostests/apitests/spoolss/AlignRpcPtr.c
Modified: trunk/reactos/win32ss/printing/base/spoolss/memory.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/win32ss/printing/base/spool... ============================================================================== --- trunk/reactos/win32ss/printing/base/spoolss/memory.c [iso-8859-1] (original) +++ trunk/reactos/win32ss/printing/base/spoolss/memory.c [iso-8859-1] Sun Apr 16 10:36:16 2017 @@ -28,6 +28,8 @@ 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);
@@ -209,7 +211,7 @@ * 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. @@ -233,6 +235,9 @@ 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)
Modified: trunk/rostests/apitests/spoolss/AlignRpcPtr.c URL: http://svn.reactos.org/svn/reactos/trunk/rostests/apitests/spoolss/AlignRpcP... ============================================================================== --- trunk/rostests/apitests/spoolss/AlignRpcPtr.c [iso-8859-1] (original) +++ trunk/rostests/apitests/spoolss/AlignRpcPtr.c [iso-8859-1] Sun Apr 16 10:36:16 2017 @@ -54,13 +54,18 @@ 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"); @@ -68,14 +73,14 @@
// 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);