/ Check-in [cdbca259]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Improve memory allocation error handling on WinCE.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: cdbca259da80df901837034d2616da434cb82216
User & Date: drh 2013-02-12 22:20:01
Context
2013-02-13
01:00
Allow an index to be used for sorting even if prior terms of the index are constrained by IN operators. check-in: 2cef8b68 user: drh tags: trunk
2013-02-12
22:20
Improve memory allocation error handling on WinCE. check-in: cdbca259 user: drh tags: trunk
22:09
A bug report coming from from Firefox (https://bugzilla.mozilla.org/show_bug.cgi?id=840377) describes an infinite recursion. We are unable to reproduce the problem. Nevertheless, this check-in should prevent the problem from ever occurring again. check-in: b7222a2b user: drh tags: trunk
09:46
Improve memory allocation error handling on WinCE. Closed-Leaf check-in: 09dfc0c9 user: mistachkin tags: wince
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/os_win.c.

   984    984   }
   985    985   
   986    986   /*
   987    987   ** This function outputs the specified (ANSI) string to the Win32 debugger
   988    988   ** (if available).
   989    989   */
   990    990   
   991         -void sqlite3_win32_write_debug(char *zBuf, int nBuf){
          991  +void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
   992    992     char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
   993    993     int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
   994    994     if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
   995    995     assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
   996    996   #if defined(SQLITE_WIN32_HAS_ANSI)
   997    997     if( nMin>0 ){
   998    998       memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
................................................................................
  1666   1666   */
  1667   1667   #define winceMutexRelease(h) ReleaseMutex(h)
  1668   1668   
  1669   1669   /*
  1670   1670   ** Create the mutex and shared memory used for locking in the file
  1671   1671   ** descriptor pFile
  1672   1672   */
  1673         -static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
         1673  +static int winceCreateLock(const char *zFilename, winFile *pFile){
  1674   1674     LPWSTR zTok;
  1675   1675     LPWSTR zName;
         1676  +  DWORD lastErrno;
         1677  +  BOOL bLogged = FALSE;
  1676   1678     BOOL bInit = TRUE;
  1677   1679   
  1678   1680     zName = utf8ToUnicode(zFilename);
  1679   1681     if( zName==0 ){
  1680   1682       /* out of memory */
  1681         -    return FALSE;
         1683  +    return SQLITE_IOERR_NOMEM;
  1682   1684     }
  1683   1685   
  1684   1686     /* Initialize the local lockdata */
  1685   1687     memset(&pFile->local, 0, sizeof(pFile->local));
  1686   1688   
  1687   1689     /* Replace the backslashes from the filename and lowercase it
  1688   1690     ** to derive a mutex name. */
................................................................................
  1691   1693       if (*zTok == '\\') *zTok = '_';
  1692   1694     }
  1693   1695   
  1694   1696     /* Create/open the named mutex */
  1695   1697     pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
  1696   1698     if (!pFile->hMutex){
  1697   1699       pFile->lastErrno = osGetLastError();
  1698         -    winLogError(SQLITE_ERROR, pFile->lastErrno, "winceCreateLock1", zFilename);
         1700  +    winLogError(SQLITE_IOERR, pFile->lastErrno,
         1701  +                "winceCreateLock1", zFilename);
  1699   1702       sqlite3_free(zName);
  1700         -    return FALSE;
         1703  +    return SQLITE_IOERR;
  1701   1704     }
  1702   1705   
  1703   1706     /* Acquire the mutex before continuing */
  1704   1707     winceMutexAcquire(pFile->hMutex);
  1705   1708     
  1706   1709     /* Since the names of named mutexes, semaphores, file mappings etc are 
  1707   1710     ** case-sensitive, take advantage of that by uppercasing the mutex name
................................................................................
  1710   1713     osCharUpperW(zName);
  1711   1714     pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
  1712   1715                                           PAGE_READWRITE, 0, sizeof(winceLock),
  1713   1716                                           zName);  
  1714   1717   
  1715   1718     /* Set a flag that indicates we're the first to create the memory so it 
  1716   1719     ** must be zero-initialized */
  1717         -  if (osGetLastError() == ERROR_ALREADY_EXISTS){
         1720  +  lastErrno = osGetLastError();
         1721  +  if (lastErrno == ERROR_ALREADY_EXISTS){
  1718   1722       bInit = FALSE;
  1719   1723     }
  1720   1724   
  1721   1725     sqlite3_free(zName);
  1722   1726   
  1723   1727     /* If we succeeded in making the shared memory handle, map it. */
  1724         -  if (pFile->hShared){
         1728  +  if( pFile->hShared ){
  1725   1729       pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared, 
  1726   1730                FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
  1727   1731       /* If mapping failed, close the shared memory handle and erase it */
  1728         -    if (!pFile->shared){
         1732  +    if( !pFile->shared ){
  1729   1733         pFile->lastErrno = osGetLastError();
  1730         -      winLogError(SQLITE_ERROR, pFile->lastErrno,
  1731         -               "winceCreateLock2", zFilename);
         1734  +      winLogError(SQLITE_IOERR, pFile->lastErrno,
         1735  +                  "winceCreateLock2", zFilename);
         1736  +      bLogged = TRUE;
  1732   1737         osCloseHandle(pFile->hShared);
  1733   1738         pFile->hShared = NULL;
  1734   1739       }
  1735   1740     }
  1736   1741   
  1737   1742     /* If shared memory could not be created, then close the mutex and fail */
  1738         -  if (pFile->hShared == NULL){
         1743  +  if( pFile->hShared==NULL ){
         1744  +    if( !bLogged ){
         1745  +      pFile->lastErrno = lastErrno;
         1746  +      winLogError(SQLITE_IOERR, pFile->lastErrno,
         1747  +                  "winceCreateLock3", zFilename);
         1748  +      bLogged = TRUE;
         1749  +    }
  1739   1750       winceMutexRelease(pFile->hMutex);
  1740   1751       osCloseHandle(pFile->hMutex);
  1741   1752       pFile->hMutex = NULL;
  1742         -    return FALSE;
         1753  +    return SQLITE_IOERR;
  1743   1754     }
  1744   1755     
  1745   1756     /* Initialize the shared memory if we're supposed to */
  1746         -  if (bInit) {
         1757  +  if( bInit ){
  1747   1758       memset(pFile->shared, 0, sizeof(winceLock));
  1748   1759     }
  1749   1760   
  1750   1761     winceMutexRelease(pFile->hMutex);
  1751         -  return TRUE;
         1762  +  return SQLITE_OK;
  1752   1763   }
  1753   1764   
  1754   1765   /*
  1755   1766   ** Destroy the part of winFile that deals with wince locks
  1756   1767   */
  1757   1768   static void winceDestroyLock(winFile *pFile){
  1758   1769     if (pFile->hMutex){
................................................................................
  2753   2764           win32IoerrRetryDelay = a[1];
  2754   2765         }else{
  2755   2766           a[1] = win32IoerrRetryDelay;
  2756   2767         }
  2757   2768         return SQLITE_OK;
  2758   2769       }
  2759   2770       case SQLITE_FCNTL_TEMPFILENAME: {
  2760         -      char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
         2771  +      char *zTFile = sqlite3MallocZero( pFile->pVfs->mxPathname );
  2761   2772         if( zTFile ){
  2762   2773           getTempname(pFile->pVfs->mxPathname, zTFile);
  2763   2774           *(char**)pArg = zTFile;
  2764   2775         }
  2765   2776         return SQLITE_OK;
  2766   2777       }
  2767   2778     }
................................................................................
  3689   3700     pFile->h = INVALID_HANDLE_VALUE;
  3690   3701   
  3691   3702     /* If the second argument to this function is NULL, generate a 
  3692   3703     ** temporary file name to use 
  3693   3704     */
  3694   3705     if( !zUtf8Name ){
  3695   3706       assert(isDelete && !isOpenJournal);
         3707  +    memset(zTmpname, 0, MAX_PATH+2);
  3696   3708       rc = getTempname(MAX_PATH+2, zTmpname);
  3697   3709       if( rc!=SQLITE_OK ){
  3698   3710         return rc;
  3699   3711       }
  3700   3712       zUtf8Name = zTmpname;
  3701   3713     }
  3702   3714   
................................................................................
  3840   3852     pFile->zPath = zName;
  3841   3853     if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
  3842   3854       pFile->ctrlFlags |= WINFILE_PSOW;
  3843   3855     }
  3844   3856   
  3845   3857   #if SQLITE_OS_WINCE
  3846   3858     if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
  3847         -       && !winceCreateLock(zName, pFile)
         3859  +       && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
  3848   3860     ){
  3849   3861       osCloseHandle(h);
  3850   3862       sqlite3_free(zConverted);
  3851         -    return SQLITE_CANTOPEN_BKPT;
         3863  +    return rc;
  3852   3864     }
  3853   3865     if( isTemp ){
  3854   3866       pFile->zDeleteOnClose = zConverted;
  3855   3867     }else
  3856   3868   #endif
  3857   3869     {
  3858   3870       sqlite3_free(zConverted);