/ Check-in [8966ec17]
Login

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

Overview
Comment:Remove use of malloc and free from the Windows VFS. Also, prevent sqlite3BtreeOpen from assuming that sqlite3OsFullPathname cannot fail.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | winSyscall
Files: files | file ages | folders
SHA1: 8966ec1797be63d1305628d459bdad5be08cf3ca
User & Date: mistachkin 2011-11-11 23:31:04
Context
2011-11-12
03:17
Make sure to flag benign malloc failures in the Windows VFS as such. Expand use of the DO_OS_MALLOC_TEST to cover the VFS functions that can now return an out of memory error. Support an experimental --match option to the test suite that will run only those tests matching the specified pattern. Closed-Leaf check-in: 76dec8aa user: mistachkin tags: winSyscall
2011-11-11
23:31
Remove use of malloc and free from the Windows VFS. Also, prevent sqlite3BtreeOpen from assuming that sqlite3OsFullPathname cannot fail. check-in: 8966ec17 user: mistachkin tags: winSyscall
22:08
Work in progress to implement the 'syscall' functionality for Windows. check-in: ed88fb00 user: mistachkin tags: winSyscall
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btree.c.

  1768   1768         char *zFullPathname = sqlite3Malloc(nFullPathname);
  1769   1769         MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
  1770   1770         p->sharable = 1;
  1771   1771         if( !zFullPathname ){
  1772   1772           sqlite3_free(p);
  1773   1773           return SQLITE_NOMEM;
  1774   1774         }
  1775         -      sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
         1775  +      rc = sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
         1776  +      if( rc!=SQLITE_OK ){
         1777  +        sqlite3_free(zFullPathname);
         1778  +        sqlite3_free(p);
         1779  +        return rc;
         1780  +      }
  1776   1781   #if SQLITE_THREADSAFE
  1777   1782         mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
  1778   1783         sqlite3_mutex_enter(mutexOpen);
  1779   1784         mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
  1780   1785         sqlite3_mutex_enter(mutexShared);
  1781   1786   #endif
  1782   1787         for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){

Changes to src/os_win.c.

    11     11   ******************************************************************************
    12     12   **
    13     13   ** This file contains code that is specific to Windows.
    14     14   */
    15     15   #include "sqliteInt.h"
    16     16   #if SQLITE_OS_WIN               /* This file is used for Windows only */
    17     17   
    18         -
    19         -/*
    20         -** A Note About Memory Allocation:
    21         -**
    22         -** This driver uses malloc()/free() directly rather than going through
    23         -** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
    24         -** are designed for use on embedded systems where memory is scarce and
    25         -** malloc failures happen frequently.  Win32 does not typically run on
    26         -** embedded systems, and when it does the developers normally have bigger
    27         -** problems to worry about than running out of memory.  So there is not
    28         -** a compelling need to use the wrappers.
    29         -**
    30         -** But there is a good reason to not use the wrappers.  If we use the
    31         -** wrappers then we will get simulated malloc() failures within this
    32         -** driver.  And that causes all kinds of problems for our tests.  We
    33         -** could enhance SQLite to deal with simulated malloc failures within
    34         -** the OS driver, but the code to deal with those failure would not
    35         -** be exercised on Linux (which does not need to malloc() in the driver)
    36         -** and so we would have difficulty writing coverage tests for that
    37         -** code.  Better to leave the code out, we think.
    38         -**
    39         -** The point of this discussion is as follows:  When creating a new
    40         -** OS layer for an embedded system, if you use this file as an example,
    41         -** avoid the use of malloc()/free().  Those routines work ok on Windows
    42         -** desktops but not so well in embedded systems.
    43         -*/
    44         -
    45     18   #ifdef __CYGWIN__
    46     19   # include <sys/cygwin.h>
    47     20   #endif
    48     21   
    49     22   /*
    50     23   ** Include code that is common to all os_*.c files
    51     24   */
................................................................................
   941    914   ** Space to hold the returned string is obtained from malloc.
   942    915   */
   943    916   static LPWSTR utf8ToUnicode(const char *zFilename){
   944    917     int nChar;
   945    918     LPWSTR zWideFilename;
   946    919   
   947    920     nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
   948         -  zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) );
          921  +  zWideFilename = sqlite3_malloc( nChar*sizeof(zWideFilename[0]) );
   949    922     if( zWideFilename==0 ){
   950    923       return 0;
   951    924     }
   952    925     nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
   953    926                                   nChar);
   954    927     if( nChar==0 ){
   955         -    free(zWideFilename);
          928  +    sqlite3_free(zWideFilename);
   956    929       zWideFilename = 0;
   957    930     }
   958    931     return zWideFilename;
   959    932   }
   960    933   
   961    934   /*
   962    935   ** Convert Microsoft Unicode to UTF-8.  Space to hold the returned string is
   963         -** obtained from malloc().
          936  +** obtained from sqlite3_malloc().
   964    937   */
   965    938   static char *unicodeToUtf8(LPCWSTR zWideFilename){
   966    939     int nByte;
   967    940     char *zFilename;
   968    941   
   969    942     nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
   970         -  zFilename = malloc( nByte );
          943  +  zFilename = sqlite3_malloc( nByte );
   971    944     if( zFilename==0 ){
   972    945       return 0;
   973    946     }
   974    947     nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
   975    948                                   0, 0);
   976    949     if( nByte == 0 ){
   977         -    free(zFilename);
          950  +    sqlite3_free(zFilename);
   978    951       zFilename = 0;
   979    952     }
   980    953     return zFilename;
   981    954   }
   982    955   
   983    956   /*
   984    957   ** Convert an ansi string to Microsoft Unicode, based on the
   985    958   ** current codepage settings for file apis.
   986    959   ** 
   987    960   ** Space to hold the returned string is obtained
   988         -** from malloc.
          961  +** from sqlite3_malloc.
   989    962   */
   990    963   static LPWSTR mbcsToUnicode(const char *zFilename){
   991    964     int nByte;
   992    965     LPWSTR zMbcsFilename;
   993    966     int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
   994    967   
   995    968     nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
   996    969                                   0)*sizeof(WCHAR);
   997         -  zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) );
          970  +  zMbcsFilename = sqlite3_malloc( nByte*sizeof(zMbcsFilename[0]) );
   998    971     if( zMbcsFilename==0 ){
   999    972       return 0;
  1000    973     }
  1001    974     nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
  1002    975                                   nByte);
  1003    976     if( nByte==0 ){
  1004         -    free(zMbcsFilename);
          977  +    sqlite3_free(zMbcsFilename);
  1005    978       zMbcsFilename = 0;
  1006    979     }
  1007    980     return zMbcsFilename;
  1008    981   }
  1009    982   
  1010    983   /*
  1011    984   ** Convert Microsoft Unicode to multi-byte character string, based on the
  1012    985   ** user's ANSI codepage.
  1013    986   **
  1014    987   ** Space to hold the returned string is obtained from
  1015         -** malloc().
          988  +** sqlite3_malloc().
  1016    989   */
  1017    990   static char *unicodeToMbcs(LPCWSTR zWideFilename){
  1018    991     int nByte;
  1019    992     char *zFilename;
  1020    993     int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
  1021    994   
  1022    995     nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
  1023         -  zFilename = malloc( nByte );
          996  +  zFilename = sqlite3_malloc( nByte );
  1024    997     if( zFilename==0 ){
  1025    998       return 0;
  1026    999     }
  1027   1000     nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
  1028   1001                                   nByte, 0, 0);
  1029   1002     if( nByte == 0 ){
  1030         -    free(zFilename);
         1003  +    sqlite3_free(zFilename);
  1031   1004       zFilename = 0;
  1032   1005     }
  1033   1006     return zFilename;
  1034   1007   }
  1035   1008   
  1036   1009   /*
  1037   1010   ** Convert multibyte character string to UTF-8.  Space to hold the
  1038         -** returned string is obtained from malloc().
         1011  +** returned string is obtained from sqlite3_malloc().
  1039   1012   */
  1040   1013   char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
  1041   1014     char *zFilenameUtf8;
  1042   1015     LPWSTR zTmpWide;
  1043   1016   
  1044   1017     zTmpWide = mbcsToUnicode(zFilename);
  1045   1018     if( zTmpWide==0 ){
  1046   1019       return 0;
  1047   1020     }
  1048   1021     zFilenameUtf8 = unicodeToUtf8(zTmpWide);
  1049         -  free(zTmpWide);
         1022  +  sqlite3_free(zTmpWide);
  1050   1023     return zFilenameUtf8;
  1051   1024   }
  1052   1025   
  1053   1026   /*
  1054   1027   ** Convert UTF-8 to multibyte character string.  Space to hold the 
  1055         -** returned string is obtained from malloc().
         1028  +** returned string is obtained from sqlite3_malloc().
  1056   1029   */
  1057   1030   char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
  1058   1031     char *zFilenameMbcs;
  1059   1032     LPWSTR zTmpWide;
  1060   1033   
  1061   1034     zTmpWide = utf8ToUnicode(zFilename);
  1062   1035     if( zTmpWide==0 ){
  1063   1036       return 0;
  1064   1037     }
  1065   1038     zFilenameMbcs = unicodeToMbcs(zTmpWide);
  1066         -  free(zTmpWide);
         1039  +  sqlite3_free(zTmpWide);
  1067   1040     return zFilenameMbcs;
  1068   1041   }
  1069   1042   
  1070   1043   
  1071   1044   /*
  1072   1045   ** The return value of getLastErrorMsg
  1073   1046   ** is zero if the error message fits in the buffer, or non-zero
................................................................................
  1124   1097     }
  1125   1098     if( 0 == dwLen ){
  1126   1099       sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", lastErrno, lastErrno);
  1127   1100     }else{
  1128   1101       /* copy a maximum of nBuf chars to output buffer */
  1129   1102       sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
  1130   1103       /* free the UTF8 buffer */
  1131         -    free(zOut);
         1104  +    sqlite3_free(zOut);
  1132   1105     }
  1133   1106     return 0;
  1134   1107   }
  1135   1108   
  1136   1109   /*
  1137   1110   **
  1138   1111   ** This function - winLogErrorAtLine() - is only ever called via the macro
................................................................................
  1270   1243   
  1271   1244   /*
  1272   1245   ** Create the mutex and shared memory used for locking in the file
  1273   1246   ** descriptor pFile
  1274   1247   */
  1275   1248   static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
  1276   1249     LPWSTR zTok;
  1277         -  LPWSTR zName = utf8ToUnicode(zFilename);
         1250  +  LPWSTR zName;
  1278   1251     BOOL bInit = TRUE;
         1252  +
         1253  +  zName = utf8ToUnicode(zFilename);
         1254  +  if( zName==0 ){
         1255  +    /* out of memory */
         1256  +    return FALSE;
         1257  +  }
  1279   1258   
  1280   1259     /* Initialize the local lockdata */
  1281   1260     memset(&pFile->local, 0, sizeof(pFile->local));
  1282   1261   
  1283   1262     /* Replace the backslashes from the filename and lowercase it
  1284   1263     ** to derive a mutex name. */
  1285   1264     zTok = osCharLowerW(zName);
