devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Frank Rowand <frowand.list-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>
Cc: jdl-CYoMK+44s/E@public.gmane.org,
	devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: [RFC PATCH v6 1/3] dtc: protect against null pointer dereference in srcpos_string()
Date: Tue, 06 Oct 2015 00:32:20 -0700	[thread overview]
Message-ID: <56137904.9080203@gmail.com> (raw)
In-Reply-To: <20151006041000.GI3861-RXTfZT5YzpxwFLYp8hBm2A@public.gmane.org>

On 10/5/2015 9:10 PM, David Gibson wrote:
> On Fri, Oct 02, 2015 at 09:49:08PM -0700, Frank Rowand wrote:
>> From: Frank Rowand <frank.rowand-/MT0OVThwyLZJqsBc5GL+g@public.gmane.org>
>>
>> Check for NULL pos before dereferencing it in srcpos_string().
>>
>> Signed-off-by: Frank Rowand <frank.rowand-/MT0OVThwyLZJqsBc5GL+g@public.gmane.org>
>> ---
>>  srcpos.c |    6 ++++--
>>  1 file changed, 4 insertions(+), 2 deletions(-)
>>
>> Index: b/srcpos.c
>> ===================================================================
>> --- a/srcpos.c
>> +++ b/srcpos.c
>> @@ -268,11 +268,13 @@ srcpos_string(struct srcpos *pos)
>>  	char *pos_str;
>>  	int rc;
>>  
>> -	if (pos)
>> +	if (pos && pos->file)
>>  		fname = pos->file->name;
>>  
>>  
>> -	if (pos->first_line != pos->last_line)
>> +	if (!pos)
>> +		rc = asprintf(&pos_str, "%s:<no-line>", fname);
>> +	else if (pos->first_line != pos->last_line)
> 
> This logic still seems backwards to me.  I'd really prefer the !pos
> check to go first, then !pos->file, then the normal case.
> 

Checking !pos first results in either an early return, a goto,
or more deeply nesting the

   if (pos->first_line != pos->last_line)
      asprintf(...)
   else if (...)
      asprintf(...)
   else
      rc = asprintf(...)

all of which seemed worse.  In the next version I'll change it to:

char *
srcpos_string(struct srcpos *pos)
{
        const char *fname = "<no-file>";
        char *pos_str;
        int rc;

        if (!pos) {
                rc = asprintf(&pos_str, "%s:<no-line>", fname);
                goto out;
        } else if (pos->file) {
                fname = pos->file->name;
        }

        if (pos->first_line != pos->last_line)
                rc = asprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
                              pos->first_line, pos->first_column,
                              pos->last_line, pos->last_column);
        else if (pos->first_column != pos->last_column)
                rc = asprintf(&pos_str, "%s:%d.%d-%d", fname,
                              pos->first_line, pos->first_column,
                              pos->last_column);
        else
                rc = asprintf(&pos_str, "%s:%d.%d", fname,
                              pos->first_line, pos->first_column);

out:
        if (rc == -1)
                die("Couldn't allocate in srcpos string");

        return pos_str;
}

  parent reply	other threads:[~2015-10-06  7:32 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-03  4:44 [RFC PATCH v6 0/2] dtc: dts source location annotation Frank Rowand
     [not found] ` <560F5D15.9060606-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-10-03  4:49   ` [RFC PATCH v6 1/3] dtc: protect against null pointer dereference in srcpos_string() Frank Rowand
     [not found]     ` <560F5E44.9080006-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-10-06  4:10       ` David Gibson
     [not found]         ` <20151006041000.GI3861-RXTfZT5YzpxwFLYp8hBm2A@public.gmane.org>
2015-10-06  7:32           ` Frank Rowand [this message]
     [not found]             ` <56137904.9080203-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-10-06 10:46               ` David Gibson
2015-10-03  4:52   ` [RFC PATCH v6 2/3] dtc: dts source location annotation Frank Rowand
     [not found]     ` <560F5F20.30709-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-10-06  4:56       ` David Gibson
     [not found]         ` <20151006045607.GK3861-RXTfZT5YzpxwFLYp8hBm2A@public.gmane.org>
2015-10-06  7:38           ` Frank Rowand
     [not found]             ` <56137A6B.6080805-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-10-07  5:27               ` David Gibson
2015-10-06  7:45           ` Frank Rowand
     [not found]             ` <56137C1E.8060005-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-10-07  5:32               ` David Gibson
     [not found]                 ` <20151007053246.GS3861-RXTfZT5YzpxwFLYp8hBm2A@public.gmane.org>
2015-10-07  6:58                   ` Frank Rowand
     [not found] ` <560F5FB5.3020602@gmail.com>
     [not found]   ` <560F5FB5.3020602-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-10-06  5:01     ` [RFC PATCH v6 3/3] dtc: dts source location annotation, short location format David Gibson
     [not found]       ` <20151006050114.GL3861-RXTfZT5YzpxwFLYp8hBm2A@public.gmane.org>
2015-10-06  7:32         ` Frank Rowand

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=56137904.9080203@gmail.com \
    --to=frowand.list-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
    --cc=david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org \
    --cc=devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=jdl-CYoMK+44s/E@public.gmane.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.
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).