Date: Tue, 23 Apr 2024 21:28:25 -0600 From: Warner Losh <imp@bsdimp.com> To: Jessica Clarke <jrtc27@freebsd.org> Cc: Warner Losh <imp@freebsd.org>, "src-committers@freebsd.org" <src-committers@freebsd.org>, "dev-commits-src-all@freebsd.org" <dev-commits-src-all@freebsd.org>, "dev-commits-src-main@freebsd.org" <dev-commits-src-main@freebsd.org> Subject: Re: git: 91bdebc958bb - main - bsdinstall/distfetch.c: check environment variables before downloading and handle memory allocation errors Message-ID: <CANCZdfp8dJWbH-wAV_QF%2BgUeUzsHCqpkthc%2BxP7-9qaB2PKvhg@mail.gmail.com> In-Reply-To: <9C180198-A6B6-41D3-AB49-D9582D356ADE@freebsd.org> References: <202404232242.43NMgqxx082026@gitrepo.freebsd.org> <9C180198-A6B6-41D3-AB49-D9582D356ADE@freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
--0000000000001c939c0616cf44d6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I've backed this out as well... I'd thought that the style stuff had been worked out... And I'll leave the pull request closed since our newer guidance would suggest this isn't enough of a win (I've been processing the older ones under the old guidance on this point, but now I'm questioning that)... The cleanups aren't that bad (moduo style), but they don't really move the needle enough and would only be acceptable in the context of other improvements (though even then it might be tough to justify). Warner On Tue, Apr 23, 2024 at 6:21=E2=80=AFPM Jessica Clarke <jrtc27@freebsd.org>= wrote: > On 23 Apr 2024, at 23:42, Warner Losh <imp@FreeBSD.org> wrote: > > > > The branch main has been updated by imp: > > > > URL: > https://cgit.FreeBSD.org/src/commit/?id=3D91bdebc958bb0da03f604bad19f99e3= b10e96ac7 > > > > commit 91bdebc958bb0da03f604bad19f99e3b10e96ac7 > > Author: rilysh <nightquick@proton.me> > > AuthorDate: 2024-04-23 22:40:19 +0000 > > Commit: Warner Losh <imp@FreeBSD.org> > > 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. > > Latter is fine I guess but they=E2=80=99re the same by definition, it=E2= =80=99s churn. > > > 3. Indent the error message a bit in chdir(). > > AKA violate style(9). > > > Signed-off-by: rilysh <nightquick@proton.me> > > 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 =3D 0; > > @@ -54,17 +54,24 @@ main(void) > > char error[PATH_MAX + 512]; > > struct bsddialog_conf conf; > > > > - if (getenv("DISTRIBUTIONS") =3D=3D NULL) > > + if ((dists =3D getenv("DISTRIBUTIONS")) =3D=3D NULL) > > errx(EXIT_FAILURE, "DISTRIBUTIONS variable is not set"); > > > > - diststring =3D strdup(getenv("DISTRIBUTIONS")); > > + if ((distdir =3D getenv("BSDINSTALL_DISTDIR")) =3D=3D NULL) > > + errx(EXIT_FAILURE, "BSDINSTALL_DISTDIR variable is not set"); > > + > > + if ((distsite =3D getenv("BSDINSTALL_DISTSITE")) =3D=3D NULL) > > + errx(EXIT_FAILURE, "BSDINSTALL_DISTSITE variable is not set"); > > + > > + if ((diststring =3D strdup(dists)) =3D=3D NULL) > > + errx(EXIT_FAILURE, "Error: diststring variable out of memory!"); > > + > > for (i =3D 0; diststring[i] !=3D 0; i++) > > if (isspace(diststring[i]) && !isspace(diststring[i+1])) > > ndists++; > > ndists++; /* Last one */ > > > > - urls =3D calloc(ndists, sizeof(const char *)); > > - if (urls =3D=3D NULL) { > > + if ((urls =3D calloc(ndists, sizeof(char *))) =3D=3D NULL) { > > Moving into the if is pointless. > > > free(diststring); > > errx(EXIT_FAILURE, "Error: distfetch URLs out of memory!"); > > } > > @@ -78,15 +85,21 @@ main(void) > > bsddialog_backtitle(&conf, OSNAME " Installer"); > > > > for (i =3D 0; i < ndists; i++) { > > - urls[i] =3D malloc(PATH_MAX); > > + if ((urls[i] =3D malloc(PATH_MAX)) =3D=3D NULL) { > > Moving into the if is pointless. > > > + free(urls); > > + free(diststring); > > Like I said in the review, reviewing urls and diststring but not the > elements of urls is a stupid halfway house, and it=E2=80=99s all a waste = of > time when you=E2=80=99re about to call errx. If one wants to be super ped= antic > and free memory prior to calling errx, fine, but do it in full, don=E2=80= =99t > do an arbitrary subset. > > > + 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")); > > Breaks style(9). > > > } > > > > - if (chdir(getenv("BSDINSTALL_DISTDIR")) !=3D 0) { > > + if (chdir(distdir) !=3D 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)); > > Breaks style(9). > > Jess > > > conf.title =3D "Error"; > > bsddialog_msgbox(&conf, error, 0, 0); > > bsddialog_end(); > > --0000000000001c939c0616cf44d6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div>I've backed this out as well... I'd thought t= hat the style stuff had been worked out...</div><div><br></div><div>And I&#= 39;ll leave the pull request closed since our newer guidance would suggest = this isn't enough of a win (I've been processing the older ones und= er the old guidance on this point, but now I'm questioning that)... The= cleanups aren't that bad (moduo style), but they don't really move= the needle enough and would only be acceptable in the context of other imp= rovements (though even then it might be tough to justify).</div><div><br></= div><div>Warner<br></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" cl= ass=3D"gmail_attr">On Tue, Apr 23, 2024 at 6:21=E2=80=AFPM Jessica Clarke &= lt;<a href=3D"mailto:jrtc27@freebsd.org">jrtc27@freebsd.org</a>> wrote:<= br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8e= x;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 23 Apr 2024, = at 23:42, Warner Losh <imp@FreeBSD.org> wrote:<br> > <br> > The branch main has been updated by imp:<br> > <br> > URL: <a href=3D"https://cgit.FreeBSD.org/src/commit/?id=3D91bdebc958bb= 0da03f604bad19f99e3b10e96ac7" rel=3D"noreferrer" target=3D"_blank">https://= cgit.FreeBSD.org/src/commit/?id=3D91bdebc958bb0da03f604bad19f99e3b10e96ac7<= /a><br> > <br> > commit 91bdebc958bb0da03f604bad19f99e3b10e96ac7<br> > Author:=C2=A0 =C2=A0 =C2=A0rilysh <<a href=3D"mailto:nightquick@pro= ton.me" target=3D"_blank">nightquick@proton.me</a>><br> > AuthorDate: 2024-04-23 22:40:19 +0000<br> > Commit:=C2=A0 =C2=A0 =C2=A0Warner Losh <imp@FreeBSD.org><br> > CommitDate: 2024-04-23 22:42:38 +0000<br> > <br> >=C2=A0 =C2=A0 bsdinstall/distfetch.c: check environment variables befor= e downloading and handle memory allocation errors<br> > <br> >=C2=A0 =C2=A0 1. Currently, distfetch checks environment variables exis= tence<br> >=C2=A0 =C2=A0 when it will use them or in a case (in chdir()) it doesn&= #39;t check<br> >=C2=A0 =C2=A0 at all. As they are necessary to set before doing anythin= g with<br> >=C2=A0 =C2=A0 it, check them, if they set or not, before proceeding any= further.<br> >=C2=A0 =C2=A0 This also avoids extra cleaning when that environment var= iable<br> >=C2=A0 =C2=A0 isn't set.<br> > <br> >=C2=A0 =C2=A0 2. Handle memory allocation error in malloc(PATH_MAX) and= replace<br> >=C2=A0 =C2=A0 (sizeof const char *) with (sizeof char *). Both are simi= lar and<br> >=C2=A0 =C2=A0 const doesn't have a size.<br> <br> Latter is fine I guess but they=E2=80=99re the same by definition, it=E2=80= =99s churn.<br> <br> >=C2=A0 =C2=A0 3. Indent the error message a bit in chdir().<br> <br> AKA violate style(9).<br> <br> >=C2=A0 =C2=A0 Signed-off-by: rilysh <<a href=3D"mailto:nightquick@pr= oton.me" target=3D"_blank">nightquick@proton.me</a>><br> >=C2=A0 =C2=A0 Reviewed by: imp<br> >=C2=A0 =C2=A0 Pull Request: <a href=3D"https://github.com/freebsd/freeb= sd-src/pull/1071" rel=3D"noreferrer" target=3D"_blank">https://github.com/f= reebsd/freebsd-src/pull/1071</a><br> > ---<br> > usr.sbin/bsdinstall/distfetch/distfetch.c | 33 +++++++++++++++++++++--= --------<br> > 1 file changed, 23 insertions(+), 10 deletions(-)<br> > <br> > diff --git a/usr.sbin/bsdinstall/distfetch/distfetch.c b/usr.sbin/bsdi= nstall/distfetch/distfetch.c<br> > index c431e187799d..094929d89ea1 100644<br> > --- a/usr.sbin/bsdinstall/distfetch/distfetch.c<br> > +++ b/usr.sbin/bsdinstall/distfetch/distfetch.c<br> > @@ -46,7 +46,7 @@ static int fetch_files(int nfiles, char **urls);<br> > int<br> > main(void)<br> > {<br> > - char *diststring;<br> > + char *diststring, *dists, *distdir, *distsite;<br> > char **urls;<br> > int i;<br> > int ndists =3D 0;<br> > @@ -54,17 +54,24 @@ main(void)<br> > char error[PATH_MAX + 512];<br> > struct bsddialog_conf conf;<br> > <br> > - if (getenv("DISTRIBUTIONS") =3D=3D NULL)<br> > + if ((dists =3D getenv("DISTRIBUTIONS")) =3D=3D NULL)<br> > errx(EXIT_FAILURE, "DISTRIBUTIONS variable is not set");<br> > <br> > - diststring =3D strdup(getenv("DISTRIBUTIONS"));<br> > + if ((distdir =3D getenv("BSDINSTALL_DISTDIR")) =3D=3D NULL= )<br> > + errx(EXIT_FAILURE, "BSDINSTALL_DISTDIR variable is not set"= ;);<br> > +<br> > + if ((distsite =3D getenv("BSDINSTALL_DISTSITE")) =3D=3D NU= LL)<br> > + errx(EXIT_FAILURE, "BSDINSTALL_DISTSITE variable is not set&quo= t;);<br> > +<br> > + if ((diststring =3D strdup(dists)) =3D=3D NULL)<br> > + errx(EXIT_FAILURE, "Error: diststring variable out of memory!&q= uot;);<br> > +<br> > for (i =3D 0; diststring[i] !=3D 0; i++)<br> > if (isspace(diststring[i]) && !isspace(diststring[i+1]))<br> > ndists++;<br> > ndists++; /* Last one */<br> > <br> > - urls =3D calloc(ndists, sizeof(const char *));<br> > - if (urls =3D=3D NULL) {<br> > + if ((urls =3D calloc(ndists, sizeof(char *))) =3D=3D NULL) {<br> <br> Moving into the if is pointless.<br> <br> > free(diststring);<br> > errx(EXIT_FAILURE, "Error: distfetch URLs out of memory!");<= br> > }<br> > @@ -78,15 +85,21 @@ main(void)<br> > bsddialog_backtitle(&conf, OSNAME " Installer");<br> > <br> > for (i =3D 0; i < ndists; i++) {<br> > - urls[i] =3D malloc(PATH_MAX);<br> > + if ((urls[i] =3D malloc(PATH_MAX)) =3D=3D NULL) {<br> <br> Moving into the if is pointless.<br> <br> > + free(urls);<br> > + free(diststring);<br> <br> Like I said in the review, reviewing urls and diststring but not the<br> elements of urls is a stupid halfway house, and it=E2=80=99s all a waste of= <br> time when you=E2=80=99re about to call errx. If one wants to be super pedan= tic<br> and free memory prior to calling errx, fine, but do it in full, don=E2=80= =99t<br> do an arbitrary subset.<br> <br> > + bsddialog_end();<br> > + errx(EXIT_FAILURE, "Error: distfetch URLs out of memory!")= ;<br> > + }<br> > +<br> > snprintf(urls[i], PATH_MAX, "%s/%s",<br> > -=C2=A0 =C2=A0 getenv("BSDINSTALL_DISTSITE"), strsep(&di= ststring, " \t"));<br> > + distsite, strsep(&diststring, " \t"));<br> <br> Breaks style(9).<br> <br> > }<br> > <br> > - if (chdir(getenv("BSDINSTALL_DISTDIR")) !=3D 0) {<br> > + if (chdir(distdir) !=3D 0) {<br> > snprintf(error, sizeof(error),<br> > -=C2=A0 =C2=A0 "Could not change to directory %s: %s\n",<br> > -=C2=A0 =C2=A0 getenv("BSDINSTALL_DISTDIR"), strerror(errno)= );<br> > + "Could not change to directory %s: %s\n",<br> > + distdir, strerror(errno));<br> <br> Breaks style(9).<br> <br> Jess<br> <br> > conf.title =3D "Error";<br> > bsddialog_msgbox(&conf, error, 0, 0);<br> > bsddialog_end();<br> <br> </blockquote></div></div> --0000000000001c939c0616cf44d6--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CANCZdfp8dJWbH-wAV_QF%2BgUeUzsHCqpkthc%2BxP7-9qaB2PKvhg>