From nobody Tue Apr 23 22:42:52 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VPHFS4z3nz5JDVX; Tue, 23 Apr 2024 22:42:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VPHFS4PLtz53w9; Tue, 23 Apr 2024 22:42:52 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713912172; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nTMxoCM+Vyjm6ePv3qo9PSoBboIxI5qnpuWIb5vYF0Q=; b=PxFTzNXzgLU2r9dA1vi9wylNqQsIfem7UVQmjBPgdd0r49jHthhbTuOcU+8LVlRE89gcT1 tdEocfUkcRPn3qno+qR3irXfUKHlPvYxOKOSyT7QaB0WP9WvOj4fopxZ/SnBQ58VfyqI8W +/z1t1sfy/Jply87KRh+9SwW6xohyt7A5XbUs/9erG9vBO60dUwAFbMCqPuyeDxkCrx4Gk shyG8C7+2khj4ptM5yNsNEOz0vJSQy/R+MDBsr3tCEK5+zKZ1g0szVtiG+yIpW5cIxxM53 Bt2N87O+ZhFQmsjA28kXQEs1CuRCjgK0K7+3NAt/go1/nWVwteEUxAf1pqp8CQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1713912172; a=rsa-sha256; cv=none; b=gjWxpuEVQvFlviPjAPuSYyM/lLsZHUP+vUh9JK7LIz0w6J312JlLELiKD7aUTUHT/gZynA HnzaG5lOVqEHhnhgZMvULfIrh2w1oG7ZRdD2VgJV5YS+LDgPFisM6XP5RKKSQtjGISgRg9 7njPTXVdUxMyJZa77hoUgJJOy7HIJ269RvJQ1jrbljeLbD6Pa0dWmLlg/UvxBS2VIIlPHg 4K+xcOSJj2jBkzN44FI4tLJ4xLBu4fxSBSUKtEn4jlGjvYKi2dwSuJoT8DsVu7EG9+N6Tf sz+HUdlH8seWFbKsPM+tqgkka+B9Eqd00WVbdR+PUGpwSrY9Pn0RN1Xkd5JyDQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713912172; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nTMxoCM+Vyjm6ePv3qo9PSoBboIxI5qnpuWIb5vYF0Q=; b=CAHF1A5yxjiFPYby0LHWiTsJOA1QM6LpXqz3xyvDpEPHhpkC+EKnGEO81+ZAW5MBuWGqcU D5w1f5jAuuMRSCiLOv8NeB17nBEPgL6uiyzuplWsdj5aOh5jkd4vcBs3CcHm8ziMgyMVnS tyeQoUq98RknoOdik0yklJvHnOXLI8Z1u8+mW0R8ME+tZ/SNddAIvSqsFTDDoeH2sxKWJ8 niXfI50Klb1rT0ev76Z6CL9SrAdYup3fb25Uxlkgi42APHaDqSoR4m2bRpGOnZHMmU6y9l 5Od4rvzyXAl/f+NLvWZsDEuHEsD0suUvP0a+vSjV38vT+ppqQ219JyPMeeSisQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VPHFS40HHzqkq; Tue, 23 Apr 2024 22:42:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43NMgqZF082029; Tue, 23 Apr 2024 22:42:52 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43NMgqxx082026; Tue, 23 Apr 2024 22:42:52 GMT (envelope-from git) Date: Tue, 23 Apr 2024 22:42:52 GMT Message-Id: <202404232242.43NMgqxx082026@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: 91bdebc958bb - main - bsdinstall/distfetch.c: check environment variables before downloading and handle memory allocation errors List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 91bdebc958bb0da03f604bad19f99e3b10e96ac7 Auto-Submitted: auto-generated The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=91bdebc958bb0da03f604bad19f99e3b10e96ac7 commit 91bdebc958bb0da03f604bad19f99e3b10e96ac7 Author: rilysh AuthorDate: 2024-04-23 22:40:19 +0000 Commit: Warner Losh CommitDate: 2024-04-23 22:42:38 +0000 bsdinstall/distfetch.c: check environment variables before downloading and handle memory allocation errors 1. Currently, distfetch checks environment variables existence when it will use them or in a case (in chdir()) it doesn't check at all. As they are necessary to set before doing anything with it, check them, if they set or not, before proceeding any further. This also avoids extra cleaning when that environment variable isn't set. 2. Handle memory allocation error in malloc(PATH_MAX) and replace (sizeof const char *) with (sizeof char *). Both are similar and const doesn't have a size. 3. Indent the error message a bit in chdir(). Signed-off-by: rilysh Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/1071 --- usr.sbin/bsdinstall/distfetch/distfetch.c | 33 +++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/usr.sbin/bsdinstall/distfetch/distfetch.c b/usr.sbin/bsdinstall/distfetch/distfetch.c index c431e187799d..094929d89ea1 100644 --- a/usr.sbin/bsdinstall/distfetch/distfetch.c +++ b/usr.sbin/bsdinstall/distfetch/distfetch.c @@ -46,7 +46,7 @@ static int fetch_files(int nfiles, char **urls); int main(void) { - char *diststring; + char *diststring, *dists, *distdir, *distsite; char **urls; int i; int ndists = 0; @@ -54,17 +54,24 @@ main(void) char error[PATH_MAX + 512]; struct bsddialog_conf conf; - if (getenv("DISTRIBUTIONS") == NULL) + if ((dists = getenv("DISTRIBUTIONS")) == NULL) errx(EXIT_FAILURE, "DISTRIBUTIONS variable is not set"); - diststring = strdup(getenv("DISTRIBUTIONS")); + if ((distdir = getenv("BSDINSTALL_DISTDIR")) == NULL) + errx(EXIT_FAILURE, "BSDINSTALL_DISTDIR variable is not set"); + + if ((distsite = getenv("BSDINSTALL_DISTSITE")) == NULL) + errx(EXIT_FAILURE, "BSDINSTALL_DISTSITE variable is not set"); + + if ((diststring = strdup(dists)) == NULL) + errx(EXIT_FAILURE, "Error: diststring variable out of memory!"); + for (i = 0; diststring[i] != 0; i++) if (isspace(diststring[i]) && !isspace(diststring[i+1])) ndists++; ndists++; /* Last one */ - urls = calloc(ndists, sizeof(const char *)); - if (urls == NULL) { + if ((urls = calloc(ndists, sizeof(char *))) == NULL) { free(diststring); errx(EXIT_FAILURE, "Error: distfetch URLs out of memory!"); } @@ -78,15 +85,21 @@ main(void) bsddialog_backtitle(&conf, OSNAME " Installer"); for (i = 0; i < ndists; i++) { - urls[i] = malloc(PATH_MAX); + if ((urls[i] = malloc(PATH_MAX)) == NULL) { + free(urls); + free(diststring); + bsddialog_end(); + errx(EXIT_FAILURE, "Error: distfetch URLs out of memory!"); + } + snprintf(urls[i], PATH_MAX, "%s/%s", - getenv("BSDINSTALL_DISTSITE"), strsep(&diststring, " \t")); + distsite, strsep(&diststring, " \t")); } - if (chdir(getenv("BSDINSTALL_DISTDIR")) != 0) { + if (chdir(distdir) != 0) { snprintf(error, sizeof(error), - "Could not change to directory %s: %s\n", - getenv("BSDINSTALL_DISTDIR"), strerror(errno)); + "Could not change to directory %s: %s\n", + distdir, strerror(errno)); conf.title = "Error"; bsddialog_msgbox(&conf, error, 0, 0); bsddialog_end();