................................................................................
  1288   1267     }
  1289   1268   
  1290   1269     /* Create/open the named mutex */
  1291   1270     pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
  1292   1271     if (!pFile->hMutex){
  1293   1272       pFile->lastErrno = osGetLastError();
  1294   1273       winLogError(SQLITE_ERROR, pFile->lastErrno, "winceCreateLock1", zFilename);
  1295         -    free(zName);
         1274  +    sqlite3_free(zName);
  1296   1275       return FALSE;
  1297   1276     }
  1298   1277   
  1299   1278     /* Acquire the mutex before continuing */
  1300   1279     winceMutexAcquire(pFile->hMutex);
  1301   1280     
  1302   1281     /* Since the names of named mutexes, semaphores, file mappings etc are 
................................................................................
  1310   1289   
  1311   1290     /* Set a flag that indicates we're the first to create the memory so it 
  1312   1291     ** must be zero-initialized */
  1313   1292     if (osGetLastError() == ERROR_ALREADY_EXISTS){
  1314   1293       bInit = FALSE;
  1315   1294     }
  1316   1295   
  1317         -  free(zName);
         1296  +  sqlite3_free(zName);
  1318   1297   
  1319   1298     /* If we succeeded in making the shared memory handle, map it. */
  1320   1299     if (pFile->hShared){
  1321   1300       pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared, 
  1322   1301                FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
  1323   1302       /* If mapping failed, close the shared memory handle and erase it */
  1324   1303       if (!pFile->shared){
................................................................................
  1608   1587       while(
  1609   1588              osDeleteFileW(pFile->zDeleteOnClose)==0
  1610   1589           && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff 
  1611   1590           && cnt++ < WINCE_DELETION_ATTEMPTS
  1612   1591       ){
  1613   1592          osSleep(100);  /* Wait a little before trying again */
  1614   1593       }
  1615         -    free(pFile->zDeleteOnClose);
         1594  +    sqlite3_free(pFile->zDeleteOnClose);
  1616   1595     }
  1617   1596   #endif
  1618   1597     OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
  1619   1598     OpenCounter(-1);
  1620   1599     return rc ? SQLITE_OK
  1621   1600               : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
  1622   1601                             "winClose", pFile->zPath);
