Ignore:
Timestamp:
Apr 29, 2002, 7:05:30 PM (23 years ago)
Author:
sandervl
Message:

PH: memory leak fixes; extra checks for NULL pointers; CreateProcess changes for debug option; GetModuleFileName doesn't count 0 terminator

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/wprocess.cpp

    r8015 r8327  
    1 /* $Id: wprocess.cpp,v 1.149 2002-02-26 11:11:17 sandervl Exp $ */
     1/* $Id: wprocess.cpp,v 1.150 2002-04-29 17:05:30 sandervl Exp $ */
    22
    33/*
     
    340340        }
    341341        threadListMutex.leave();
    342 
     342     
     343        // PH 2002-04-11
     344        // free allocated memory
     345        free( winteb->o.odin.threadinfo.pTokenGroups );
     346     
     347#ifdef DEBUG
     348        if (winteb->o.odin.arrstrCallStack != NULL)
     349          free( winteb->o.odin.arrstrCallStack );
     350#endif
     351     
    343352        //Restore our original FS selector
    344353        SetFS(orgtibsel);
     
    12031212         */
    12041213        cch = strlen(pszPeExe)+1;
     1214     
     1215        // PH 2002-04-11
     1216        // Note: intentional memory leak, pszCmdLineW will not be freed
     1217        // or allocated after process startup
    12051218        pszCmdLineA = psz = (PSZ)malloc(cch);
    12061219        if (psz == NULL)
     
    13041317    if (rc == NO_ERROR)
    13051318    {
     1319      // PH 2002-04-11
     1320      // Note: intentional memory leak, pszCmdLineW will not be freed
     1321      // or allocated after process startup
    13061322        pszCmdLineW = (WCHAR*)malloc(cch * 2);
    13071323        if (pszCmdLineW != NULL)
     
    14021418 *              terminating '\0'.
    14031419 *              On error 0 is returned. Last error is set.
     1420 *
     1421 *              2002-04-25 PH
     1422 *              Q - Do we set ERROR_BUFFER_OVERFLOW when cch > cchPath?
     1423 *              Q - Does NT really set the last error?
     1424 *              A > Win2k does not set LastError here, remains OK
     1425 *
     1426 *              While GetModuleFileName does add a trailing termination zero
     1427 *              if there is enough room, the returned number of characters
     1428 *              *MUST NOT* include the zero character!
     1429 *              (Notes R6 Installer on Win2kSP6, verified Testcase)
     1430 *
    14041431 * @param       hModule     Handle to the module you like to get the file name to.
    14051432 * @param       lpszPath    Output buffer for full path and file name.
     
    14211448 * @author      knut st. osmundsen (knut.stange.osmundsen@mynd.no)
    14221449 *              Sander van Leeuwen (sandervl@xs4all.nl)
     1450 *              Patrick Haller     (patrick.haller@innotek.de)
    14231451 * @remark      - Do we still have to call Open32?
    1424  *              - Do we set ERROR_BUFFER_OVERFLOW when cch > cchPath?
    1425  *              - Does NT really set the last error?
    14261452 */
    14271453DWORD WIN32API GetModuleFileNameA(HMODULE hModule, LPTSTR lpszPath, DWORD cchPath)
    14281454{
    14291455    Win32ImageBase *    pMod;           /* Pointer to the module object. */
    1430     DWORD               cch;            /* Length of the  */
    1431 
     1456    DWORD               cch = 0;        /* Length of the  */
     1457 
     1458    // PH 2002-04-24 Note:
     1459    // WIN2k just crashes in NTDLL if lpszPath is invalid!
    14321460    if (!VALID_PSZ(lpszPath))
    14331461    {
     
    14441472        if (pszFn)
    14451473        {
    1446             cch = strlen(pszFn) + 1;
    1447             if (cch > cchPath)
    1448                 cch = cchPath;
     1474            cch = strlen(pszFn);
     1475            if (cch >= cchPath)
     1476              cch = cchPath;
     1477            else
     1478              // if there is sufficient room for the zero termination,
     1479              // write it additionally, uncounted
     1480              lpszPath[cch] = '\0';
     1481             
    14491482            memcpy(lpszPath, pszFn, cch);
    1450             lpszPath[cch - 1] = '\0';
    14511483        }
    14521484        else
     
    14631495        //(console init only it seems...)
    14641496        cch = OSLibDosGetModuleFileName(hModule, lpszPath, cchPath);
    1465         if (cch > 0)    cch++;          /* Open32 doesn't count the terminator. */
    14661497    }
    14671498
     
    17941825
    17951826    DWORD Characteristics, SubSystem, fNEExe;
    1796     if(Win32ImageBase::isPEImage(szAppName, &Characteristics, &SubSystem, &fNEExe) == 0) {
    1797         char *lpszPE;
    1798         if(SubSystem == IMAGE_SUBSYSTEM_WINDOWS_CUI) {
    1799              lpszPE = "PEC.EXE";
    1800         }
    1801         else lpszPE = "PE.EXE";
    1802 
     1827    if(Win32ImageBase::isPEImage(szAppName, &Characteristics, &SubSystem, &fNEExe) == 0)
     1828    {
     1829      char *lpszPE;
     1830      char *lpszExecutable;
     1831      int  iNewCommandLineLength;
     1832
     1833      // calculate base length for the new command line
     1834      iNewCommandLineLength = strlen(szAppName) + strlen(lpCommandLine);
     1835     
     1836      if(SubSystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
     1837        lpszExecutable = "PEC.EXE";
     1838      else
     1839        lpszExecutable = "PE.EXE";
     1840     
     1841      lpszPE = lpszExecutable;
     1842     
     1843      // 2002-04-24 PH
     1844      // set the ODIN32.DEBUG_CHILD environment variable to start new PE processes
     1845      // under a new instance of the (IPMD) debugger.
     1846#ifdef DEBUG
     1847      CHAR debug_szPE[ 512 ];
     1848      PSZ debug_pszOS2Debugger  = getenv("ODIN32.DEBUG_CHILD");
     1849      if (NULL != debug_pszOS2Debugger)
     1850      {
     1851        // build new start command
     1852        strcpy(debug_szPE, debug_pszOS2Debugger);
     1853        strcat(debug_szPE, " ");
     1854        strcat(debug_szPE, lpszExecutable);
     1855       
     1856        // we require more space in the new command line
     1857        iNewCommandLineLength += strlen( debug_szPE );
     1858       
     1859        // only launch the specified executable (ICSDEBUG.EXE)
     1860        lpszPE = debug_szPE;
     1861        lpszExecutable = debug_pszOS2Debugger;
     1862      }
     1863#endif
     1864     
    18031865        //SvL: Allright. Before we call O32_CreateProcess, we must take care of
    18041866        //     lpCurrentDirectory ourselves. (Open32 ignores it!)
     
    18061868            char *newcmdline;
    18071869
    1808             newcmdline = (char *)malloc(strlen(lpCurrentDirectory) + strlen(szAppName) + strlen(lpCommandLine) + 32);
     1870            newcmdline = (char *)malloc(strlen(lpCurrentDirectory) + iNewCommandLineLength + 32);
    18091871            sprintf(newcmdline, "%s /OPT:[CURDIR=%s] %s %s", lpszPE, lpCurrentDirectory, szAppName, lpCommandLine);
    18101872            free(cmdline);
     
    18141876            char *newcmdline;
    18151877
    1816             newcmdline = (char *)malloc(strlen(szAppName) + strlen(lpCommandLine) + 16);
     1878            newcmdline = (char *)malloc(iNewCommandLineLength + 16);
    18171879            sprintf(newcmdline, "%s %s %s", lpszPE, szAppName, lpCommandLine);
    18181880            free(cmdline);
    18191881            cmdline = newcmdline;
    18201882        }
    1821         rc = O32_CreateProcess(lpszPE, (LPCSTR)cmdline,lpProcessAttributes,
     1883     
     1884        dprintf(("KERNEL32: CreateProcess starting [%s],[%s]",
     1885                 lpszExecutable,
     1886                 cmdline));
     1887     
     1888        rc = O32_CreateProcess(lpszExecutable, (LPCSTR)cmdline,lpProcessAttributes,
    18221889                               lpThreadAttributes, bInheritHandles, dwCreationFlags,
    18231890                               lpEnvironment, lpCurrentDirectory, lpStartupInfo,
Note: See TracChangeset for help on using the changeset viewer.