You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@subversion.apache.org by "Peter N. Lundblad" <pe...@famlundblad.se> on 2006/02/07 22:28:41 UTC

[PATCH] Fix corner case bug in libsvn_diff

Hi,

While testing the new diff stuff (ignore whitespace etc.), I changed the
token read chunk size from 128K to 4, and found an old corner case bug.
The problem is that the code will return a spurious empty token if the
file is exactly n*CHUNK_SIZE bytes long (n>0).  This confuses the diff
algorithm, which thinks that the file has an extra line.  Attached is a
fix for this, which seems a little clumsy.  If anyone has a better
solution, feel free to suggest/commit.

Note that this is in 1.3 (and probably earlier)

Thanks,
//Peter

[[[
Fix corner case in libsvn_diff, when a file has a length that is an exact
multiple of the read chunk size (128K).

* subversion/libsvn_diff/diff_file.c
  (svn_diff__file_datasource_get_next_token): Avoid returning a token
  of length 0.
]]]

Re: [PATCH] Fix corner case bug in libsvn_diff

Posted by Malcolm Rowe <ma...@farside.org.uk>.
On Wed, Feb 08, 2006 at 11:00:59PM +0100, Peter N. Lundblad wrote:
> This code isn't entirely straight. We *have* to handle 0 byte files.  NOte
> that the baton fields are not uninitialized; they are set to zero in the
> svn_diff_file_diff{,34} functions.
> 
> This code have served us well for years, but honestly it could need some
> more documentation in some places.
> 

Yup, and I'm obviously missing something about the way it's working too
(also, I wasn't able to reproduce the original problem either, so I'm
doubly confused).

Never mind though - if it works now, there's little point in fiddling
with it.  There are enough things that are actually broken without
needing to find others that could merely be polished!

Regards,
Malcolm

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] Fix corner case bug in libsvn_diff

Posted by "Peter N. Lundblad" <pe...@famlundblad.se>.
On Wed, 8 Feb 2006, Malcolm Rowe wrote:

> On Tue, Feb 07, 2006 at 11:28:41PM +0100, Peter N. Lundblad wrote:
> >    file_baton->curp[idx] = eol;
> > -  *token = file_token;
> > +  /* If the file length is exactly a multiple of CHUNK_SIZE, we will end up
> > +   * with a spurious empty token.  Avoid returning it. */
> > +  if (file_token->length > 0)
> > +    *token = file_token;
> >
> >    return SVN_NO_ERROR;
> >  }
>
> I'm not sure, but isn't this what the section at the top of the function
> is trying to guard against:
>
>   curp = file_baton->curp[idx];
>   endp = file_baton->endp[idx];
>
>   last_chunk = offset_to_chunk(file_baton->size[idx]);
>
>   if (curp == endp
>       && last_chunk == file_baton->chunk[idx])
>     {
>       return SVN_NO_ERROR;
>     }
>
That's what makes this function terminate early if there is no data,
regardless of whether it ended on a chunk boundry or not.


> ?  There's also some checking against last_chunk later on, apparently for
> files that are a multiple of the chunksize with no trailing EOL marker.

That check is for files with no eol at eof, regardless of the file size.

> Isn't the problem here actually that we shouldn't use offset_to_chunk()
> with a file _size_ directly? (a file of 128k will only have one chunk,
> so last_chunk should be zero until the file is 128k+1 bytes)
>
> I think the more correct fix would be just to change the calculation of
> last_chunk to something like:
>
>   last_chunk = offset_to_chunk(file_baton->size[idx] - 1);
>
> Untested, and I'm also assuming we don't have to deal with empty

It would be correct if it worked:-)  I tried that first, but then the last
chunk is being missed.

> (zero-length) files at this stage, since much of file_baton seems to
> be unintialised in that case (see svn_diff__file_datasource_open()),
> so we can't (for example) compare curp and endp meaningfully.
>
This code isn't entirely straight. We *have* to handle 0 byte files.  NOte
that the baton fields are not uninitialized; they are set to zero in the
svn_diff_file_diff{,34} functions.

This code have served us well for years, but honestly it could need some
more documentation in some places.

Malcolm, as I said, I think my fix looks a little hacky, so if you have
the time and energy, feel free to find a more proper fix.  A tip, if you
do try, is to set the chunk size to a small value like I did to really
make sure it works.

Reards,
//Peter

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] Fix corner case bug in libsvn_diff