................................................................................
  2436   2415   
  2437   2416     assert( pDbFd->pShm==0 );    /* Not previously opened */
  2438   2417   
  2439   2418     /* Allocate space for the new sqlite3_shm object.  Also speculatively
  2440   2419     ** allocate space for a new winShmNode and filename.
  2441   2420     */
  2442   2421     p = sqlite3_malloc( sizeof(*p) );
  2443         -  if( p==0 ) return SQLITE_NOMEM;
         2422  +  if( p==0 ) return SQLITE_IOERR_NOMEM;
  2444   2423     memset(p, 0, sizeof(*p));
  2445   2424     nName = sqlite3Strlen30(pDbFd->zPath);
  2446   2425     pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 15 );
  2447   2426     if( pNew==0 ){
  2448   2427       sqlite3_free(p);
  2449         -    return SQLITE_NOMEM;
         2428  +    return SQLITE_IOERR_NOMEM;
  2450   2429     }
  2451   2430     memset(pNew, 0, sizeof(*pNew));
  2452   2431     pNew->zFilename = (char*)&pNew[1];
  2453   2432     sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
  2454   2433     sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); 
  2455   2434   
  2456   2435     /* Look to see if there is an existing winShmNode that can be used.
................................................................................
  2470   2449       pNew = 0;
  2471   2450       ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
  2472   2451       pShmNode->pNext = winShmNodeList;
  2473   2452       winShmNodeList = pShmNode;
  2474   2453   
  2475   2454       pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  2476   2455       if( pShmNode->mutex==0 ){
  2477         -      rc = SQLITE_NOMEM;
         2456  +      rc = SQLITE_IOERR_NOMEM;
  2478   2457         goto shm_open_err;
  2479   2458       }
  2480   2459   
  2481   2460       rc = winOpen(pDbFd->pVfs,
  2482   2461                    pShmNode->zFilename,             /* Name of the file (UTF-8) */
  2483   2462                    (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
  2484   2463                    SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
................................................................................
  2920   2899     }else if( isNT() ){
  2921   2900       char *zMulti;
  2922   2901       WCHAR zWidePath[MAX_PATH];
  2923   2902       osGetTempPathW(MAX_PATH-30, zWidePath);
  2924   2903       zMulti = unicodeToUtf8(zWidePath);
  2925   2904       if( zMulti ){
  2926   2905         sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
  2927         -      free(zMulti);
         2906  +      sqlite3_free(zMulti);
  2928   2907       }else{
  2929         -      return SQLITE_NOMEM;
         2908  +      return SQLITE_IOERR_NOMEM;
  2930   2909       }
  2931   2910   /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
  2932   2911   ** Since the ASCII version of these Windows API do not exist for WINCE,
  2933   2912   ** it's important to not reference them for WINCE builds.
  2934   2913   */
  2935   2914   #if SQLITE_OS_WINCE==0
  2936   2915     }else{
  2937   2916       char *zUtf8;
  2938   2917       char zMbcsPath[MAX_PATH];
  2939   2918       osGetTempPathA(MAX_PATH-30, zMbcsPath);
  2940   2919       zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
  2941   2920       if( zUtf8 ){
  2942   2921         sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
  2943         -      free(zUtf8);
         2922  +      sqlite3_free(zUtf8);
  2944   2923       }else{
  2945         -      return SQLITE_NOMEM;
         2924  +      return SQLITE_IOERR_NOMEM;
  2946   2925       }
  2947   2926   #endif
  2948   2927     }
  2949   2928   
  2950   2929     /* Check that the output buffer is large enough for the temporary file 
  2951   2930     ** name. If it is not, return SQLITE_ERROR.
  2952   2931     */
