Skip site navigation (1)Skip section navigation (2)
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&#39;ve backed this out as well... I&#39;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&#39;t enough of a win (I&#39;ve been processing the older ones und=
er the old guidance on this point, but now I&#39;m questioning that)... The=
 cleanups aren&#39;t that bad (moduo style), but they don&#39;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>&gt; 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 &lt;imp@FreeBSD.org&gt; wrote:<br>
&gt; <br>
&gt; The branch main has been updated by imp:<br>
&gt; <br>
&gt; 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>
&gt; <br>
&gt; commit 91bdebc958bb0da03f604bad19f99e3b10e96ac7<br>
&gt; Author:=C2=A0 =C2=A0 =C2=A0rilysh &lt;<a href=3D"mailto:nightquick@pro=
ton.me" target=3D"_blank">nightquick@proton.me</a>&gt;<br>
&gt; AuthorDate: 2024-04-23 22:40:19 +0000<br>
&gt; Commit:=C2=A0 =C2=A0 =C2=A0Warner Losh &lt;imp@FreeBSD.org&gt;<br>
&gt; CommitDate: 2024-04-23 22:42:38 +0000<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 bsdinstall/distfetch.c: check environment variables befor=
e downloading and handle memory allocation errors<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 1. Currently, distfetch checks environment variables exis=
tence<br>
&gt;=C2=A0 =C2=A0 when it will use them or in a case (in chdir()) it doesn&=
#39;t check<br>
&gt;=C2=A0 =C2=A0 at all. As they are necessary to set before doing anythin=
g with<br>
&gt;=C2=A0 =C2=A0 it, check them, if they set or not, before proceeding any=
 further.<br>
&gt;=C2=A0 =C2=A0 This also avoids extra cleaning when that environment var=
iable<br>
&gt;=C2=A0 =C2=A0 isn&#39;t set.<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 2. Handle memory allocation error in malloc(PATH_MAX) and=
 replace<br>