Posted by Malcolm Rowe <ma...@farside.org.uk>.
On Tue, Feb 07, 2006 at 11:28:41PM +0100, Peter N. Lundblad wrote:
> Hi,
> 
> While testing the new diff stuff (ignore whitespace etc.), I changed the
> token read chunk size from 128K to 4, and found an old corner case bug.
> The problem is that the code will return a spurious empty token if the
> file is exactly n*CHUNK_SIZE bytes long (n>0).  This confuses the diff
> algorithm, which thinks that the file has an extra line.  Attached is a
> fix for this, which seems a little clumsy.  If anyone has a better
> solution, feel free to suggest/commit.
> 
>  
>    file_baton->curp[idx] = eol;
> -  *token = file_token;
> +  /* If the file length is exactly a multiple of CHUNK_SIZE, we will end up
> +   * with a spurious empty token.  Avoid returning it. */
> +  if (file_token->length > 0)
> +    *token = file_token;
>  
>    return SVN_NO_ERROR;
>  }

I'm not sure, but isn't this what the section at the top of the function
is trying to guard against:

  curp = file_baton->curp[idx];
  endp = file_baton->endp[idx];

  last_chunk = offset_to_chunk(file_baton->size[idx]);

  if (curp == endp
      && last_chunk == file_baton->chunk[idx])
    {
      return SVN_NO_ERROR;
    }

?  There's also some checking against last_chunk later on, apparently for
files that are a multiple of the chunksize with no trailing EOL marker.
Isn't the problem here actually that we shouldn't use offset_to_chunk()
with a file _size_ directly? (a file of 128k will only have one chunk,
so last_chunk should be zero until the file is 128k+1 bytes)

I think the more correct fix would be just to change the calculation of
last_chunk to something like:

  last_chunk = offset_to_chunk(file_baton->size[idx] - 1);

Untested, and I'm also assuming we don't have to deal with empty
(zero-length) files at this stage, since much of file_baton seems to
be unintialised in that case (see svn_diff__file_datasource_open()),
so we can't (for example) compare curp and endp meaningfully.

Regards,
Malcolm

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] Fix corner case bug in libsvn_diff

Posted by Julian Foad <ju...@btopenworld.com>.
Peter N. Lundblad wrote:
> 
> Attached is a fix for this, which seems a little clumsy.  If anyone
> has a better solution, feel free to suggest/commit.

> * subversion/libsvn_diff/diff_file.c
>   (svn_diff__file_datasource_get_next_token): Avoid returning a token
>   of length 0.

I'm having trouble determining how this is supposed to work.  Please, please 
could someone familiar with it expand the doc string a little?

Index: subversion/include/svn_diff.h
===================================================================
--- subversion/include/svn_diff.h       (revision 18347)
+++ subversion/include/svn_diff.h       (working copy)
@@ -112,5 +112,7 @@

-  /** Get the next "token" from the datasource of type @a datasource. */
+  /** Get the next "token" from the datasource of type @a datasource.
+   * Set @a *token to ... or to null if ... @a hash ...
+   */
    svn_error_t *(*datasource_get_next_token)(apr_uint32_t *hash, void **token,
                                              void *diff_baton,
                                              svn_diff_datasource_e datasource);

- Julian

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] Fix corner case bug in libsvn_diff

Posted by "Peter N. Lundblad" <pe...@famlundblad.se>.
On Wed, 8 Feb 2006, Sander Striker wrote:

> Peter N. Lundblad wrote:
> > Hi,
> >
> > While testing the new diff stuff (ignore whitespace etc.), I changed the
> > token read chunk size from 128K to 4, and found an old corner case bug.
> > The problem is that the code will return a spurious empty token if the
> > file is exactly n*CHUNK_SIZE bytes long (n>0).  This confuses the diff
> > algorithm, which thinks that the file has an extra line.  Attached is a
> > fix for this, which seems a little clumsy.  If anyone has a better
> > solution, feel free to suggest/commit.
>
> The fix makes sense.  Though I'd avoid calculating the hash as well.
>
Good point.  Will do.

> > Note that this is in 1.3 (and probably earlier)
>
> Yes, this has probably been there since the start.  Nice catch.

Just don't get the impression that I found it by looking at the code:-)

Thanks,
//Peter

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] Fix corner case bug in libsvn_diff

Posted by Sander Striker <st...@apache.org>.
Peter N. Lundblad wrote:
> Hi,
> 
> While testing the new diff stuff (ignore whitespace etc.), I changed the
> token read chunk size from 128K to 4, and found an old corner case bug.
> The problem is that the code will return a spurious empty token if the
> file is exactly n*CHUNK_SIZE bytes long (n>0).  This confuses the diff
> algorithm, which thinks that the file has an extra line.  Attached is a
> fix for this, which seems a little clumsy.  If anyone has a better
> solution, feel free to suggest/commit.

The fix makes sense.  Though I'd avoid calculating the hash as well.
 
> Note that this is in 1.3 (and probably earlier)

Yes, this has probably been there since the start.  Nice catch.


Sander

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org