................................................................................
  3061   3040       }
  3062   3041       zUtf8Name = zTmpname;
  3063   3042     }
  3064   3043   
  3065   3044     /* Convert the filename to the system encoding. */
  3066   3045     zConverted = convertUtf8Filename(zUtf8Name);
  3067   3046     if( zConverted==0 ){
  3068         -    return SQLITE_NOMEM;
         3047  +    return SQLITE_IOERR_NOMEM;
  3069   3048     }
  3070   3049   
  3071   3050     if( isReadWrite ){
  3072   3051       dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
  3073   3052     }else{
  3074   3053       dwDesiredAccess = GENERIC_READ;
  3075   3054     }
................................................................................
  3139   3118     OSTRACE(("OPEN %d %s 0x%lx %s\n", 
  3140   3119              h, zName, dwDesiredAccess, 
  3141   3120              h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
  3142   3121   
  3143   3122     if( h==INVALID_HANDLE_VALUE ){
  3144   3123       pFile->lastErrno = osGetLastError();
  3145   3124       winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
  3146         -    free(zConverted);
         3125  +    sqlite3_free(zConverted);
  3147   3126       if( isReadWrite && !isExclusive ){
  3148   3127         return winOpen(pVfs, zName, id, 
  3149   3128                ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
  3150   3129       }else{
  3151   3130         return SQLITE_CANTOPEN_BKPT;
  3152   3131       }
  3153   3132     }
................................................................................
  3170   3149     pFile->sectorSize = getSectorSize(pVfs, zUtf8Name);
  3171   3150   
  3172   3151   #if SQLITE_OS_WINCE
  3173   3152     if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
  3174   3153          && !winceCreateLock(zName, pFile)
  3175   3154     ){
  3176   3155       osCloseHandle(h);
  3177         -    free(zConverted);
         3156  +    sqlite3_free(zConverted);
  3178   3157       return SQLITE_CANTOPEN_BKPT;
  3179   3158     }
  3180   3159     if( isTemp ){
  3181   3160       pFile->zDeleteOnClose = zConverted;
  3182   3161     }else
  3183   3162   #endif
  3184   3163     {
  3185         -    free(zConverted);
         3164  +    sqlite3_free(zConverted);
  3186   3165     }
  3187   3166   
  3188   3167     OpenCounter(+1);
  3189   3168     return rc;
  3190   3169   }
  3191   3170   
  3192   3171   /*
................................................................................
  3211   3190     void *zConverted;
  3212   3191     UNUSED_PARAMETER(pVfs);
  3213   3192     UNUSED_PARAMETER(syncDir);
  3214   3193   
  3215   3194     SimulateIOError(return SQLITE_IOERR_DELETE);
  3216   3195     zConverted = convertUtf8Filename(zFilename);
  3217   3196     if( zConverted==0 ){
  3218         -    return SQLITE_NOMEM;
         3197  +    return SQLITE_IOERR_NOMEM;
  3219   3198     }
  3220   3199     if( isNT() ){
  3221   3200       rc = 1;
  3222   3201       while( osGetFileAttributesW(zConverted)!=INVALID_FILE_ATTRIBUTES &&
  3223   3202              (rc = osDeleteFileW(zConverted))==0 && retryIoerr(&cnt) ){}
  3224   3203       rc = rc ? SQLITE_OK : SQLITE_ERROR;
  3225   3204   /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
................................................................................
  3236   3215     }
  3237   3216     if( rc ){
  3238   3217       rc = winLogError(SQLITE_IOERR_DELETE, osGetLastError(),
  3239   3218                "winDelete", zFilename);
  3240   3219     }else{
  3241   3220       logIoerr(cnt);
  3242   3221     }
  3243         -  free(zConverted);
         3222  +  sqlite3_free(zConverted);
  3244   3223     OSTRACE(("DELETE \"%s\" %s\n", zFilename, (rc ? "failed" : "ok" )));
  3245   3224     return rc;
  3246   3225   }
  3247   3226   
  3248   3227   /*
  3249   3228   ** Check the existance and status of a file.
  3250   3229   */
................................................................................
  3258   3237     int rc = 0;
  3259   3238     void *zConverted;
  3260   3239     UNUSED_PARAMETER(pVfs);
  3261   3240   
  3262   3241     SimulateIOError( return SQLITE_IOERR_ACCESS; );
  3263   3242     zConverted = convertUtf8Filename(zFilename);
  3264   3243     if( zConverted==0 ){
  3265         -    return SQLITE_NOMEM;
         3244  +    return SQLITE_IOERR_NOMEM;
  3266   3245     }
  3267   3246     if( isNT() ){
  3268   3247       int cnt = 0;
  3269   3248       WIN32_FILE_ATTRIBUTE_DATA sAttrData;
  3270   3249       memset(&sAttrData, 0, sizeof(sAttrData));
  3271   3250       while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
  3272   3251                                GetFileExInfoStandard, 
................................................................................
  3283   3262           attr = sAttrData.dwFileAttributes;
  3284   3263         }
  3285   3264       }else{
  3286   3265         DWORD lastErrno = osGetLastError();
  3287   3266         logIoerr(cnt);
  3288   3267         if( lastErrno!=ERROR_FILE_NOT_FOUND ){
  3289   3268           winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
  3290         -        free(zConverted);
         3269  +        sqlite3_free(zConverted);
  3291   3270           return SQLITE_IOERR_ACCESS;
  3292   3271         }else{
  3293   3272           attr = INVALID_FILE_ATTRIBUTES;
  3294   3273         }
  3295   3274       }
  3296   3275   /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
  3297   3276   ** Since the ASCII version of these Windows API do not exist for WINCE,
