All of lore.kernel.org
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: Kevin Wolf <kwolf@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 2/3] dmg: use pread
Date: Mon, 10 May 2010 22:20:22 +0200	[thread overview]
Message-ID: <20100510202022.GA26186@lst.de> (raw)
In-Reply-To: <4BE7DAEC.4090405@redhat.com>

On Mon, May 10, 2010 at 12:07:40PM +0200, Kevin Wolf wrote:
> >  
> > -    info_begin=read_off(s->fd);
> > -    if(info_begin==0)
> > -	goto fail;
> > -    if(lseek(s->fd,info_begin,SEEK_SET)<0)
> > -	goto fail;
> 
> We seek to info_begin.
> 
> > -    if(read_uint32(s->fd)!=0x100)
> > -	goto fail;
> 
> Now we are at info_begin + 4
> 
> > -    if((count = read_uint32(s->fd))==0)
> > -	goto fail;
> 
> info_begin + 8
> 
> > -    info_end = info_begin+count;
> > -    if(lseek(s->fd,0xf8,SEEK_CUR)<0)
> 
> info_begin + 0x100
> 
> > +    info_begin = read_off(s->fd, offset);
> > +    if (info_begin == 0) {
> >  	goto fail;
> > +    }
> > +
> > +    if (read_uint32(s->fd, info_begin) != 0x100) {
> > +        goto fail;
> > +    }
> > +
> > +    count = read_uint32(s->fd, info_begin + 4);
> > +    if (count == 0) {
> > +        goto fail;
> > +    }
> > +    info_end = info_begin + count;
> > +
> > +    offset = info_begin + 0xfc;
> 
> So, wrong offset here?

Yeah, should be 0x100.  That's what you get for quickly doing hex
calculation in your head.

> > +	if (type == 0x6d697368 && count >= 244) {
> >  	    int new_size, chunk_count;
> > -	    if(lseek(s->fd,200,SEEK_CUR)<0)
> > -	        goto fail;
> > +
> > +            offset += 4;
> 
> Isn't this needed in the else case, too?

I don't think so.  For that case we previously did a

	lseek(s->fd,count-4,SEEK_CUR)

to undo the 4 byte advance done by the read.

> > -		s->sectors[i] = last_out_offset+read_off(s->fd);
> > -		s->sectorcounts[i] = read_off(s->fd);
> > -		s->offsets[i] = last_in_offset+read_off(s->fd);
> > -		s->lengths[i] = read_off(s->fd);
> > +		read_uint32(s->fd, offset);
> 
> This read is useless. offset += 4 alone should be enough.

Thanks, fixed.

> >  	    /* we need to buffer, because only the chunk as whole can be
> >  	     * inflated. */
> >  	    i=0;
> >  	    do {
> > -		ret = read(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i);
> > +		ret = pread(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i,
> > +                            s->offsets[chunk]);
> 
> This is in a loop, whereas the lseek was outside the loop. From the
> second iteration on you'll repeat the first read instead of advancing.

You're right.  The EINTR check confused me an I took this for just
retrying reads on EINTR.  Now this code i quite nasty for error returns
except EINTR because we'll subtract one from the i loop iteration,
yikes.  I'll just reuse the i variable to keep the same kind of bug
for both sides of the equation.

God, do I hate this code..

  reply	other threads:[~2010-05-10 20:20 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-07 14:55 [Qemu-devel] [PATCH 0/3] dmg updates Christoph Hellwig
2010-05-07 14:55 ` [Qemu-devel] [PATCH 1/3] dmg: fix reading of uncompressed chunks Christoph Hellwig
2010-05-07 14:56 ` [Qemu-devel] [PATCH 2/3] dmg: use pread Christoph Hellwig
2010-05-10 10:07   ` Kevin Wolf
2010-05-10 20:20     ` Christoph Hellwig [this message]
2010-05-11  8:00       ` Kevin Wolf
2010-05-12 14:31   ` [Qemu-devel] [PATCH 2/3 v2] " Christoph Hellwig
2010-05-07 14:56 ` [Qemu-devel] [PATCH 3/3] dmg: use qemu block API Christoph Hellwig
2010-05-12 14:31   ` [Qemu-devel] [PATCH 3/3 v2] " Christoph Hellwig
2010-05-12 15:51     ` Kevin Wolf

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=20100510202022.GA26186@lst.de \
    --to=hch@lst.de \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.