summaryrefslogtreecommitdiffstats
path: root/pb_encode.c
diff options
context:
space:
mode:
authorGuillaume Lager <g.lager@innoseis.com>2016-12-09 10:02:08 +0100
committerGuillaume Lager <g.lager@innoseis.com>2016-12-09 16:38:29 +0100
commit69aaa491fbe57e7dbb2eded28248a6e5b570e535 (patch)
treed5d382612255b1c24ccd2a40c17d681b543ebe20 /pb_encode.c
parent69e9c1fc8162956feffa32e07a97c53bdb92f5ef (diff)
Fix potential unaligned access
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)
Diffstat (limited to 'pb_encode.c')
-rw-r--r--pb_encode.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/pb_encode.c b/pb_encode.c
index b0a736a2..00c381c0 100644
--- a/pb_encode.c
+++ b/pb_encode.c
@@ -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;
}