Skip to content

Commit 78ed710

Browse files
committed
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 at 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, which 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 <[email protected]>
1 parent 0d50556 commit 78ed710

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

xdiff/xutils.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,20 @@ static unsigned long xdl_hash_record_with_whitespace(char const **data,
242242
for (; ptr < top && *ptr != '\n'; ptr++) {
243243
if (isspace(*ptr)) {
244244
const char *ptr2 = ptr;
245+
int at_eol;
245246
while (ptr + 1 < top && isspace(ptr[1])
246247
&& ptr[1] != '\n')
247248
ptr++;
249+
at_eol = (top <= ptr + 1 || ptr[1] == '\n');
248250
if (flags & XDF_IGNORE_WHITESPACE)
249251
; /* already handled */
250252
else if (flags & XDF_IGNORE_WHITESPACE_CHANGE
251-
&& ptr[1] != '\n') {
253+
&& !at_eol) {
252254
ha += (ha << 5);
253255
ha ^= (unsigned long) ' ';
254256
}
255257
else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL
256-
&& ptr[1] != '\n') {
258+
&& !at_eol) {
257259
while (ptr2 != ptr + 1) {
258260
ha += (ha << 5);
259261
ha ^= (unsigned long) *ptr2;

0 commit comments

Comments
 (0)