summaryrefslogtreecommitdiffstats
path: root/pb_decode.c
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2016-01-27 18:59:54 +0200
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2016-01-27 20:34:46 +0200
commit3b6099faa2d829c74c9576f4aa1ce4b237c997db (patch)
treedc0822f496f2bd5bf4f03ffc80a22385bb086cd8 /pb_decode.c
parent8d35488bcde1cc6d912d4c875bcf6d2745899d7d (diff)
Fix a few remaining bugs related to CHAR_BIT!=8 platforms.
Diffstat (limited to 'pb_decode.c')
-rw-r--r--pb_decode.c59
1 files changed, 34 insertions, 25 deletions
diff --git a/pb_decode.c b/pb_decode.c
index b7a0093e..1699091c 100644
--- a/pb_decode.c
+++ b/pb_decode.c
@@ -1155,19 +1155,22 @@ static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *fi
* not break decoding of such messages, we cast <=32 bit fields to
* int32_t first to get the sign correct.
*/
- if (field->data_size == 8)
+ if (field->data_size == sizeof(int64_t))
svalue = (int64_t)value;
else
svalue = (int32_t)value;
- switch (field->data_size)
- {
- case 1: clamped = *(int8_t*)dest = (int8_t)svalue; break;
- case 2: clamped = *(int16_t*)dest = (int16_t)svalue; break;
- case 4: clamped = *(int32_t*)dest = (int32_t)svalue; break;
- case 8: clamped = *(int64_t*)dest = svalue; break;
- default: PB_RETURN_ERROR(stream, "invalid data_size");
- }
+ /* Cast to the proper field size, while checking for overflows */
+ if (field->data_size == sizeof(int64_t))
+ clamped = *(int64_t*)dest = svalue;
+ else if (field->data_size == sizeof(int32_t))
+ clamped = *(int32_t*)dest = (int32_t)svalue;
+ else if (field->data_size == sizeof(int_least16_t))
+ clamped = *(int_least16_t*)dest = (int_least16_t)svalue;
+ else if (field->data_size == sizeof(int_least8_t))
+ clamped = *(int_least8_t*)dest = (int_least8_t)svalue;
+ else
+ PB_RETURN_ERROR(stream, "invalid data_size");
if (clamped != svalue)
PB_RETURN_ERROR(stream, "integer too large");
@@ -1181,14 +1184,17 @@ static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *f
if (!pb_decode_varint(stream, &value))
return false;
- switch (field->data_size)
- {
- case 1: clamped = *(uint_least8_t*)dest = (uint_least8_t)value; break;
- case 2: clamped = *(uint_least16_t*)dest = (uint_least16_t)value; break;
- case 4: clamped = *(uint32_t*)dest = (uint32_t)value; break;
- case 8: clamped = *(uint64_t*)dest = value; break;
- default: PB_RETURN_ERROR(stream, "invalid data_size");
- }
+ /* Cast to the proper field size, while checking for overflows */
+ if (field->data_size == sizeof(uint64_t))
+ clamped = *(uint64_t*)dest = value;
+ else if (field->data_size == sizeof(uint32_t))
+ clamped = *(uint32_t*)dest = (uint32_t)value;
+ else if (field->data_size == sizeof(uint_least16_t))
+ clamped = *(uint_least16_t*)dest = (uint_least16_t)value;
+ else if (field->data_size == sizeof(uint_least8_t))
+ clamped = *(uint_least8_t*)dest = (uint_least8_t)value;
+ else
+ PB_RETURN_ERROR(stream, "invalid data_size");
if (clamped != value)
PB_RETURN_ERROR(stream, "integer too large");
@@ -1202,14 +1208,17 @@ static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *f
if (!pb_decode_svarint(stream, &value))
return false;
- switch (field->data_size)
- {
- case 1: clamped = *(int_least8_t*)dest = (int_least8_t)value; break;
- case 2: clamped = *(int_least16_t*)dest = (int_least16_t)value; break;
- case 4: clamped = *(int32_t*)dest = (int32_t)value; break;
- case 8: clamped = *(int64_t*)dest = value; break;
- default: PB_RETURN_ERROR(stream, "invalid data_size");
- }
+ /* Cast to the proper field size, while checking for overflows */
+ if (field->data_size == sizeof(int64_t))
+ clamped = *(int64_t*)dest = value;
+ else if (field->data_size == sizeof(int32_t))
+ clamped = *(int32_t*)dest = (int32_t)value;
+ else if (field->data_size == sizeof(int_least16_t))
+ clamped = *(int_least16_t*)dest = (int_least16_t)value;
+ else if (field->data_size == sizeof(int_least8_t))
+ clamped = *(int_least8_t*)dest = (int_least8_t)value;
+ else
+ PB_RETURN_ERROR(stream, "invalid data_size");
if (clamped != value)
PB_RETURN_ERROR(stream, "integer too large");