public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "Maciej W. Rozycki" <macro@mips.com>
To: DJ Delorie <dj@redhat.com>
Cc: <schwab@suse.de>, <libc-alpha@sourceware.org>
Subject: Re: [PATCH v2] nisplus: Correct pwent parsing issue and resulting compilation error [BZ #23266]
Date: Mon, 18 Jun 2018 20:11:00 -0000	[thread overview]
Message-ID: <alpine.DEB.2.00.1806182048160.20622@tp.orcam.me.uk> (raw)
In-Reply-To: <xnlgbbnalq.fsf@greed.delorie.com>

On Mon, 18 Jun 2018, DJ Delorie wrote:

> "Maciej W. Rozycki" <macro@mips.com> writes:
> > -  if (len == 0 && numstr[len - 1] != '\0')
> > +  if (len == 0 || numstr[len - 1] != '\0')
> 
> I think this part of the patch(es) is good, but a second part might be
> needed to avoid future problems[*]:
> 
> +    if (len > 0)
>        strncpy (first_unused, numstr, len);

 Do you expect GCC to start complaining here sometime even if it cannot 
statically prove that `len' can sometimes be zero, which seems the case 
here?

 It would have to always complain then if it found `strncpy' called with 
the the length argument variable.  And I think that while calling 
`strncpy' with constant zero length might be silly (though not invalid!), 
it certainly is fine to call it with a variable length argument that is 
sometimes zero, especially if the likelihood of it being zero (or more 
generally, just the context of the call) does not justify avoiding the 
call in that case.  This also seems to be the case here, being the 
unlikely error path.

> It looks like that clause handles two cases (if not, ignore the rest of
> this email ;)...
> 
> 1. if we don't have a valid entry, create a zero-length temporary
>    entry.
> 
> 2. If the entry is too long to be nul-terminated (or otherwise isn't
>    nul-terminated), create a temporary nul-terminated one.
> 
> The strncpy is only needed for the second case.  Since we already know
> the length, and are going to nul-terminate it ourselves anyway, a memcpy
> would work just as well (but still need the above if-check), unless the
> nis server is allowed to return an entry with an embedded nul, in which
> case strncpy might prevent data leakage from whatever's after the nul.
> I would consider that a different kind of bug, though, and we're calling
> strtoul on the result anyway.
> 
> However, for the first case, we're always producing a string that causes
> the following "if (numstr[0] == '\0')" test to be true, so the only real
> purpose of going through all that code for zero-length strings is to
> make sure the buffer has room for the trailing nul :-P  (and we need the
> test for other cases anyway).

 Yes, and I wouldn't have written it like this if I were to create this 
piece of code from scratch, however...

> I don't think that's worth changing the code for, though.  Maybe a
> comment clarifying why it's the way it is, if we care that much ;-)

... as I noted in the discussion, this is legacy code, so I chose the path 
of least resistance by reusing correct code already present elsewhere 
(that we don't need to touch), because uniformity has a value too.  We 
could optimise it, rewrite for clarity, etc. across all the instances.  
But as you write, is it worth it?

 I'm not sure even if it's worth adding a comment here: if unsure, then 
run `git blame' and examine the relevant commit description.

  Maciej

  reply	other threads:[~2018-06-18 20:11 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-18 15:10 [PATCH] nisplus: Correct pwent parsing issue and resulting compilation error Maciej W. Rozycki
2018-06-18 15:25 ` Joseph Myers
2018-06-18 15:45   ` Maciej W. Rozycki
2018-06-18 15:49     ` Joseph Myers
2018-06-18 15:41 ` Andreas Schwab
2018-06-18 16:13   ` [PATCH v2] nisplus: Correct pwent parsing issue and resulting compilation error [BZ #23266] Maciej W. Rozycki
2018-06-18 19:44     ` DJ Delorie
2018-06-18 20:11       ` Maciej W. Rozycki [this message]
2018-06-18 20:40         ` DJ Delorie
2018-06-25 19:17     ` [PING][PATCH " Maciej W. Rozycki
2018-06-26  1:32       ` DJ Delorie
2018-06-27 20:14         ` [committed v3] nisplus: Correct pwent parsing issue and resulting build " Maciej W. Rozycki
2018-06-27 21:03           ` Joseph Myers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.00.1806182048160.20622@tp.orcam.me.uk \
    --to=macro@mips.com \
    --cc=dj@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=schwab@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).