
|
If you were logged in you would be able to see more operations.
|
|
|
| Planning Status: |
Unspecified
|
|
Current code:
------------------ [BEGIN]
while (differences < end && p < p_end)
{
const SSHORT l = *differences++;
if (l > 0)
{
if (p + l > p_end)
{
BUGCHECK(177); /* msg 177 applied differences will not fit in record */
}
memcpy(p, differences, l); // <--------- CAN READ AFTER LAST ELEMENT OF differences
------------------ [END]
I offer to rewrite this function
------------------ [BEGIN]
USHORT SQZ_apply_differences(Record* const record, const SCHAR* differences, const SCHAR* const end)
{
/**************************************
*
* S Q Z _ a p p l y _ d i f f e r e n c e s
*
**************************************
*
* Functional description
* Apply a differences (delta) to a record. Return the length.
*
**************************************/
typedef USHORT result_type;
fb_assert(record != NULL);
fb_assert(differences <= end);
if (end - differences > MAX_DIFFERENCES)
{
BUGCHECK(176); /* msg 176 bad difference record */
}
SCHAR* p = reinterpret_cast<SCHAR*>(record->rec_data);
const SCHAR* const p_beg = p;
const SCHAR* const p_end = p_beg + record->rec_length;
while (differences < end)
{
const SSHORT l = *differences;
++differences;
//fb_assert(differences <= end);
fb_assert(p >= p_beg);
fb_assert(p <= p_end);
if (l > 0)
{
if (static_cast<size_t>(p_end - p) < static_cast<size_t>(l))
{
BUGCHECK(177); /* msg 177 applied differences will not fit in record */
}
if (static_cast<size_t>(end - differences) < static_cast<size_t>(l)) //<---- an important test
{
BUGCHECK(177); /* or 176? */
}
memcpy(p, differences, l);
p += l;
differences += l;
}
else
{
if (static_cast<size_t>(p_end - p) < static_cast<size_t>(-l))
{
BUGCHECK(177); /* msg 177 applied differences will not fit in record */
}
p += -l;
}//else
}//while (differences < end)
fb_assert(differences == end);
fb_assert(p >= p_beg);
fb_assert(p <= p_end);
//fb_assert(can_numeric_cast<result_type>(p - p_beg));
const result_type length = static_cast<result_type>(p - p_beg);
fb_assert(length <= record->rec_length);
return length;
}//SQZ_apply_differences
------------------ [END]
|
|
Description
|
Current code:
------------------ [BEGIN]
while (differences < end && p < p_end)
{
const SSHORT l = *differences++;
if (l > 0)
{
if (p + l > p_end)
{
BUGCHECK(177); /* msg 177 applied differences will not fit in record */
}
memcpy(p, differences, l); // <--------- CAN READ AFTER LAST ELEMENT OF differences
------------------ [END]
I offer to rewrite this function
------------------ [BEGIN]
USHORT SQZ_apply_differences(Record* const record, const SCHAR* differences, const SCHAR* const end)
{
/**************************************
*
* S Q Z _ a p p l y _ d i f f e r e n c e s
*
**************************************
*
* Functional description
* Apply a differences (delta) to a record. Return the length.
*
**************************************/
typedef USHORT result_type;
fb_assert(record != NULL);
fb_assert(differences <= end);
if (end - differences > MAX_DIFFERENCES)
{
BUGCHECK(176); /* msg 176 bad difference record */
}
SCHAR* p = reinterpret_cast<SCHAR*>(record->rec_data);
const SCHAR* const p_beg = p;
const SCHAR* const p_end = p_beg + record->rec_length;
while (differences < end)
{
const SSHORT l = *differences;
++differences;
//fb_assert(differences <= end);
fb_assert(p >= p_beg);
fb_assert(p <= p_end);
if (l > 0)
{
if (static_cast<size_t>(p_end - p) < static_cast<size_t>(l))
{
BUGCHECK(177); /* msg 177 applied differences will not fit in record */
}
if (static_cast<size_t>(end - differences) < static_cast<size_t>(l)) //<---- an important test
{
BUGCHECK(177); /* or 176? */
}
memcpy(p, differences, l);
p += l;
differences += l;
}
else
{
if (static_cast<size_t>(p_end - p) < static_cast<size_t>(-l))
{
BUGCHECK(177); /* msg 177 applied differences will not fit in record */
}
p += -l;
}//else
}//while (differences < end)
fb_assert(differences == end);
fb_assert(p >= p_beg);
fb_assert(p <= p_end);
//fb_assert(can_numeric_cast<result_type>(p - p_beg));
const result_type length = static_cast<result_type>(p - p_beg);
fb_assert(length <= record->rec_length);
return length;
}//SQZ_apply_differences
------------------ [END]
|
Show » |
|
changes, however clearer and possibly more correct, will reduce performance
measurably unless the c++ compilers have improved enormously. Do look
at the generated code and test performance before you make that change.