/ Check-in [4474d69b]
Login

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

Overview
Comment:Fix memory errors associated with argv in the CLI when it is compiled on Windows.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 4474d69b5c21b4e6f0d1376fbceca0f18c715ff673aea63053a02bfbe041d03b
User & Date: drh 2018-05-17 13:29:14
Context
2018-05-17
14:09
Improved error and help messages for the ".archive" command and "-A" option to the CLI. If a memory leak in --list processing. check-in: 02541ac6 user: drh tags: trunk
13:29
Fix memory errors associated with argv in the CLI when it is compiled on Windows. check-in: 4474d69b user: drh tags: trunk
2018-05-16
15:35
Enhance the sqlite3_str_new() interface so that it always returns a valid and non-NULL pointer even in an OOM condition. check-in: ed5b0968 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/shell.c.in.

  8281   8281     int i;
  8282   8282     int rc = 0;
  8283   8283     int warnInmemoryDb = 0;
  8284   8284     int readStdin = 1;
  8285   8285     int nCmd = 0;
  8286   8286     char **azCmd = 0;
  8287   8287     const char *zVfs = 0;           /* Value of -vfs command-line option */
         8288  +#if !SQLITE_SHELL_IS_UTF8
         8289  +  char **argvToFree = 0;
         8290  +  int argcToFree = 0;
         8291  +#endif
  8288   8292   
  8289   8293     setBinaryMode(stdin, 0);
  8290   8294     setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
  8291   8295     stdin_is_interactive = isatty(0);
  8292   8296     stdout_is_console = isatty(1);
  8293   8297   
  8294   8298   #if USE_SYSTEM_SQLITE+0!=1
................................................................................
  8304   8308     ** The SQLite memory allocator subsystem has to be enabled in order to
  8305   8309     ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
  8306   8310     ** subsequent sqlite3_config() calls will work.  So copy all results into
  8307   8311     ** memory that does not come from the SQLite memory allocator.
  8308   8312     */
  8309   8313   #if !SQLITE_SHELL_IS_UTF8
  8310   8314     sqlite3_initialize();
  8311         -  argv = malloc(sizeof(argv[0])*argc);
         8315  +  argvToFree = malloc(sizeof(argv[0])*argc*2);
         8316  +  argcToFree = argc;
         8317  +  argv = argvToFree + argc;
  8312   8318     if( argv==0 ) shell_out_of_memory();
  8313   8319     for(i=0; i<argc; i++){
  8314   8320       char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
  8315   8321       int n;
  8316   8322       if( z==0 ) shell_out_of_memory();
  8317   8323       n = (int)strlen(z);
  8318   8324       argv[i] = malloc( n+1 );
  8319   8325       if( argv[i]==0 ) shell_out_of_memory();
  8320   8326       memcpy(argv[i], z, n+1);
         8327  +    argvToFree[i] = argv[i];
  8321   8328       sqlite3_free(z);
  8322   8329     }
  8323   8330     sqlite3_shutdown();
  8324   8331   #endif
  8325   8332   
  8326   8333     assert( argc>=1 && argv && argv[0] );
  8327   8334     Argv0 = argv[0];
................................................................................
  8727   8734     }
  8728   8735     sqlite3_free(data.zFreeOnClose);
  8729   8736     find_home_dir(1);
  8730   8737     output_reset(&data);
  8731   8738     data.doXdgOpen = 0;
  8732   8739     clearTempFile(&data);
  8733   8740   #if !SQLITE_SHELL_IS_UTF8
  8734         -  for(i=0; i<argc; i++) free(argv[i]);
  8735         -  free(argv);
         8741  +  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
         8742  +  free(argvToFree);
  8736   8743   #endif
  8737   8744     return rc;
  8738   8745   }