Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sdottaka committed Dec 16, 2024
1 parent 76b5948 commit 852a4b1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 50 deletions.
53 changes: 5 additions & 48 deletions Src/CompareEngines/ByteComparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ ByteComparator::ByteComparator(const QuickCompareOptions * options)
, m_ignore_eol_diff(options->m_bIgnoreEOLDifference)
, m_ignore_blank_lines(options->m_bIgnoreBlankLines)
, m_ignore_numbers(options->m_bIgnoreNumbers)
, m_ignore_eof_newline_presence(options->m_bIgnoreEofNewlinePresence)
// state
, m_wsflag(false)
, m_eol0(false)
Expand Down Expand Up @@ -341,6 +340,8 @@ ByteComparator::COMP_RESULT ByteComparator::CompareBuffers(
}
else // don't skip blank lines, but still ignore eol difference
{
const char* ptr0b = ptr0;
const char* ptr1b = ptr1;
HandleSide0Eol((char **) &ptr0, end0, eof0);
HandleSide1Eol((char **) &ptr1, end1, eof1);

Expand All @@ -349,11 +350,13 @@ ByteComparator::COMP_RESULT ByteComparator::CompareBuffers(
// these flags mean possible split CR/LF
goto need_more;
}
if (!m_ignore_eof_newline_presence && (m_eol0 || m_eol1))
if (m_eol0 || m_eol1)
{
if ((!m_eol0 || !m_eol1) && (orig0 == end0 || orig1 == end1))
{
// one side had an end-of-line, but the other didn't
ptr0 = ptr0b;

Check warning

Code scanning / CodeQL

Local variable address stored in non-local memory Warning

A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
ptr1 = ptr1b;

Check warning

Code scanning / CodeQL

Local variable address stored in non-local memory Warning

A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
A stack address which arrived via a
parameter
may be assigned to a non-local variable.
result = RESULT_DIFF;
goto exit;
}
Expand All @@ -380,52 +383,6 @@ ByteComparator::COMP_RESULT ByteComparator::CompareBuffers(

if (ptr0 == end0 || ptr1 == end1)
{
if (m_ignore_eof_newline_presence)
{
if (eof0 && eof1)
{
const size_t rest0 = end0 - ptr0;
const size_t rest1 = end1 - ptr1;
if ((rest0 == 0 && rest1 == 0) ||
(rest0 == 1 && ((!m_cr0 && *ptr0 == '\r') || ((m_cr0 || !m_eol0) && *ptr0 == '\n'))) ||
(rest0 == 2 && (!m_eol0 && (*ptr0 == '\r' && *(ptr0 + 1) == '\n'))) ||
(rest1 == 1 && ((!m_cr1 && *ptr1 == '\r') || ((m_cr1 || !m_eol1) && *ptr1 == '\n'))) ||
(rest1 == 2 && (!m_eol1 && (*ptr1 == '\r' && *(ptr1 + 1) == '\n'))))
{
ptr0 = end0;
ptr1 = end1;
result = RESULT_SAME;
}
else
{
result = RESULT_DIFF;
}
goto exit;
}
else if (eof0 || eof1)
{
const size_t rest0 = end0 - ptr0;
const size_t rest1 = end1 - ptr1;
if ((rest0 == 0 && rest1 == 0) ||
(rest0 == 1 && (*ptr0 == '\r' || *ptr0 == '\n')) ||
(rest0 == 2 && (*ptr0 == '\r' && *(ptr0 + 1) == '\n')) ||
(rest1 == 1 && (*ptr1 == '\r' || *ptr1 == '\n')) ||
(rest1 == 2 && (*ptr1 == '\r' && *(ptr1 + 1) == '\n')))
{
if (end0 - ptr0 >= 1)
{
m_eol0 = true;
ptr0 = end0;
}
if (end1 - ptr1 >= 1)
{
m_eol1 = true;
ptr1 = end1;
}
goto need_more;
}
}
}
if (ptr0 == end0 && ptr1 == end1)
{
if (!eof0 || !eof1)
Expand Down
1 change: 0 additions & 1 deletion Src/CompareEngines/ByteComparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class ByteComparator
bool m_ignore_all_space; /**< Ignore all whitespace changes */
bool m_ignore_eol_diff; /**< Ignore differences in EOL bytes */
bool m_ignore_blank_lines; /**< Ignore blank lines */
bool m_ignore_eof_newline_presence; /**< Ignore end-of-file newline presence */
// state
bool m_wsflag; /**< ignore_space_change & in a whitespace area */
bool m_eol0; /**< 0-side has an eol */
Expand Down
31 changes: 30 additions & 1 deletion Src/CompareEngines/ByteCompare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ int ByteCompare::CompareFiles(DiffFileData* diffData)
// because transform code converted any UCS-2 files to UTF-8
// We could compare directly in UCS-2LE here, as an optimization, in that case
char buff[2][WMCMPBUFF]; // buffered access to files
std::string lasteol[2];
int i;
unsigned diffcode = 0;

Expand Down Expand Up @@ -126,11 +127,23 @@ int ByteCompare::CompareFiles(DiffFileData* diffData)
if (rtn < space)
eof[i] = true;
bfend[i] += rtn;
if (m_pOptions->m_bIgnoreEofNewlinePresence)
{
for (int j = bfstart[i]; j < bfend[i]; ++j)
{
const char c = buff[i][j];
if (c == '\r' || c == '\n')
lasteol[i].push_back(c);
else
lasteol[i].clear();
}
}
if (diffData->m_inf[0].desc == diffData->m_inf[1].desc)
{
bfstart[1] = bfstart[0];
bfend[1] = bfend[0];
eof[1] = eof[0];
lasteol[1] = lasteol[0];
diffData->m_FileLocation[1] = diffData->m_FileLocation[0];
memcpy(&buff[1][bfend[1] - rtn], &buff[0][bfend[0] - rtn], rtn);
break;
Expand Down Expand Up @@ -165,7 +178,21 @@ int ByteCompare::CompareFiles(DiffFileData* diffData)
}
else
{
diffcode |= DIFFCODE::DIFF;
if (m_pOptions->m_bIgnoreEofNewlinePresence)
{
if ((eof[0] || eof[1]) &&
((end0 - ptr0 <= 1 && (lasteol[0] == "\r" || lasteol[0] == "\n" || lasteol[0] == "\r\n") && (end1 == ptr1))) ||
((end0 - ptr0 == 2 && (lasteol[0] == "\r\n") && (end1 == ptr1))) ||
((end1 - ptr1 <= 1 && (lasteol[1] == "\r" || lasteol[1] == "\n" || lasteol[1] == "\r\n") && (end0 == ptr0))) ||
((end1 - ptr1 == 2 && (lasteol[1] == "\r\n") && (end0 == ptr0))))

Check notice

Code scanning / CodeQL

Complex condition Note

Complex condition: too many logical operations in this expression.
;
else
diffcode |= DIFFCODE::DIFF;
}
else
{
diffcode |= DIFFCODE::DIFF;
}
ptr0 = end0;
ptr1 = end1;
// move our current pointers over what we just compared
Expand Down Expand Up @@ -250,7 +277,9 @@ int ByteCompare::CompareFiles(DiffFileData* diffData)

// If either unfinished, they differ
if (ptr0 != end0 || ptr1 != end1)
{
diffcode = (diffcode & DIFFCODE::DIFF);
}
if (diffcode & DIFFCODE::DIFF)
return diffcode | DIFFCODE::DIFF;
else
Expand Down

0 comments on commit 852a4b1

Please sign in to comment.