[dlls/cabinet/cabextract.c] Strncpy elimination + bug

Peter Berg Larsen pebl at math.ku.dk
Fri Apr 15 16:35:08 CDT 2005


In find_cabinet_file SearchPatchA is called and the return value is used
as indicator whether a path was found (is actually how many bytes in the
path). But the actual path is not returned if the the buffer given is to
small, yet there is no test for this. Is it because this cannot happen? It
also seems risky to do the +1 on the found variable. Secondly the filepart
is not used nor needed.

Patch in the buttom.

Changelog:
          Eliminate a strncpy and remove unused variable.



void find_cabinet_file(char **cabname, LPCSTR origcab) {
    ....
  if ((cab = (char *) malloc(MAX_PATH))) {
    ...

    do {
      nextpart = strchr(name, '\\');
      if (nextpart) *nextpart = '\0';

      found = SearchPathA(cab, name, NULL, MAX_PATH, nametmp, &filepart);

      if (nextpart) *nextpart = '\\', name = &nextpart[1];

    } while (nextpart && found);

    if (found) {
      free((void *) *cabname);
      *cabname = cab;
      strncpy(cab, nametmp, found+1);
      TRACE("result: %s\n", debugstr_a(cab));
    } else {
      free((void *) cab);
      TRACE("result: nothing\n");
    }
}




 * SearchPath

 * RETURNS
 *    Success: length of string copied into buffer, not including
 *             terminating null character. If the filename found is
 *             longer than the length of the buffer, the length of the
 *             filename is returned.
 *    Failure: Zero
 *
DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext,
                          DWORD buflen, LPSTR buffer, LPSTR *lastpart )
{
    WCHAR *pathW, *nameW = NULL, *extW = NULL;
    WCHAR bufferW[MAX_PATH];
    DWORD ret;

    if (name && !(nameW = FILE_name_AtoW( name, FALSE ))) return 0;
    if (!(pathW = FILE_name_AtoW( path, TRUE ))) return 0;
    if (ext && !(extW = FILE_name_AtoW( ext, TRUE )))
    {
        HeapFree( GetProcessHeap(), 0, pathW );
        return 0;
    }

    ret = SearchPathW(pathW, nameW, extW, MAX_PATH, bufferW, NULL);

    HeapFree( GetProcessHeap(), 0, pathW );
    HeapFree( GetProcessHeap(), 0, extW );

    if (!ret) return 0;
    if (ret > MAX_PATH)
    {
        SetLastError(ERROR_FILENAME_EXCED_RANGE);
        return 0;
    }
    ret = copy_filename_WtoA( bufferW, buffer, buflen );
    if (buflen > ret && lastpart)
        *lastpart = strrchr(buffer, '\\') + 1;
    return ret;
}


/***********************************************************************
 *           copy_filename_WtoA
 *
 * copy a file name back to OEM/Ansi, but only if the buffer is large enough
 */

...




Index: dlls/cabinet/cabextract.c
===================================================================
RCS file: /home/wine/wine/dlls/cabinet/cabextract.c,v
retrieving revision 1.17
diff -u -r1.17 cabextract.c
--- dlls/cabinet/cabextract.c   22 Dec 2004 17:11:31 -0000      1.17
+++ dlls/cabinet/cabextract.c   15 Apr 2005 20:13:44 -0000
@@ -2068,7 +2068,7 @@
  */
 void find_cabinet_file(char **cabname, LPCSTR origcab) {

-  char *tail, *cab, *name, *nextpart, nametmp[MAX_PATH], *filepart;
+  char *tail, *cab, *name, *nextpart, nametmp[MAX_PATH];
   int found = 0;

   TRACE("(*cabname == ^%p, origcab == %s)\n", cabname ? *cabname : NULL, debugstr_a(origcab));
@@ -2107,11 +2107,11 @@
       nextpart = strchr(name, '\\');
       if (nextpart) *nextpart = '\0';

-      found = SearchPathA(cab, name, NULL, MAX_PATH, nametmp, &filepart);
+      found = SearchPathA(cab, name, NULL, MAX_PATH, nametmp, NULL);

       /* if the component was not found, look for it in the current dir */
       if (!found) {
-        found = SearchPathA(".", name, NULL, MAX_PATH, nametmp, &filepart);
+        found = SearchPathA(".", name, NULL, MAX_PATH, nametmp, NULL);
       }

       if (found)
@@ -2135,7 +2135,7 @@
     if (found) {
       free((void *) *cabname);
       *cabname = cab;
-      strncpy(cab, nametmp, found+1);
+      memcpy(cab, nametmp, found+1);
       TRACE("result: %s\n", debugstr_a(cab));
     } else {
       free((void *) cab);





More information about the wine-patches mailing list