aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2019-12-31 13:07:17 +0200
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2019-12-31 13:38:53 +0200
commitced3bb2478b018ad463143c272a4e6315e265fef (patch)
treefbd63b9428d40dd79fa59d20b75219f3e86dea36
parentddaa999787108c12db76e7525f7340f9066ef547 (diff)
downloadnanopb-c-ced3bb2478b018ad463143c272a4e6315e265fef.tar.gz
Add workaround for avr-libc realloc() bug (#475)
-rw-r--r--pb_decode.c10
1 files changed, 10 insertions, 0 deletions
diff --git a/pb_decode.c b/pb_decode.c
index 1779503..5195f88 100644
--- a/pb_decode.c
+++ b/pb_decode.c
@@ -491,6 +491,16 @@ static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t
if (data_size == 0 || array_size == 0)
PB_RETURN_ERROR(stream, "invalid size");
+#ifdef __AVR__
+ /* Workaround for AVR libc bug 53284: http://savannah.nongnu.org/bugs/?53284
+ * Realloc to size of 1 byte can cause corruption of the malloc structures.
+ */
+ if (data_size == 1 && array_size == 1)
+ {
+ data_size = 2;
+ }
+#endif
+
/* Check for multiplication overflows.
* This code avoids the costly division if the sizes are small enough.
* Multiplication is safe as long as only half of bits are set