uclient-utils: Handle memory allocation failure for url file name
[project/uclient.git] / uclient-fetch.c
index dff144b..38c9c53 100644 (file)
@@ -50,7 +50,7 @@ static bool verify = true;
 static bool proxy = true;
 static bool default_certs = false;
 static bool no_output;
-static const char *output_file;
+static const char *opt_output_file;
 static int output_fd = -1;
 static int error_ret;
 static off_t out_offset;
@@ -97,6 +97,7 @@ get_proxy_url(char *url)
 
 static int open_output_file(const char *path, uint64_t resume_offset)
 {
+       const char *output_file = opt_output_file;
        char *filename = NULL;
        int flags;
        int ret;
@@ -121,6 +122,11 @@ static int open_output_file(const char *path, uint64_t resume_offset)
                }
        } else {
                filename = uclient_get_url_filename(path, "index.html");
+               if (!filename) {
+                       ret = -ENOMEM;
+                       goto out;
+               }
+
                output_file = filename;
        }
 
@@ -150,6 +156,7 @@ done:
 
 free:
        free(filename);
+out:
        return ret;
 }
 
@@ -367,7 +374,7 @@ static void request_done(struct uclient *cl)
                        return;
        }
 
-       if (output_fd >= 0 && !output_file) {
+       if (output_fd >= 0 && !opt_output_file) {
                close(output_fd);
                output_fd = -1;
        }
@@ -493,7 +500,7 @@ static int no_ssl(const char *progname)
 {
        fprintf(stderr,
                "%s: SSL support not available, please install one of the "
-               "libustream-ssl-* libraries as well as the ca-bundle and "
+               "libustream-.*[ssl|tls] packages as well as the ca-bundle and "
                "ca-certificates packages.\n",
                progname);
 
@@ -615,7 +622,7 @@ int main(int argc, char **argv)
                        user_agent = optarg;
                        break;
                case 'O':
-                       output_file = optarg;
+                       opt_output_file = optarg;
                        break;
                case 'P':
                        if (chdir(optarg)) {