[dlls/dmusic/collection.c] Elimination of strncpy + bug

Peter Berg Larsen pebl at math.ku.dk
Fri Apr 15 17:03:01 CDT 2005


From
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_c/directx/htm/idirectmusiccollection8enuminstrument.asp
>>
pwszName
 Address of a buffer that receives the instrument name. Can be NULL if the
 name is not wanted.

dwNameLen
 Number of WCHAR elements in the instrument name buffer.
<<

The dwNameLen cannot be used to return the pwszName length, so it must be
'\0' terminated.

Changelog:
        Eliminate strncpy and only write to pwszName if not NULL.


Index: dlls/dmusic/collection.c
===================================================================
RCS file: /home/wine/wine/dlls/dmusic/collection.c,v
retrieving revision 1.12
diff -u -r1.12 collection.c
--- dlls/dmusic/collection.c    26 Jan 2005 19:41:43 -0000      1.12
+++ dlls/dmusic/collection.c    15 Apr 2005 20:13:59 -0000
@@ -131,6 +131,7 @@
        unsigned int r = 0;
        DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
        struct list *listEntry;
+       DWORD dwLen;

        TRACE("(%p, %ld, %p, %p, %ld)\n", This, dwIndex, pdwPatch, pwszName, dwNameLen);
        LIST_FOR_EACH (listEntry, &This->Instruments) {
@@ -138,8 +139,11 @@
                if (r == dwIndex) {
                        ICOM_NAME_MULTI (IDirectMusicInstrumentImpl, InstrumentVtbl, tmpEntry->pInstrument, pInstrument);
                        IDirectMusicInstrument_GetPatch (tmpEntry->pInstrument, pdwPatch);
-                       dwNameLen = strlenW (pInstrument->wszName);
-                       strncpyW (pwszName, pInstrument->wszName, dwNameLen);
+                       if (pwszName) {
+                               dwLen = min(strlenW(pInstrument->wszName),dwNameLen-1);
+                               memcpy (pwszName, pInstrument->wszName, dwLen * sizeof(WCHAR));
+                               pwszName[dwLen] = '\0';
+                       }
                        return S_OK;
                }
                r++;
@@ -190,11 +194,11 @@
        if (pDesc->dwValidData & DMUS_OBJ_CLASS)
                memcpy (&This->pDesc->guidClass, &pDesc->guidClass, sizeof (pDesc->guidClass));
        if (pDesc->dwValidData & DMUS_OBJ_NAME)
-               strncpyW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
+               lstrcpynW(This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
        if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
-               strncpyW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
+               lstrcpynW(This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
        if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
-               strncpyW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
+               lstrcpynW(This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
        if (pDesc->dwValidData & DMUS_OBJ_VERSION)
                memcpy (&This->pDesc->vVersion, &pDesc->vVersion, sizeof (pDesc->vVersion));
        if (pDesc->dwValidData & DMUS_OBJ_DATE)





More information about the wine-patches mailing list