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