git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Thell Fowler <git@tbfowler.name>
Cc: git@vger.kernel.org, Johannes.Schindelin@gmx.de
Subject: Re: [PATCH-v2/RFC 2/6] xutils: fix hash with whitespace on incomplete line
Date: Sun, 23 Aug 2009 00:51:09 -0700	[thread overview]
Message-ID: <7veir3ynma.fsf@alter.siamese.dyndns.org> (raw)
In-Reply-To: 1250999357-10827-2-git-send-email-git@tbfowler.name

Thell Fowler <git@tbfowler.name> writes:

>   - Make xdl_hash_record_with_whitespace stop hashing before the
>     eof when ignoring space change or space at eol on an incomplete
>     line.
>
>   Resolves issue with a final trailing space being included in the
>   hash on an incomplete line by treating the eof in the same fashion
>   as a newline.

Please study the style of existing commit messages and imitate them.

> Signed-off-by: Thell Fowler <git@tbfowler.name>
> ---
>  xdiff/xutils.c |    4 ++--
>  1 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/xdiff/xutils.c b/xdiff/xutils.c
> index 04ad468..c6512a5 100644
> --- a/xdiff/xutils.c
> +++ b/xdiff/xutils.c
> @@ -248,12 +248,12 @@ static unsigned long xdl_hash_record_with_whitespace(char const **data,
>  			if (flags & XDF_IGNORE_WHITESPACE)
>  				; /* already handled */
>  			else if (flags & XDF_IGNORE_WHITESPACE_CHANGE
> -					&& ptr[1] != '\n') {
> +					&& ptr[1] != '\n' && ptr + 1 < top) {
>  				ha += (ha << 5);
>  				ha ^= (unsigned long) ' ';
>  			}
>  			else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL
> -					&& ptr[1] != '\n') {
> +					&& ptr[1] != '\n' && ptr + 1 < top) {
>  				while (ptr2 != ptr + 1) {
>  					ha += (ha << 5);
>  					ha ^= (unsigned long) *ptr2;

Thanks.

The issue you identified and tried to fix is a worthy one.  But before the
pre-context of this hunk, I notice these lines:

		if (isspace(*ptr)) {
			const char *ptr2 = ptr;
			while (ptr + 1 < top && isspace(ptr[1])
					&& ptr[1] != '\n')
				ptr++;

If you have trailing whitespaces on an incomplete line, ptr initially
points at the first such whitespace, ptr2 points at the same location, and
then the while() loop advances ptr to point at the last byte on the line,
which in turn will be the last byte of the file.  And the codepath with
your updates still try to access ptr[1] that is beyond that last byte.

I would write it like this patch instead.

The intent is the same as your patch, but it avoids accessing ptr[1] when
that is beyond the end of the buffer, and the logic is easier to follow as
well.

-- >8 --
Subject: xutils: fix hashing an incomplete line with whitespaces at the end

Upon seeing a whitespace, xdl_hash_record_with_whitespace() first skipped
the run of whitespaces (excluding LF) that begins there, ensuring that the
pointer points the last whitespace character in the run, and assumed that
the next character must be LF at the end of the line.  This does not work
when hashing an incomplete line, that lacks the LF at the end.

Introduce "at_eol" variable that is true when either we are at the end of
line (looking at LF) or at the end of an incomplete line, and use that
instead throughout the code.

Noticed by Thell Fowler.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 xdiff/xutils.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/xdiff/xutils.c b/xdiff/xutils.c
index 04ad468..9411fa9 100644
--- a/xdiff/xutils.c
+++ b/xdiff/xutils.c
@@ -242,18 +242,20 @@ static unsigned long xdl_hash_record_with_whitespace(char const **data,
 	for (; ptr < top && *ptr != '\n'; ptr++) {
 		if (isspace(*ptr)) {
 			const char *ptr2 = ptr;
+			int at_eol;
 			while (ptr + 1 < top && isspace(ptr[1])
 					&& ptr[1] != '\n')
 				ptr++;
+			at_eol = (top <= ptr + 1 || ptr[1] == '\n');
 			if (flags & XDF_IGNORE_WHITESPACE)
 				; /* already handled */
 			else if (flags & XDF_IGNORE_WHITESPACE_CHANGE
-					&& ptr[1] != '\n') {
+				 && !at_eol) {
 				ha += (ha << 5);
 				ha ^= (unsigned long) ' ';
 			}
 			else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL
-					&& ptr[1] != '\n') {
+				 && !at_eol) {
 				while (ptr2 != ptr + 1) {
 					ha += (ha << 5);
 					ha ^= (unsigned long) *ptr2;

  reply	other threads:[~2009-08-23  7:51 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-04 23:33 Help/Advice needed on diff bug in xutils.c Thell Fowler
2009-08-05 20:45 ` Johannes Schindelin
2009-08-10 18:54   ` Thell Fowler
2009-08-12  0:47   ` [PATCH/RFC] Add diff tests for trailing-space and now newline Thell Fowler
2009-08-19 23:05 ` [PATCH 0/6 RFC] Series to correct xutils incomplete line handling Thell Fowler
2009-08-21 17:39   ` Thell Fowler
2009-08-21 22:16     ` Alex Riesen
2009-08-22  4:23       ` Thell Fowler
     [not found] ` <cover.1250719760.git.git@tbfowler.name>
2009-08-19 23:06   ` [PATCH 1/6] Add supplemental test for trailing-whitespace on incomplete lines Thell Fowler
2009-08-19 23:06   ` [PATCH 2/6] Make xdl_hash_record_with_whitespace ignore eof Thell Fowler
2009-08-19 23:07   ` [PATCH 3/6] Make diff -w handle trailing-spaces on incomplete lines Thell Fowler
2009-08-20 23:09     ` Thell Fowler
2009-08-19 23:07   ` [PATCH 4/6] Make diff -b " Thell Fowler
2009-08-19 23:08   ` [PATCH 5/6] Make diff --ignore-space-at-eol handle " Thell Fowler
2009-08-19 23:09   ` [PATCH 6/6] Add diff tests for trailing-space on " Thell Fowler
2009-08-23  3:47 ` [PATCH-v2/RFC 0/6] improvements for trailing-space processing " Thell Fowler
2009-08-23  3:49   ` [PATCH-v2/RFC 1/6] Add supplemental test for trailing-whitespace " Thell Fowler
2009-08-23  3:49   ` [PATCH-v2/RFC 2/6] xutils: fix hash with whitespace on incomplete line Thell Fowler
2009-08-23  7:51     ` Junio C Hamano [this message]
2009-08-23 17:02       ` Thell Fowler
2009-08-23  3:49   ` [PATCH-v2/RFC 3/6] xutils: fix ignore-all-space " Thell Fowler
2009-08-23  7:57     ` Junio C Hamano
2009-08-23  8:18       ` Nanako Shiraishi
2009-08-23  8:56         ` Junio C Hamano
2009-08-23 21:07           ` Nanako Shiraishi
2009-08-23 21:14             ` Junio C Hamano
2009-08-23 22:13             ` Thell Fowler
2009-08-23 22:30               ` Junio C Hamano
2009-08-24  4:16                 ` [PATCH] Teach mailinfo to ignore everything before -- >8 -- mark Nicolas Sebrecht
2009-08-24  4:51                   ` Junio C Hamano
2009-08-24  5:36                     ` Junio C Hamano
2009-08-24  6:21                       ` [PATCH] " Nicolas Sebrecht
2009-08-24  6:58                         ` Junio C Hamano
2009-08-24  7:31                           ` Nicolas Sebrecht
2009-08-24 14:02                             ` Don Zickus
2009-08-24 21:48                               ` Junio C Hamano
2009-08-24  5:16                   ` [PATCH] " Nanako Shiraishi
2009-08-24  7:17                     ` [PATCH] " Nicolas Sebrecht
2009-08-24  7:24                       ` Nicolas Sebrecht
2009-08-24 22:17                       ` Junio C Hamano
2009-08-25 16:18                         ` Nicolas Sebrecht
2009-08-26  1:51                           ` Junio C Hamano
     [not found]                             ` <20090826110332.6117@nanako3.lavabit.com>
2009-08-26  2:20                               ` Junio C Hamano
2009-08-26  3:03                             ` Junio C Hamano
2009-08-26  5:02                               ` Nicolas Sebrecht
2009-08-26  8:57                                 ` Jakub Narebski
2009-08-26  9:00                                   ` Johannes Schindelin
2009-08-27  5:46                                     ` Junio C Hamano
2009-08-27 10:49                                       ` Johannes Schindelin
2009-08-26  9:03                                   ` Junio C Hamano
2009-08-26  3:54                             ` Nicolas Sebrecht
2009-08-24  8:09                     ` [PATCH] " Nanako Shiraishi
2009-08-23 17:01       ` [PATCH-v2/RFC 3/6] xutils: fix ignore-all-space on incomplete line Thell Fowler
2009-08-23 19:40         ` Junio C Hamano
2009-08-23 20:33           ` Thell Fowler
2009-08-23 21:11             ` Junio C Hamano
2009-08-24  3:26               ` Thell Fowler
2009-08-24  6:02                 ` Junio C Hamano
2009-08-24 14:13                   ` Thell Fowler
2009-08-25  5:58                     ` Thell Fowler
2009-08-23  3:49   ` [PATCH-v2/RFC 4/6] xutils: fix ignore-space-change " Thell Fowler
2009-08-23  3:49   ` [PATCH-v2/RFC 5/6] xutils: fix ignore-space-at-eol " Thell Fowler
2009-08-23  3:49   ` [PATCH-v2/RFC 6/6] t4015: add tests for trailing-space " Thell Fowler

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=7veir3ynma.fsf@alter.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@tbfowler.name \
    --cc=git@vger.kernel.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).