[
https://issues.apache.org/jira/browse/TS-4313?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15218148#comment-15218148
]
ASF GitHub Bot commented on TS-4313:
------------------------------------
Github user jpeach commented on the pull request:
https://github.com/apache/trafficserver/pull/542#issuecomment-203489537
``ptrdiff_t`` is the right type for pointer arithmetic.
That said, I don't really understand the problem here. If ``field`` is not
in the ``MIMEFieldBlockImpl`` you are scanning, then the pointer arithmetic is
not valid in the first place. For the pointer arithmetic to overflow 32bits,
the distance between the pointers would need to be ``2^32 *
sizeof(MIMEFieldBlockImpl)``, which seems unlikely.
A potential real problem here could be the compiler assuming that the
comparison can't happen, since pointer arithmetic across different objects is
not standards compliant, see [here](https://lwn.net/Articles/278137/). To
address this, I'd add ``MIMEFieldBlockImpl::contains(MIMEField*)``,
```C
int
mime_hdr_field_slotnum(MIMEHdrImpl *mh, MIMEField *field)
{
int slots_so_far;
MIMEFieldBlockImpl *fblock;
slots_so_far = 0;
for (fblock = &(mh->m_first_fblock); fblock != NULL; fblock =
fblock->m_next) {
if (block->contains(field)) {
MIMEField *first = &(fblock->m_field_slots[0]);
ptrdiff_t block_slot = field - first; // in units of MIMEField
return (slots_so_far + block_slot);
}
slots_so_far += MIME_FIELD_BLOCK_SLOTS;
}
return -1;
}
```
Can you think of a way to write tests for this?
> MIMEHdr fails to find header fields
> -----------------------------------
>
> Key: TS-4313
> URL: https://issues.apache.org/jira/browse/TS-4313
> Project: Traffic Server
> Issue Type: Bug
> Components: MIME
> Reporter: Masakazu Kitajo
> Assignee: Masakazu Kitajo
> Fix For: 6.2.0
>
>
> MIMEHdr fails to find a MIMEField occasionally due to improper type
> conversion.
> It happens if the lower 32 bits of addresses of m_field_slots are the same.
> The logic below picks up wrong block.
> mime_hdr_field_slotnum():
> {code}
> for (fblock = &(mh->m_first_fblock); fblock != NULL; fblock = fblock->m_next)
> {
> MIMEField *first = &(fblock->m_field_slots[0]);
> int block_slot = (int)(field - first); // in units of MIMEField
> if ((block_slot >= 0) && (block_slot < MIME_FIELD_BLOCK_SLOTS))
> {code}
> The type of block_slot should be intptr_t.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)