Commit Graph
109 Commits
Author SHA1 Message Date
Petteri Aimonen 4b455dafde Fix problem with [default=xxxx, (nanopb).proto3=true] field option combination (#558) 2020-09-10 13:53:45 +03:00
Petteri Aimonen 69e02afb6e Rework bytes size check (#547)
The previous check was prone to giving warnings on various platforms.
Also there is no real need to check for other than PB_ATYPE_STATIC, as
anyone passing a pointer variable is responsible for having correctly
allocated the array.
2020-06-23 13:28:21 +03:00
Petteri Aimonen 6801500e23 Fix clang undefined behavior sanitizer errors.
Signed and unsigned integer overflows, no actual consequences
but clean up the errors.
2020-06-23 13:21:16 +03:00
Petteri Aimonen 649c4337b0 Fix buffer overflow when encoding bytes with size set to 65535 (#547)
On platforms where size_t equals pb_size_t, for example AVR where both
are 16-bit, or x86 and ARM when PB_FIELD_32BIT is defined, the buffer size
checks in pb_write() and pb_enc_submessage can overflow if a bytes field
has size close to maximum size value. This causes read and write out of bounds.

This issue can cause a security vulnerability if the size of a bytes field
in the structure given to pb_encode() is untrusted. Note that pb_decode()
has correct bounds checking and will reject too large values.
2020-06-22 14:57:26 +03:00
Petteri Aimonen 0790dba6e9 Fix segfault with pointer fields and proto3_singular_msgs = true. (#504,#505)
If option proto3_singular_msgs was given and a proto3 submessage
contained a pointer field set to NULL, the encoder would crash with
null pointer dereference.
2020-06-22 10:45:25 +03:00
Petteri Aimonen f1319267a1 Fix compilation error on platforms without uint8_t, add test. (#485) 2020-01-29 18:59:36 +02:00
Petteri Aimonen a6ae0741a4 Move pb_const_cast to pb_common.c 2020-01-16 11:08:04 +02:00
Petteri Aimonen 9c0130ddf4 Move pb_validate_utf8() to pb_common.c (#437)
The validation function can be useful for user code also.
Also made the encoding check validity also, because it's
better to catch invalid messages in the transmitting end.

Added unittests for the validation.
2019-12-18 19:12:06 +02:00
Petteri Aimonen b9c7bbaeab Improve callback handling inside oneofs. (#175)
Removed PB_OLD_CALLBACK_STYLE. Added generator option
submsg_callback to have a message-level callback for
setting up callbacks inside oneofs. Added example/testcase
about using callbacks with oneofs.
2019-12-18 18:35:00 +02:00
Petteri Aimonen b6a7e45402 Fixes for AVR compiler warnings 2019-12-16 16:03:44 +02:00
Petteri Aimonen b435795e98 Fix unsigned integer overflow warning 2019-12-16 12:34:50 +02:00
Petteri Aimonen 511b708290 Add PB_CONVERT_DOUBLE_FLOAT setting to convert doubles on AVR.
This is easier to use than the old method of separate conversion functions.
More importantly, it should allow running the same tests on AVR target.
2019-12-16 12:14:16 +02:00
Petteri Aimonen 6f214ffb87 Avoid compiler warning in bool cast (#440) 2019-10-16 21:03:22 +03:00
Pavol Rusnak 6757959795 Fix "unused variable" warning when using PB_ENCODE_ARRAYS_UNPACKED 2019-10-10 19:48:45 +02:00
Petteri Aimonen e96a240ecb Missed one bool access in previous commit 2019-10-02 11:24:14 +03:00
Petteri Aimonen eab98ebbd6 Fix undefined behavior with bool fields (#434)
Previously nanopb didn't enforce that decoded bool fields
had valid true/false values. This could lead to undefined
behavior in user code.

This has potential security implications when

1) message contains bool field (has_ fields are safe)
and
2) user code uses ternary operator dependent on the field value,
   such as: int value = msg.my_bool ? 1234 : 0
and
3) the value returned from ternary operator affects a memory access,
   such as: data_array[value] = 9999
2019-10-02 11:16:51 +03:00
Pavol Rusnak 2e3d39d8ff Introduce new compile time flag: PB_ENCODE_ARRAYS_UNPACKED
Don't encode scalar arrays as packed. This is only to be used when
the decoder on the receiving side cannot process packed scalar arrays.
Such example is older protobuf.js.
2019-08-27 13:14:22 +02:00
Petteri Aimonen 95d614263c Don't call stream callback with count=0 (#421)
This could happen with zero-length submessages and strings.
While technically valid, it is an useless call and a rare
corner case that can easily lead to bugs in the callback
implementation.
2019-08-22 11:31:24 +03:00
Petteri Aimonen e31fa8eb03 Add pb_decode_ex(), pb_encode_ex() functions.
This should simplify specifying combinations, so that we don't
need every variant of pb_decode_noinit_delimited() etc.

Also disables zero-terminated decoding in pb_decode (issue #278).
2019-01-25 20:37:15 +02:00
Petteri Aimonen b71e4b7b2a Combine new per-msg encode/decode callbacks to one field_callback.
This seems like it will result in less boilerplate in typical situations
where a specific message is either decoded or encoded. Otherwise it would
result in lots of dummy functions like:

bool MyQuery_encode_callback(...)
{
 return false; /* MyQuery is never encoded */
}
2019-01-25 18:56:34 +02:00
Petteri Aimonen e605e188be Prepare for moving callback pointer to pb_msgdesc_t. 2019-01-25 18:56:34 +02:00
Petteri Aimonen 6a595279b2 New field descriptor format.
Now field information is stored as a variable-length uint32_t array.
This reduces the minimum size in most cases:

                            old            new
basic field:                10 bytes       4 bytes
repeated field:             10 bytes       8 bytes
field with 12bit tag/size:  16 bytes       8 bytes
field with 16bit tag/size:  16 bytes       16 bytes
field with 32bit tag/size:  28 bytes       16 bytes

Further, now the descriptor size can be increased per-field and per-message,
instead of previous global PB_FIELD_16BIT and PB_FIELD_32BIT.

PB_FIELD_32BIT still affects the pb_size_t type, which is 16-bit by default.
2019-01-25 18:56:34 +02:00
Petteri Aimonen 2210cd92a6 Big refactoring to better separate field descriptor encoding from logic.
This will enable further development of the field descriptor encoding.
2019-01-25 18:56:34 +02:00
Petteri Aimonen 9710d46d90 Try to compromise between avoiding 64-bit shifts and number of special cases. 2018-11-10 22:33:15 +02:00
Petteri Aimonen f2629e932a Fix warning with PB_WITHOUT_64BIT 2018-11-10 21:47:45 +02:00
Petteri Aimonen abf4798454 Merge branch 'dev_0.4' 2018-11-10 21:08:55 +02:00
Petteri Aimonen 6655ea52ee Fix splint test error (#359) 2018-11-02 09:09:53 +02:00
Oliver Lee bec486f2c1 Handle a single non-packed repeated fixed count field
Allow the elements of that single non-packed repeated fixed count field
to be interleaved among other (non-packed repeated fixed count) fields.
Multiple non-packed repeated fixed count fields are allowed if the
elements are not interleaved.

An error is returned if the count of a repeated fixed count field is not
equal the expected count.
2018-02-14 22:37:59 +01:00
Oliver Lee b28d29f987 Fix decoding of non-packable fixed count repeated fields 2018-02-14 22:37:59 +01:00
Oliver Lee fc41a5dad7 Allow fixed count repeated values 2018-02-14 14:24:45 +01:00
Sébastien Morin d8130ebcee Fixing some warning on the test for clang 2017-10-06 14:16:42 -04:00
Sébastien Morin 4a976e4e54 Fixing some warning on the test 2017-10-06 13:51:31 -04:00
Sébastien Morin 01e07315ba Adding a special case to encode a negative varint. 2017-10-05 10:50:56 -04:00
Petteri Aimonen e21e78c67c Add pb_en/decode_nullterminated() (part of #278)
Nanopb has traditionally supported messages to be terminated with a zero tag.
However, this is not really standard protobuf behaviour, so it makes sense
to separate it into a different function. Because it is a breaking change,
it will happen in 0.4.0 release. But I add the functions here early so that
new code can start using them now.

Also changed the network_server example to use en/decode_delimited(), which
is the more common protobuf method of simple message framing.
2017-09-16 13:40:57 +03:00
Petteri Aimonen c291fee05f Add option to build without 64-bit support (issue #86) 2017-09-16 09:28:08 +03:00
Petteri Aimonen c59a9d7e7a Combine pb_encode_varint32() into pb_encode_varint() 2017-05-05 07:53:21 +03:00
Petteri Aimonen 885d810fb0 Merge branch 'pb_encode32' of https://github.com/ewalt1/nanopb into dev_0.4 2017-05-05 07:36:07 +03:00
Petteri Aimonen fb735c0015 Make pb_enc_string() return an error for unterminated strings 2017-05-03 07:42:03 +03:00
Alice Wang bfeb7655dd The max size for encoding strings must leave spaces for a null terminator in the case where the field type isn't a pointer. 2017-05-03 07:42:02 +03:00
Petteri Aimonen 278ffb890e Fix bugs in proto3 mode encoding of submessages (#256)
pb_check_proto3_default_value() recurses into submessages,
but it didn't handle other than singular fields correctly.
This caused it to sometimes skip submessages with only
repeated or oneof fields present.
2017-04-14 11:46:18 +03:00
Nick Ewalt 4b3b4b1bab Add pb_encode_varint32 and pb_encode_svarint32
- Optimized 32 bit versions of pb_encode_varint and pb_encode_svarint
2017-03-15 16:08:06 -07:00
Petteri Aimonen 019d7bf948 Fix build failure (const_cast name conflict)
Thought I was cleaning up the naming by calling the function const_cast(),
because that's what it does. But of course it conflicts with the C++ builtin,
so renaming it to pb_const_cast() now.
2017-02-25 21:47:24 +02:00
Petteri Aimonen 9fdea3a35e Make pb_check_proto3_default_value() recurse into submessages (issue #247) 2017-02-25 21:23:10 +02:00
Justin DeMartino 119d0fd396 Fix infinite loop in pb_check_proto3_default_value
- Occurs with proto3, PB_FIELD_16BIT and submessage > 255 bytes
- Add test case for PB_FIELD_16BIT and proto3
2017-02-24 21:01:43 -08:00
Petteri Aimonen 07375a1263 Extend inline / fixed length bytes array support (issue #244)
Adds support for proto3 and POINTER field types to have
fixed length bytes arrays. Also changed the .proto option
to a separate fixed_length:true, while also supporting the old FT_INLINE
option.

Restructured the generator and decoder logic to threat the inline
bytes fields more like "just another field type".
2017-02-22 21:10:26 +02:00
Petteri Aimonen 58af4d1fb7 Enable clang integer sanitizer and clean up a few warnings.
Changed to use simple indexing instead of while (count--) in buf_read()/buf_write(),
because the count overflowed from 0 to max on the last iteration. While the unsigned
integer overflow is defined and behaviour was correct, making this simple change
allowed enabling the sanitizer which might catch true errors elsewhere in the code.
2016-12-09 18:57:08 +02:00
Petteri Aimonen bb78ba5164 Refactor proto3 logic into pb_check_proto3_default_value()
Mainly to clean up the code. Also fixed handling of
PB_LTYPE_FIXED_LENGTH_BYTES for proto3 files.
2016-12-09 18:37:55 +02:00
Guillaume Lager 69aaa491fb 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)
2016-12-09 16:38:29 +01:00
berni155 be75cf4d60 Fix typo in encoding 2016-10-10 21:53:53 +02:00
Bernhard Krämer ee44d0cee9 Prevent fields with default value from encoding when proto3 option is set 2016-10-09 20:27:28 +02:00