................................................................................
  3298   3277   ** it's important to not reference them for WINCE builds.
  3299   3278   */
  3300   3279   #if SQLITE_OS_WINCE==0
  3301   3280     }else{
  3302   3281       attr = osGetFileAttributesA((char*)zConverted);
  3303   3282   #endif
  3304   3283     }
  3305         -  free(zConverted);
         3284  +  sqlite3_free(zConverted);
  3306   3285     switch( flags ){
  3307   3286       case SQLITE_ACCESS_READ:
  3308   3287       case SQLITE_ACCESS_EXISTS:
  3309   3288         rc = attr!=INVALID_FILE_ATTRIBUTES;
  3310   3289         break;
  3311   3290       case SQLITE_ACCESS_READWRITE:
  3312   3291         rc = attr!=INVALID_FILE_ATTRIBUTES &&
................................................................................
  3363   3342     ** using the io-error infrastructure to test that SQLite handles this
  3364   3343     ** function failing. This function could fail if, for example, the
  3365   3344     ** current working directory has been unlinked.
  3366   3345     */
  3367   3346     SimulateIOError( return SQLITE_ERROR );
  3368   3347     UNUSED_PARAMETER(nFull);
  3369   3348     zConverted = convertUtf8Filename(zRelative);
         3349  +  if( zConverted==0 ){
         3350  +    return SQLITE_IOERR_NOMEM;
         3351  +  }
  3370   3352     if( isNT() ){
  3371   3353       LPWSTR zTemp;
  3372   3354       nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0) + 3;
  3373         -    zTemp = malloc( nByte*sizeof(zTemp[0]) );
         3355  +    zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
  3374   3356       if( zTemp==0 ){
  3375         -      free(zConverted);
  3376         -      return SQLITE_NOMEM;
         3357  +      sqlite3_free(zConverted);
         3358  +      return SQLITE_IOERR_NOMEM;
  3377   3359       }
  3378   3360       osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
  3379         -    free(zConverted);
         3361  +    sqlite3_free(zConverted);
  3380   3362       zOut = unicodeToUtf8(zTemp);
  3381         -    free(zTemp);
         3363  +    sqlite3_free(zTemp);
  3382   3364   /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
  3383   3365   ** Since the ASCII version of these Windows API do not exist for WINCE,
  3384   3366   ** it's important to not reference them for WINCE builds.
  3385   3367   */
  3386   3368   #if SQLITE_OS_WINCE==0
  3387   3369     }else{
  3388   3370       char *zTemp;
  3389   3371       nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
  3390         -    zTemp = malloc( nByte*sizeof(zTemp[0]) );
         3372  +    zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
  3391   3373       if( zTemp==0 ){
  3392         -      free(zConverted);
  3393         -      return SQLITE_NOMEM;
         3374  +      sqlite3_free(zConverted);
         3375  +      return SQLITE_IOERR_NOMEM;
  3394   3376       }
  3395   3377       osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
  3396         -    free(zConverted);
         3378  +    sqlite3_free(zConverted);
  3397   3379       zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
  3398         -    free(zTemp);
         3380  +    sqlite3_free(zTemp);
  3399   3381   #endif
  3400   3382     }
  3401   3383     if( zOut ){
  3402   3384       sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
  3403         -    free(zOut);
         3385  +    sqlite3_free(zOut);
  3404   3386       return SQLITE_OK;
  3405   3387     }else{
  3406         -    return SQLITE_NOMEM;
         3388  +    return SQLITE_IOERR_NOMEM;
  3407   3389     }
  3408   3390   #endif
  3409   3391   }
  3410   3392   
  3411   3393   /*
  3412   3394   ** Get the sector size of the device used to store
  3413   3395   ** file.
................................................................................
  3464   3446           }
  3465   3447           dwRet = osGetDiskFreeSpaceA((char*)zConverted,
  3466   3448                                       &dwDummy,
  3467   3449                                       &bytesPerSector,
  3468   3450                                       &dwDummy,
  3469   3451                                       &dwDummy);
  3470   3452         }
  3471         -      free(zConverted);
         3453  +      sqlite3_free(zConverted);
  3472   3454       }
  3473   3455       if( !dwRet ){
  3474   3456         bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
  3475   3457       }
  3476   3458     }
  3477   3459   #endif
  3478   3460     return (int) bytesPerSector; 
................................................................................
  3501   3483   ** it's important to not reference them for WINCE builds.
  3502   3484   */
  3503   3485   #if SQLITE_OS_WINCE==0
  3504   3486     }else{
  3505   3487       h = osLoadLibraryA((char*)zConverted);
  3506   3488   #endif
  3507   3489     }
  3508         -  free(zConverted);
         3490  +  sqlite3_free(zConverted);
  3509   3491     return (void*)h;
  3510   3492   }
  3511   3493   static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
  3512   3494     UNUSED_PARAMETER(pVfs);
  3513   3495     getLastErrorMsg(osGetLastError(), nBuf, zBufOut);
  3514   3496   }
  3515   3497   static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){