Changeset 1836 for trunk/dll/filldir.c


Ignore:
Timestamp:
Jul 15, 2015, 2:48:14 AM (10 years ago)
Author:
Gregg Young
Message:

Fix trap in collector if a pci-pszFileName = NullStr and fix heap corruption problem and its related traps. This code reverts John double free fix. Ticket [551][555]

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/dll/filldir.c

    r1814 r1836  
    107107  28 Jun 14 GKY Fix errors identified with CPPCheck
    108108  30 Aug 14 GKY Use saymsg2 for Suggest dialog
     109  14 Jun 15 GKY Changes to prevent trap in Filter, heap corruption and traps associated with it
    109110  19 Jun 15 JBS Ticket 514: Double free fix (which also fixes a memory leak)
    110111
     
    153154#include "pathutil.h"                   // AddBackslashToPath
    154155#include "tmrsvcs.h"                    // ITIMER_DESC
     156#if 0
     157#define  __PMPRINTF__
     158#include "PMPRINTF.H"
     159#endif
    155160
    156161VOID StubbyScanThread(VOID * arg);
     
    424429  BOOL fLoadSubjectForDrive = fLoadSubject && ~flags & DRIVE_NOLOADSUBJS;
    425430  BOOL fLoadLongNameForDrive = fLoadLongnames &&  //~flags & DRIVE_NOLONGNAMES &&
    426                                ~flags & DRIVE_NOLOADLONGS;
     431                               ~flags & DRIVE_NOLOADLONGS;
    427432  if (fLoadSubjectForDrive || fLoadLongNameForDrive) {
    428433    // Allocate space to hold GEA2s and .SUBJECT and .LONGNAME strings
    429     PGEA2LIST pgealist = xmallocz(sizeof(GEA2LIST) + sizeof(GEA2) + 64, pszSrcFile, __LINE__);
     434      PGEA2LIST pgealist = xmallocz(sizeof(GEA2LIST) + (sizeof(GEA2) * 2) + 32,
     435                                    pszSrcFile, __LINE__);
    430436    if (pgealist) {
    431437      APIRET rc;
     
    466472      //DbgMsg(pszSrcFile, __LINE__, "pgealist %p cbList %u", pgealist, pgealist->cbList);
    467473
    468       pfealist = xmallocz(4096, pszSrcFile, __LINE__);
     474      pfealist = xmallocz(65536, pszSrcFile, __LINE__);
    469475      if (pfealist) {
    470         pfealist->cbList = 4096;
     476        pfealist->cbList = 65536;
    471477        eaop.fpGEA2List = pgealist;
    472478        eaop.fpFEA2List = pfealist;
     
    514520            pfea = (PFEA2)((PSZ)pfea + pfea->oNextEntryOffset);
    515521          } // while
    516         }
     522        }
    517523        free(pfealist);
    518524      }
     
    688694  pci->rc.hptrIcon = hptr;
    689695
     696
    690697  // check to see if record should be visible
     698  DosRequestMutexSem(hmtxFiltering, SEM_INDEFINITE_WAIT);
    691699  if (dcd && (*dcd->mask.szMask || dcd->mask.antiattr ||
    692700              ((dcd->mask.attrFile &
    693701                (FILE_HIDDEN | FILE_SYSTEM | FILE_READONLY | FILE_ARCHIVED))
    694                !=
    695                (FILE_HIDDEN | FILE_SYSTEM | FILE_READONLY | FILE_ARCHIVED))))
    696   {
     702               != (FILE_HIDDEN | FILE_SYSTEM | FILE_READONLY | FILE_ARCHIVED)))) {
    697703    if (*dcd->mask.szMask || dcd->mask.antiattr) {
    698704      if (!Filter((PMINIRECORDCORE) pci, (PVOID) & dcd->mask))
    699         pci->rc.flRecordAttr |= CRA_FILTERED;
     705        pci->rc.flRecordAttr |= CRA_FILTERED;
    700706    }
    701707    else if ((!(dcd->mask.attrFile & FILE_HIDDEN) &&
     
    710716    }
    711717  }
     718  DosReleaseMutexSem(hmtxFiltering);
    712719
    713720  return pffb->cbFile + pci->easize;
     
    851858  pci->rc.hptrIcon = hptr;
    852859
     860  DosRequestMutexSem(hmtxFiltering, SEM_INDEFINITE_WAIT);
    853861  if (dcd &&
    854862      (*dcd->mask.szMask || dcd->mask.antiattr ||
     
    858866    if (*dcd->mask.szMask || dcd->mask.antiattr) {
    859867      if (!Filter((PMINIRECORDCORE) pci, (PVOID) & dcd->mask))
    860         pci->rc.flRecordAttr |= CRA_FILTERED;
     868        pci->rc.flRecordAttr |= CRA_FILTERED;
    861869    }
    862870    else if ((!(dcd->mask.attrFile & FILE_HIDDEN) &&
     
    870878      pci->rc.flRecordAttr |= CRA_FILTERED;
    871879  }
     880  DosReleaseMutexSem(hmtxFiltering);
    872881
    873882  return pfsa4->cbFile + pci->easize;
     
    968977        pffbFile = paffbFound;
    969978        ulSelCnt = 0;
    970         for (;;) {
     979        for (;;) {
    971980          if (!*pffbFile->achName ||
    972981              (!filestoo && ~pffbFile->attrFile & FILE_DIRECTORY) ||
     
    989998          if (stopflag && *stopflag)
    990999            goto Abort;
    991           if (fSyncUpdates) {
     1000          if (fSyncUpdates) {
     1001            if (!WinIsWindow(WinQueryAnchorBlock(hwndCnr), hwndCnr)) {
     1002              ok = FALSE;
     1003              ullTotalBytes = 0;
     1004              break;
     1005            }
    9921006            pciFirst = WinSendMsg(hwndCnr, CM_ALLOCRECORD,
    9931007                                  MPFROMLONG(EXTRA_RECORD_BYTES),
     
    10051019              // Finish filling pci items
    10061020              for (x = 0; x < ulSelCnt; x++) {
    1007                 pffbFile = papffbSelected[x];
     1021                pffbFile = papffbSelected[x];
    10081022                ullBytes = FillInRecordFromFFB(hwndCnr, pci, pszFileSpec,
    10091023                                               pffbFile, partial, dcd);
     
    10391053            if (ok) {
    10401054              ullReturnBytes += ullTotalBytes;
    1041               ulReturnFiles += ulSelCnt;
     1055              ulReturnFiles += ulSelCnt;
     1056              DosRequestMutexSem(hmtxFiltering, SEM_INDEFINITE_WAIT);
    10421057              if (dcd) {
    10431058                dcd->totalfiles += ulSelCnt;
    10441059                dcd->ullTotalBytes += ullTotalBytes;
    1045               }
     1060              }
     1061              DosReleaseMutexSem(hmtxFiltering);
    10461062            }
    10471063          } // if sync updates
     
    10981114          ULONG ulRecsToInsert;
    10991115
    1100           if (pci ==NULL) {
     1116          if (pci ==NULL) {
     1117            if (!WinIsWindow(WinQueryAnchorBlock(hwndCnr), hwndCnr)) {
     1118              ok = FALSE;
     1119              ullTotalBytes = 0;
     1120              break;
     1121            }
    11011122            ulRecsToInsert = cAffbTotal - x <  USHRT_MAX  ? cAffbTotal - x : USHRT_MAX;
    11021123            pciFirst = WinSendMsg(hwndCnr, CM_ALLOCRECORD,
    11031124                                  MPFROMLONG(EXTRA_RECORD_BYTES), MPFROMLONG(ulRecsToInsert));
    11041125
    1105             if (!pciFirst) {
     1126            if (!pciFirst) {
     1127              //ERRORID erridErrorCode = WinGetLastError(WinQueryAnchorBlock(hwndCnr));
     1128              //PmpfF(("Allocation failed %i", erridErrorCode));
    11061129              Win_Error(hwndCnr, HWND_DESKTOP, pszSrcFile, __LINE__,
    11071130                        GetPString(IDS_CMALLOCRECERRTEXT));
     
    11181141              pci = pciFirst;
    11191142            }
    1120           }
     1143          }
    11211144          ullBytes = FillInRecordFromFFB(hwndCnr, pci, pszFileSpec,
    11221145                                         pffbFile, partial, dcd);
    11231146          pci = (PCNRITEM) pci->rc.preccNextRecord;
    11241147          ullTotalBytes += ullBytes;
    1125           // 15 Sep 09 SHL allow timed updates to see
     1148          // 15 Sep 09 SHL allow timed updates to see
     1149          DosRequestMutexSem(hmtxFiltering, SEM_INDEFINITE_WAIT);
    11261150          if (dcd) {
    11271151            dcd->totalfiles = x;
    11281152            dcd->ullTotalBytes = ullTotalBytes;
    1129           }
     1153          }
     1154          DosReleaseMutexSem(hmtxFiltering);
    11301155          // Can not use offset since we have merged lists - this should be equivalent
    11311156          pffbFile = (PFILEFINDBUF4L)((PBYTE)pffbFile + sizeof(FILEFINDBUF4L));
    11321157
    1133           if (!IdleIfNeeded(&itdSleep, 30)) {
    1134             for (x = x+1; x < cAffbTotal; x++) {
     1158          if (!IdleIfNeeded(&itdSleep, 30)) {
     1159            for (x = x+1; x < cAffbTotal; x++) {
    11351160              ullBytes = FillInRecordFromFFB(hwndCnr, pci, pszFileSpec,
    11361161                                          pffbFile, partial, dcd);
    11371162              pci = (PCNRITEM) pci->rc.preccNextRecord;
    1138               ullTotalBytes += ullBytes;
     1163              ullTotalBytes += ullBytes;
     1164              DosRequestMutexSem(hmtxFiltering, SEM_INDEFINITE_WAIT);
    11391165              if (dcd) {
    11401166                dcd->totalfiles = x;
    11411167                dcd->ullTotalBytes = ullTotalBytes;
    1142               }
    1143               pffbFile = (PFILEFINDBUF4L)((PBYTE)pffbFile + sizeof(FILEFINDBUF4L));
     1168              }
     1169              DosReleaseMutexSem(hmtxFiltering);
     1170              pffbFile = (PFILEFINDBUF4L)((PBYTE)pffbFile + sizeof(FILEFINDBUF4L));
    11441171              if (pci == NULL) {
    11451172                priority_normal();
     
    18321859VOID FreeCnrItemData(PCNRITEM pci)
    18331860{
     1861 
    18341862  if (pci->pszSubject) {
    18351863    if (pci->pszSubject != NullStr)
     
    18371865    pci->pszSubject = NULL;             // Catch illegal references
    18381866  }
    1839 
     1867 
    18401868  // 08 Sep 08 SHL Remove excess logic
    18411869  if (pci->pszLongName) {
     
    18441872    pci->pszLongName = NULL;            // Catch illegal references
    18451873  }
    1846 
     1874 
    18471875  // Bypass free if pszDisplayName points into pszFileName buffer
    18481876  // 05 Sep 08 SHL Correct pointer overlap compare logic
     
    18531881          pci->pszDisplayName >= pci->pszFileName + _msize(pci->pszFileName))
    18541882      {
    1855         free(pci->pszDisplayName);
     1883        free(pci->pszDisplayName);
    18561884      }
    18571885    }
     
    18591887  }
    18601888
    1861   if (pci->pszFileName) {
    1862     if (pci->pszFileName != NullStr) {
     1889#if 0 // 26 Sep 09 SHL debug dup free complaints
     1890  if (!pci->pszFileName)
     1891    Runtime_Error(pszSrcFile, __LINE__, "FreeCnrItemData attempting to free %p data twice", pci);
     1892  else {
     1893    if (pci->pszFileName != NullStr)
    18631894      free(pci->pszFileName);
    1864     }
    1865     pci->pszFileName = NULL;            // Catch illegal references and dup free attempts
    1866   }
    1867 
    1868 // 26 Mar 14 JBS Twice on the longname?
     1895    pci->pszFileName = NULL;            // Catch illegal references
     1896  }
     1897#else
     1898  {
     1899    #define HIST_COUNT 50
     1900    static struct {
     1901      PCNRITEM pci;
     1902      PSZ pszFileName;
     1903    } history[HIST_COUNT];
     1904    static volatile UINT iHistNdx;
     1905    UINT i;
     1906
     1907    if (!pci->pszFileName) {
     1908      // Looks like pci was already freed
     1909      // Try to locate original file name in history buffer
     1910      for (i = 0; i < HIST_COUNT && pci != history[i].pci; i++) { }     // Scan
     1911      if (i < HIST_COUNT) {
     1912        Runtime_Error(pszSrcFile, __LINE__, "FreeCnrItemData attempting to free %p data twice, fileName was %.260s",
     1913                      pci, history[i].pszFileName);
     1914      } else {
     1915        Runtime_Error(pszSrcFile, __LINE__, "FreeCnrItemData attempting to free %p data twice", pci);
     1916      }
     1917    }
     1918    else {
     1919      PSZ psz;
     1920      PSZ *ppsz;
     1921      // Grab a history slot
     1922      // This should work well enoungh to prevent MT/SMP conflicts
     1923      for (;;) {
     1924        i = iHistNdx;
     1925        if (++i >= HIST_COUNT)
     1926          i = 0;
     1927        if (++iHistNdx >= HIST_COUNT)
     1928          iHistNdx = 0;
     1929        if (i == iHistNdx) break;
     1930      }
     1931      ppsz = &history[iHistNdx].pszFileName;
     1932      psz = *ppsz;
     1933      if (psz)
     1934        free(psz);
     1935      if (pci->pszFileName && pci->pszFileName != NullStr)
     1936        *ppsz = strdup(pci->pszFileName);
     1937      else
     1938        *ppsz = NULL;
     1939      history[iHistNdx].pci = pci;
     1940      if (pci->pszFileName != NullStr)
     1941        free(pci->pszFileName);
     1942      pci->pszFileName = NULL;          // Catch illegal references and dup free attempts
     1943    }
     1944  }
     1945
     1946
     1947#endif
     1948
    18691949  // 08 Sep 08 SHL Remove excess logic
    18701950  if (pci->pszLongName) {
     
    19432023{
    19442024  INT remaining = usCnt;
     2025  BOOL bIdlePrioritySet = FALSE;
     2026// #define RCI_ITEMS_PER_TIMER_CHECK (10)
     2027// 10 seems a very conservative number
     2028//   USHORT usTimerCheckCountdown       = RCI_ITEMS_PER_TIMER_CHECK;
    19452029  PCNRITEM pci;
    19462030  ITIMER_DESC itdSleep = { 0 };         // 30 May 11 GKY
     
    19662050      InitITimer(&itdSleep, 500);
    19672051      while (pci) {
    1968         FreeCnrItemData(pci);
     2052        FreeCnrItemData(pci);
     2053        pci = (PCNRITEM)pci->rc.preccNextRecord;
     2054        if (!pci)
     2055          break;
    19692056        if (remaining && --remaining == 0)
    19702057          break;
    1971         pci = (PCNRITEM)WinSendMsg(hwnd, CM_QUERYRECORD, (MPARAM)pci,
    1972                                    MPFROM2SHORT(CMA_NEXT, CMA_ITEMORDER));
    1973         if (pci && !IdleIfNeeded(&itdSleep, 30)) {
    1974           // Finishing in idle mode
    1975           while (pci) {
    1976             FreeCnrItemData(pci);
    1977             if (remaining && --remaining == 0)
    1978               break;
    1979             pci = (PCNRITEM)WinSendMsg(hwnd, CM_QUERYRECORD, (MPARAM)pci,
    1980                                    MPFROM2SHORT(CMA_NEXT, CMA_ITEMORDER));
    1981 
    1982           }
    1983           priority_normal();
    1984           break;
     2058        if (!bIdlePrioritySet /* && --usTimerCheckCountdown == 0 */) {
     2059          bIdlePrioritySet = !IdleIfNeeded(&itdSleep, 30);
     2060//           usTimerCheckCountdown = RCI_ITEMS_PER_TIMER_CHECK;
    19852061        }
    19862062      } // while
     2063      if (bIdlePrioritySet)
     2064        priority_normal();
     2065
    19872066      DosPostEventSem(CompactSem);
    19882067    }
    19892068  }
     2069
     2070  // DbgMsg(pszSrcFile, __LINE__, "RemoveCnrItems %p %u %s", pci, usCnt, pci->pszFileName);
    19902071
    19912072  if (remaining != - 1) {
     
    19952076    }
    19962077  }
     2078
    19972079  return remaining;
    19982080}
Note: See TracChangeset for help on using the changeset viewer.