@@ -529,6 +529,16 @@ int main(int argc, char *argv[]) {
529529#endif
530530 }
531531
532+ // temporary directories are required in a few places
533+ // therefore we implement the detection of the temp base dir at the top of the code to avoid redundancy
534+ char temp_base [PATH_MAX ] = P_tmpdir ;
535+
536+ {
537+ const char * const TMPDIR = getenv ("TMPDIR" );
538+ if (TMPDIR != NULL )
539+ strcpy (temp_base , getenv ("TMPDIR" ));
540+ }
541+
532542 fs_offset = appimage_get_elf_size (appimage_path );
533543
534544 // error check
@@ -585,13 +595,7 @@ int main(int argc, char *argv[]) {
585595 }
586596
587597 if (arg && strcmp (arg , "appimage-extract-and-run" ) == 0 ) {
588- char temp_base [PATH_MAX ] = "/tmp" ;
589-
590- const char * const TMPDIR = getenv ("TMPDIR" );
591- if (TMPDIR != NULL )
592- strcpy (temp_base , getenv ("TMPDIR" ));
593-
594- char * hexlified_digest ;
598+ char * hexlified_digest = NULL ;
595599
596600 // calculate MD5 hash of file, and use it to make extracted directory name "content-aware"
597601 // see https://github.com/AppImage/AppImageKit/issues/841 for more information
@@ -714,22 +718,30 @@ int main(int argc, char *argv[]) {
714718
715719 int dir_fd , res ;
716720
717- char mount_dir [64 ];
721+ size_t templen = strlen (temp_base );
722+
723+ // allocate enough memory (size of name won't exceed 60 bytes)
724+ char mount_dir [templen + 60 ];
725+
718726 size_t namelen = strlen (basename (argv [0 ]));
719- if (namelen > 6 ){
720- namelen = 6 ;
727+ // limit length of tempdir name
728+ if (namelen > 6 ){
729+ namelen = 6 ;
721730 }
722- strncpy (mount_dir , "/tmp/.mount_" , 12 );
723- strncpy (mount_dir + 12 , basename (argv [0 ]), namelen );
724- strncpy (mount_dir + 12 + namelen , "XXXXXX" , 6 );
725- mount_dir [12 + namelen + 6 ] = 0 ; // null terminate destination
726731
727- char filename [100 ]; /* enough for mount_dir + "/AppRun" */
732+ strcpy (mount_dir , temp_base );
733+ strncpy (mount_dir + templen , "/.mount_" , 8 );
734+ strncpy (mount_dir + templen + 8 , basename (argv [0 ]), namelen );
735+ strncpy (mount_dir + templen + 8 + namelen , "XXXXXX" , 6 );
736+ mount_dir [templen + 8 + namelen + 6 ] = 0 ; // null terminate destination
737+
738+ size_t mount_dir_size = strlen (mount_dir );
728739 pid_t pid ;
729740 char * * real_argv ;
730741 int i ;
731742
732743 if (mkdtemp (mount_dir ) == NULL ) {
744+ perror ("create mount dir error" );
733745 exit (1 );
734746 }
735747
@@ -799,17 +811,18 @@ int main(int argc, char *argv[]) {
799811 }
800812 close (dir_fd );
801813
802- strcpy (filename , mount_dir );
803- strcat (filename , "/AppRun" );
804-
805814 real_argv = malloc (sizeof (char * ) * (argc + 1 ));
806815 for (i = 0 ; i < argc ; i ++ ) {
807816 real_argv [i ] = argv [i ];
808817 }
809818 real_argv [i ] = NULL ;
810819
811820 if (arg && strcmp (arg ,"appimage-mount" )== 0 ) {
812- printf ("%s\n" , mount_dir );
821+ char real_mount_dir [PATH_MAX ];
822+ if (realpath (mount_dir , real_mount_dir ) == real_mount_dir )
823+ printf ("%s\n" , real_mount_dir );
824+ else
825+ printf ("%s\n" , mount_dir );
813826 for (;;) pause ();
814827 }
815828
@@ -855,6 +868,10 @@ int main(int argc, char *argv[]) {
855868 setenv ( "OWD" , cwd , 1 );
856869 }
857870
871+ char filename [mount_dir_size + 8 ]; /* enough for mount_dir + "/AppRun" */
872+ strcpy (filename , mount_dir );
873+ strcat (filename , "/AppRun" );
874+
858875 /* TODO: Find a way to get the exit status and/or output of this */
859876 execv (filename , real_argv );
860877 /* Error if we continue here */
0 commit comments