&gt;=C2=A0 =C2=A0 (sizeof const char *) with (sizeof char *). Both are simi=
lar and<br>
&gt;=C2=A0 =C2=A0 const doesn&#39;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>
&gt;=C2=A0 =C2=A0 3. Indent the error message a bit in chdir().<br>
<br>
AKA violate style(9).<br>
<br>
&gt;=C2=A0 =C2=A0 Signed-off-by: rilysh &lt;<a href=3D"mailto:nightquick@pr=
oton.me" target=3D"_blank">nightquick@proton.me</a>&gt;<br>
&gt;=C2=A0 =C2=A0 Reviewed by: imp<br>
&gt;=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>
&gt; ---<br>
&gt; usr.sbin/bsdinstall/distfetch/distfetch.c | 33 +++++++++++++++++++++--=
--------<br>
&gt; 1 file changed, 23 insertions(+), 10 deletions(-)<br>
&gt; <br>
&gt; diff --git a/usr.sbin/bsdinstall/distfetch/distfetch.c b/usr.sbin/bsdi=
nstall/distfetch/distfetch.c<br>
&gt; index c431e187799d..094929d89ea1 100644<br>
&gt; --- a/usr.sbin/bsdinstall/distfetch/distfetch.c<br>
&gt; +++ b/usr.sbin/bsdinstall/distfetch/distfetch.c<br>
&gt; @@ -46,7 +46,7 @@ static int fetch_files(int nfiles, char **urls);<br>
&gt; int<br>
&gt; main(void)<br>
&gt; {<br>
&gt; - char *diststring;<br>
&gt; + char *diststring, *dists, *distdir, *distsite;<br>
&gt; char **urls;<br>
&gt; int i;<br>
&gt; int ndists =3D 0;<br>
&gt; @@ -54,17 +54,24 @@ main(void)<br>
&gt; char error[PATH_MAX + 512];<br>
&gt; struct bsddialog_conf conf;<br>
&gt; <br>
&gt; - if (getenv(&quot;DISTRIBUTIONS&quot;) =3D=3D NULL)<br>
&gt; + if ((dists =3D getenv(&quot;DISTRIBUTIONS&quot;)) =3D=3D NULL)<br>
&gt; errx(EXIT_FAILURE, &quot;DISTRIBUTIONS variable is not set&quot;);<br>
&gt; <br>
&gt; - diststring =3D strdup(getenv(&quot;DISTRIBUTIONS&quot;));<br>
&gt; + if ((distdir =3D getenv(&quot;BSDINSTALL_DISTDIR&quot;)) =3D=3D NULL=
)<br>
&gt; + errx(EXIT_FAILURE, &quot;BSDINSTALL_DISTDIR variable is not set&quot=
;);<br>
&gt; +<br>
&gt; + if ((distsite =3D getenv(&quot;BSDINSTALL_DISTSITE&quot;)) =3D=3D NU=
LL)<br>
&gt; + errx(EXIT_FAILURE, &quot;BSDINSTALL_DISTSITE variable is not set&quo=
t;);<br>
&gt; +<br>
&gt; + if ((diststring =3D strdup(dists)) =3D=3D NULL)<br>
&gt; + errx(EXIT_FAILURE, &quot;Error: diststring variable out of memory!&q=
uot;);<br>
&gt; +<br>
&gt; for (i =3D 0; diststring[i] !=3D 0; i++)<br>
&gt; if (isspace(diststring[i]) &amp;&amp; !isspace(diststring[i+1]))<br>
&gt; ndists++;<br>
&gt; ndists++; /* Last one */<br>
&gt; <br>
&gt; - urls =3D calloc(ndists, sizeof(const char *));<br>
&gt; - if (urls =3D=3D NULL) {<br>
&gt; + if ((urls =3D calloc(ndists, sizeof(char *))) =3D=3D NULL) {<br>
<br>
Moving into the if is pointless.<br>
<br>
&gt; free(diststring);<br>
&gt; errx(EXIT_FAILURE, &quot;Error: distfetch URLs out of memory!&quot;);<=
br>
&gt; }<br>
&gt; @@ -78,15 +85,21 @@ main(void)<br>
&gt; bsddialog_backtitle(&amp;conf, OSNAME &quot; Installer&quot;);<br>
&gt; <br>
&gt; for (i =3D 0; i &lt; ndists; i++) {<br>
&gt; - urls[i] =3D malloc(PATH_MAX);<br>
&gt; + if ((urls[i] =3D malloc(PATH_MAX)) =3D=3D NULL) {<br>
<br>
Moving into the if is pointless.<br>
<br>
&gt; + free(urls);<br>
&gt; + 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>
&gt; + bsddialog_end();<br>
&gt; + errx(EXIT_FAILURE, &quot;Error: distfetch URLs out of memory!&quot;)=
;<br>
&gt; + }<br>
&gt; +<br>
&gt; snprintf(urls[i], PATH_MAX, &quot;%s/%s&quot;,<br>
&gt; -=C2=A0 =C2=A0 getenv(&quot;BSDINSTALL_DISTSITE&quot;), strsep(&amp;di=
ststring, &quot; \t&quot;));<br>
&gt; + distsite, strsep(&amp;diststring, &quot; \t&quot;));<br>
<br>
Breaks style(9).<br>
<br>
&gt; }<br>
&gt; <br>
&gt; - if (chdir(getenv(&quot;BSDINSTALL_DISTDIR&quot;)) !=3D 0) {<br>
&gt; + if (chdir(distdir) !=3D 0) {<br>
&gt; snprintf(error, sizeof(error),<br>
&gt; -=C2=A0 =C2=A0 &quot;Could not change to directory %s: %s\n&quot;,<br>
&gt; -=C2=A0 =C2=A0 getenv(&quot;BSDINSTALL_DISTDIR&quot;), strerror(errno)=
);<br>
&gt; + &quot;Could not change to directory %s: %s\n&quot;,<br>
&gt; + distdir, strerror(errno));<br>
<br>
Breaks style(9).<br>
<br>
Jess<br>
<br>
&gt; conf.title =3D &quot;Error&quot;;<br>
&gt; bsddialog_msgbox(&amp;conf, error, 0, 0);<br>
&gt; 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>