Skip to content

Commit 310e9f4

Browse files
authored
Merge pull request #937 from AppImage/fix-segfault
Hopefully fix git version guessing related segfaults
2 parents 58c9f8e + 5ee7b86 commit 310e9f4

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

src/appimagetool.c

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static gboolean sign = FALSE;
7979
static gboolean no_appstream = FALSE;
8080
gchar **remaining_args = NULL;
8181
gchar *updateinformation = NULL;
82-
static gboolean guessupdateinformation = FALSE;
82+
static gboolean guess_update_information = FALSE;
8383
gchar *bintray_user = NULL;
8484
gchar *bintray_repo = NULL;
8585
gchar *sqfs_comp = "gzip";
@@ -464,7 +464,7 @@ static GOptionEntry entries[] =
464464
{
465465
{ "list", 'l', 0, G_OPTION_ARG_NONE, &list, "List files in SOURCE AppImage", NULL },
466466
{ "updateinformation", 'u', 0, G_OPTION_ARG_STRING, &updateinformation, "Embed update information STRING; if zsyncmake is installed, generate zsync file", NULL },
467-
{ "guess", 'g', 0, G_OPTION_ARG_NONE, &guessupdateinformation, "Guess update information based on Travis CI or GitLab environment variables", NULL },
467+
{ "guess", 'g', 0, G_OPTION_ARG_NONE, &guess_update_information, "Guess update information based on Travis CI or GitLab environment variables", NULL },
468468
{ "bintray-user", 0, 0, G_OPTION_ARG_STRING, &bintray_user, "Bintray user name", NULL },
469469
{ "bintray-repo", 0, 0, G_OPTION_ARG_STRING, &bintray_repo, "Bintray repository", NULL },
470470
{ "version", 0, 0, G_OPTION_ARG_NONE, &showVersionOnly, "Show version number", NULL },
@@ -559,23 +559,40 @@ main (int argc, char *argv[])
559559
* TODO: Might also want to somehow make use of
560560
* git rev-parse --abbrev-ref HEAD
561561
* git log -1 --format=%ci */
562-
gchar *version_env; // In which cases do we need to malloc() here?
563-
version_env = getenv("VERSION");
564-
if(guessupdateinformation){
565-
if(g_find_program_in_path ("git")) {
562+
gchar* version_env = getenv("VERSION");
563+
564+
if (guess_update_information) {
565+
char* gitPath = g_find_program_in_path("git");
566+
567+
if (gitPath != NULL) {
566568
if (version_env == NULL) {
567569
GError* error = NULL;
568-
gchar* out = NULL;
570+
gchar* out = NULL;
569571

570572
char command_line[] = "git rev-parse --short HEAD";
571573

572-
int exitcode = -1;
573-
int ret = g_spawn_command_line_sync(command_line, &out, NULL, &exitcode, &error);
574+
// *not* the exit code! must be interpreted via g_spawn_check_exit_status!
575+
int exit_status = -1;
576+
577+
// g_spawn_command_line_sync returns whether the program succeeded
578+
gint ret = g_spawn_command_line_sync(command_line, &out, NULL, &exit_status, &error);
574579

575-
if (ret != 0 ||error != NULL) {
576-
g_printerr("Failed to run 'git rev-parse --short HEAD': %s\n", error->message);
577-
} else if (exitcode != 0) {
578-
g_printerr("Failed to run 'git rev-parse --short HEAD': exited with code %d\n", exitcode);
580+
if (ret != 0 || error != NULL) {
581+
// g_spawn_command_line_sync might have set error already, in that case we don't want to overwrite
582+
if (error == NULL) {
583+
// to get a proper error message, we now fetch the message via the returned exit code
584+
// the call returns false if the call failed, and this is what we expect to have happened
585+
// hence we can assume that there must be an error in GLib if it returned true
586+
if (g_spawn_check_exit_status(exit_status, &error)) {
587+
g_printerr("Failed to run 'git rev-parse --short HEAD, but GLib says the process didn't exit abnormally");
588+
}
589+
}
590+
591+
if (error == NULL) {
592+
g_printerr("Failed to run 'git rev-parse --short HEAD, but failed to interpret GLib error state: %d\n", exit_status);
593+
} else {
594+
g_printerr("Failed to run 'git rev-parse --short HEAD: %s (code %d)\n", error->message, error->code);
595+
}
579596
} else {
580597
version_env = g_strstrip(out);
581598

@@ -587,6 +604,8 @@ main (int argc, char *argv[])
587604
}
588605
}
589606
}
607+
608+
free(gitPath);
590609
}
591610

592611
if(!((0 == strcmp(sqfs_comp, "gzip")) || (0 ==strcmp(sqfs_comp, "xz"))))
@@ -861,7 +880,7 @@ main (int argc, char *argv[])
861880

862881
/* If the user has not provided update information but we know this is a Travis CI build,
863882
* then fill in update information based on TRAVIS_REPO_SLUG */
864-
if(guessupdateinformation){
883+
if(guess_update_information){
865884
if(travis_repo_slug){
866885
if(!github_token) {
867886
printf("Will not guess update information since $GITHUB_TOKEN is missing,\n");

0 commit comments

Comments
 (0)