]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/path_init.c
q3map2: fix dangling pointer dereference
[xonotic/netradiant.git] / tools / quake3 / q3map2 / path_init.c
index 6bd061350d7327034afa11167f5fcb89af4139f1..5a31644a90c3f836bd674202305fc6617946761a 100644 (file)
@@ -66,29 +66,23 @@ char *LokiGetHomeDir( void ){
        #ifndef Q_UNIX
        return NULL;
        #else
+       static char     buf[ 4096 ];
+       struct passwd   pw, *pwp;
        char            *home;
-       uid_t id;
-       struct passwd   *pwd;
 
 
        /* get the home environment variable */
        home = getenv( "HOME" );
-       if ( home == NULL ) {
-               /* do some more digging */
-               id = getuid();
-               setpwent();
-               while ( ( pwd = getpwent() ) != NULL )
-               {
-                       if ( pwd->pw_uid == id ) {
-                               home = pwd->pw_dir;
-                               break;
-                       }
-               }
-               endpwent();
+       if ( home ) {
+               return Q_strncpyz( buf, home, sizeof( buf ) );
+       }
+
+       /* look up home dir in password database */
+       if ( getpwuid_r( getuid(), &pw, buf, sizeof( buf ), &pwp ) == 0 ) {
+               return pw.pw_dir;
        }
 
-       /* return it */
-       return home;
+       return NULL;
        #endif
 }
 
@@ -117,20 +111,16 @@ void LokiInitPaths( char *argv0 ){
                home = ".";
        }
 
+       path = getenv( "PATH" );
+
        /* do some path divining */
        strcpy( temp, argv0 );
        if ( strrchr( temp, '/' ) ) {
                argv0 = strrchr( argv0, '/' ) + 1;
        }
-       else
-       {
-               /* get path environment variable */
-               path = getenv( "PATH" );
-
-               /* minor setup */
-               last[ 0 ] = path[ 0 ];
-               last[ 1 ] = '\0';
+       else if ( path ) {
                found = qfalse;
+               last = path;
 
                /* go through each : segment of path */
                while ( last[ 0 ] != '\0' && found == qfalse )