From 2074d8e7680c122e0d501d7c37734c4d891066d4 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Thu, 17 Oct 2024 07:28:07 +0200 Subject: [PATCH] [FIX] configure.c: Do not assume git If parsee were downloaded from sources other than git-clone(1) (e.g.: a tarball), perror(3) fails and/or git(1) is not available on the system, "git remote get-url origin" would have unexpected results or even return a NULL pointer, which would cause undefined behaviour on strchr(repo, ...); In such conditions, a placeholder string, namely "N/A", is strdup(3)ed to `repo`. --- configure.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/configure.c b/configure.c index e04b8e0..574b75f 100644 --- a/configure.c +++ b/configure.c @@ -186,18 +186,34 @@ cmd_stdout(char *cmd) FILE *f; char *line = NULL; size_t size; + int result; if (!cmd) { - return NULL; + goto failure; } if (!(f = popen(cmd, "r"))) { - return NULL; + goto failure; } getline(&line, &size, f); - pclose(f); + result = pclose(f); + + if (result < 0) + { + perror("pclose(3)"); + goto failure; + } + else if (result) + { + fprintf(stderr, "command exited with status %d: %s\n", result, cmd); + goto failure; + } return line; + +failure: + free(line); + return NULL; } static int exec_code(char *program, char *argv[]) @@ -426,10 +442,13 @@ main_build(int argc, char *argv[]) int opt; str_array_t *sources, *images, *utils, *aya; - if (strchr(repo, '\n')) + if (repo) { - *(strchr(repo, '\n')) = '\0'; + char *lf = strchr(repo, '\n'); + *lf = '\0'; } + else + repo = strdup("N/A"); while ((opt = getopt(argc, argv, "sl")) != -1) {