zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: zsh workers <zsh-workers@zsh.org>
Subject: Re: Fishy code in sticky emulation?
Date: Mon, 05 Jan 2015 14:48:19 +0000	[thread overview]
Message-ID: <20150105144819.2f768fcb@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <CAHYJk3SjCocOpnoS5K_N=J7CWKmVBTgrUtryLLf+cu6xLqxZ8w@mail.gmail.com>

On Mon, 5 Jan 2015 15:34:00 +0100
Mikael Magnusson <mikachu@gmail.com> wrote:
> I'm looking through Coverity issues (some patches to come later), and
> it flagged this in builtin.c that I can't quite say for sure if it's
> right or wrong about.
> 
> int
> bin_emulate(UNUSED(char *nam), char **argv, Options ops, UNUSED(int func))
> {
> ...
>     if (sticky->n_on_opts)
>       on_ptr = sticky->on_opts =
>         zhalloc(sticky->n_on_opts * sizeof(*sticky->on_opts));
>     else
>       on_ptr = NULL;
>     if (sticky->n_off_opts)
>       off_ptr = sticky->off_opts = zhalloc(sticky->n_off_opts *
>                                    sizeof(*sticky->off_opts));
>     else
>       off_ptr = NULL;
>     for (optnode = firstnode(optlist); optnode; incnode(optnode)) {
>       /* Data is index into new_opts */
>       char *optptr = (char *)getdata(optnode);
>       int optno = optptr - new_opts;
>       if (*optptr)
>         *on_ptr++ = optno;
>       else
>         *off_ptr++ = optno;
>       }
> ...
> 
> In particular, on_ptr and off_ptr can be NULL, but unconditionally one
> of them is always incremented in the for loop, which isn't very well
> defined for a NULL pointer. Am I missing something, or are these
> n_*_opts simply never 0?

You missed out the previous loop which is exactly parallel to the second
one you quoted:

    for (optnode = firstnode(optlist); optnode; incnode(optnode)) {
	/* Data is index into new_opts */
	char *optptr = (char *)getdata(optnode);
	if (*optptr)
	    sticky->n_on_opts++;
	else
	    sticky->n_off_opts++;
    }

This means that in the second loop on_ptr must be non-NULL whenever
*optptr is non-NULL and off_ptr must be non-NULL whenever it's NULL.

However, it would be fine to make the test for the pointers more
explicit; it's not going to make any noticeable difference even if you
run emulate very frequently.

pws


  reply	other threads:[~2015-01-05 14:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-05 14:34 Mikael Magnusson
2015-01-05 14:48 ` Peter Stephenson [this message]
2015-01-05 23:21   ` Mikael Magnusson

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=20150105144819.2f768fcb@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.com \
    --cc=zsh-workers@zsh.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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 NNTP newsgroup(s).