Skip to content

Commit

Permalink
Fix potential unaligned access
Browse files Browse the repository at this point in the history
If the type is string, do not try to deference it as int16, int32 or int64.
This may lead to unalign memory access, which may cause trap on some architectures (ARM)
  • Loading branch information
kurddt committed Dec 9, 2016
1 parent 69e9c1f commit 69aaa49
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pb_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,15 @@ static bool checkreturn encode_basic_field(pb_ostream_t *stream,
if(bytes->size == 0)
implicit_has = false;
}
else if ((PB_LTYPE(field->type) == PB_LTYPE_STRING && *(const char*)pData == '\0') ||
(field->data_size == sizeof(uint_least8_t) && *(const uint_least8_t*)pData == 0) ||
(field->data_size == sizeof(uint_least16_t) && *(const uint_least16_t*)pData == 0) ||
(field->data_size == sizeof(uint32_t) && *(const uint_least32_t*)pData == 0) ||
(field->data_size == sizeof(uint64_t) && *(const uint_least64_t*)pData == 0))
else if (PB_LTYPE(field->type) == PB_LTYPE_STRING )
{
if( *(const char*)pData == '\0')
implicit_has = false;
}
else if ((field->data_size == sizeof(uint_least8_t) && *(const uint_least8_t*)pData == 0) ||
(field->data_size == sizeof(uint_least16_t) && *(const uint_least16_t*)pData == 0) ||
(field->data_size == sizeof(uint32_t) && *(const uint_least32_t*)pData == 0) ||
(field->data_size == sizeof(uint64_t) && *(const uint_least64_t*)pData == 0))
{
implicit_has = false;
}
Expand Down

0 comments on commit 69aaa49

Please sign in to comment.