Skip to content

Commit

Permalink
Fix compilation error on platforms without uint8_t, add test. (nanopb…
Browse files Browse the repository at this point in the history
  • Loading branch information
PetteriAimonen committed Jan 29, 2020
1 parent 6a85a16 commit f131926
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion generator/nanopb_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ def fields_declaration(self, dependencies):
defval = self.default_value(dependencies)
if defval:
hexcoded = ''.join("\\x%02x" % ord(defval[i:i+1]) for i in range(len(defval)))
result += '#define %s_DEFAULT (const uint8_t*)"%s\\x00"\n' % (self.name, hexcoded)
result += '#define %s_DEFAULT (const pb_byte_t*)"%s\\x00"\n' % (self.name, hexcoded)
else:
result += '#define %s_DEFAULT NULL\n' % self.name

Expand Down
12 changes: 6 additions & 6 deletions pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -684,12 +684,12 @@ struct pb_extension_s {

#define PB_FIELDINFO_4(tag, type, data_offset, data_size, size_offset, array_size) \
(2 | (((tag) << 2) & 0xFF) | ((type) << 8) | (((uint32_t)(array_size) & 0xFFFF) << 16)), \
((uint32_t)(int8_t)(size_offset) | (((uint32_t)(tag) << 2) & 0xFFFFFF00)), \
((uint32_t)(int_least8_t)(size_offset) | (((uint32_t)(tag) << 2) & 0xFFFFFF00)), \
(data_offset), (data_size),

#define PB_FIELDINFO_8(tag, type, data_offset, data_size, size_offset, array_size) \
(3 | (((tag) << 2) & 0xFF) | ((type) << 8)), \
((uint32_t)(int8_t)(size_offset) | (((uint32_t)(tag) << 2) & 0xFFFFFF00)), \
((uint32_t)(int_least8_t)(size_offset) | (((uint32_t)(tag) << 2) & 0xFFFFFF00)), \
(data_offset), (data_size), (array_size), 0, 0, 0,

/* These assertions verify that the field information fits in the allocated space.
Expand All @@ -709,20 +709,20 @@ struct pb_extension_s {
#ifndef PB_FIELD_32BIT
/* Maximum field sizes are still 16-bit if pb_size_t is 16-bit */
#define PB_FIELDINFO_ASSERT_4(tag, type, data_offset, data_size, size_offset, array_size) \
PB_STATIC_ASSERT(PB_FITS(tag,16) && PB_FITS(data_offset,16) && PB_FITS((int8_t)size_offset,8) && PB_FITS(data_size,16) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width4_field ## tag)
PB_STATIC_ASSERT(PB_FITS(tag,16) && PB_FITS(data_offset,16) && PB_FITS((int_least8_t)size_offset,8) && PB_FITS(data_size,16) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width4_field ## tag)

#define PB_FIELDINFO_ASSERT_8(tag, type, data_offset, data_size, size_offset, array_size) \
PB_STATIC_ASSERT(PB_FITS(tag,16) && PB_FITS(data_offset,16) && PB_FITS((int8_t)size_offset,8) && PB_FITS(data_size,16) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width8_field ## tag)
PB_STATIC_ASSERT(PB_FITS(tag,16) && PB_FITS(data_offset,16) && PB_FITS((int_least8_t)size_offset,8) && PB_FITS(data_size,16) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width8_field ## tag)
#else
/* Up to 32-bit fields supported.
* Note that the checks are against 31 bits to avoid compiler warnings about shift wider than type in the test.
* I expect that there is no reasonable use for >2GB messages with nanopb anyway.
*/
#define PB_FIELDINFO_ASSERT_4(tag, type, data_offset, data_size, size_offset, array_size) \
PB_STATIC_ASSERT(PB_FITS(tag,30) && PB_FITS(data_offset,31) && PB_FITS((int8_t)size_offset,8) && PB_FITS(data_size,31) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width4_field ## tag)
PB_STATIC_ASSERT(PB_FITS(tag,30) && PB_FITS(data_offset,31) && PB_FITS(size_offset,8) && PB_FITS(data_size,31) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width4_field ## tag)

#define PB_FIELDINFO_ASSERT_8(tag, type, data_offset, data_size, size_offset, array_size) \
PB_STATIC_ASSERT(PB_FITS(tag,30) && PB_FITS(data_offset,31) && PB_FITS((int8_t)size_offset,8) && PB_FITS(data_size,31) && PB_FITS(array_size,31), FIELDINFO_DOES_NOT_FIT_width8_field ## tag)
PB_STATIC_ASSERT(PB_FITS(tag,30) && PB_FITS(data_offset,31) && PB_FITS(size_offset,8) && PB_FITS(data_size,31) && PB_FITS(array_size,31), FIELDINFO_DOES_NOT_FIT_width8_field ## tag)
#endif


Expand Down
12 changes: 6 additions & 6 deletions pb_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ static bool load_descriptor_values(pb_field_iter_t *iter)
{
uint32_t word0;
uint32_t data_offset;
uint8_t format;
int8_t size_offset;
uint_least8_t format;
int_least8_t size_offset;

if (iter->index >= iter->descriptor->field_count)
return false;
Expand All @@ -24,7 +24,7 @@ static bool load_descriptor_values(pb_field_iter_t *iter)
{
/* 1-word format */
iter->array_size = 1;
size_offset = (int8_t)((word0 >> 24) & 0x0F);
size_offset = (int_least8_t)((word0 >> 24) & 0x0F);
data_offset = (word0 >> 16) & 0xFF;
iter->data_size = (pb_size_t)((word0 >> 28) & 0x0F);
}
Expand All @@ -35,7 +35,7 @@ static bool load_descriptor_values(pb_field_iter_t *iter)

iter->array_size = (pb_size_t)((word0 >> 16) & 0x0FFF);
iter->tag = (pb_size_t)(iter->tag | ((word1 >> 28) << 6));
size_offset = (int8_t)((word0 >> 28) & 0x0F);
size_offset = (int_least8_t)((word0 >> 28) & 0x0F);
data_offset = word1 & 0xFFFF;
iter->data_size = (pb_size_t)((word1 >> 16) & 0x0FFF);
}
Expand All @@ -48,7 +48,7 @@ static bool load_descriptor_values(pb_field_iter_t *iter)

iter->array_size = (pb_size_t)(word0 >> 16);
iter->tag = (pb_size_t)(iter->tag | ((word1 >> 8) << 6));
size_offset = (int8_t)(word1 & 0xFF);
size_offset = (int_least8_t)(word1 & 0xFF);
data_offset = word2;
iter->data_size = (pb_size_t)word3;
}
Expand All @@ -62,7 +62,7 @@ static bool load_descriptor_values(pb_field_iter_t *iter)

iter->array_size = (pb_size_t)word4;
iter->tag = (pb_size_t)(iter->tag | ((word1 >> 8) << 6));
size_offset = (int8_t)(word1 & 0xFF);
size_offset = (int_least8_t)(word1 & 0xFF);
data_offset = word2;
iter->data_size = (pb_size_t)word3;
}
Expand Down
8 changes: 4 additions & 4 deletions pb_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ static bool checkreturn pb_decode_varint32_eof(pb_istream_t *stream, uint32_t *d
if (bitpos >= 32)
{
/* Note: The varint could have trailing 0x80 bytes, or 0xFF for negative. */
uint8_t sign_extension = (bitpos < 63) ? 0xFF : 0x01;
pb_byte_t sign_extension = (bitpos < 63) ? 0xFF : 0x01;

if ((byte & 0x7F) != 0x00 && ((result >> 31) == 0 || byte != sign_extension))
{
Expand Down Expand Up @@ -1135,7 +1135,7 @@ static bool checkreturn pb_decode_inner(pb_istream_t *stream, const pb_msgdesc_t
if ((req_field_count & 31) != 0)
{
if (fields_seen.bitfield[req_field_count >> 5] !=
(allbits >> (uint8_t)(32 - (req_field_count & 31))))
(allbits >> (uint_least8_t)(32 - (req_field_count & 31))))
{
PB_RETURN_ERROR(stream, "missing required field");
}
Expand Down Expand Up @@ -1666,7 +1666,7 @@ static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb
#ifdef PB_CONVERT_DOUBLE_FLOAT
bool pb_decode_double_as_float(pb_istream_t *stream, float *dest)
{
uint8_t sign;
uint_least8_t sign;
int exponent;
uint32_t mantissa;
uint64_t value;
Expand All @@ -1676,7 +1676,7 @@ bool pb_decode_double_as_float(pb_istream_t *stream, float *dest)
return false;

/* Decompose input value */
sign = (uint8_t)((value >> 63) & 1);
sign = (uint_least8_t)((value >> 63) & 1);
exponent = (int)((value >> 52) & 0x7FF) - 1023;
mantissa = (value >> 28) & 0xFFFFFF; /* Highest 24 bits */

Expand Down
4 changes: 2 additions & 2 deletions pb_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -912,14 +912,14 @@ static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb
bool pb_encode_float_as_double(pb_ostream_t *stream, float value)
{
union { float f; uint32_t i; } in;
uint8_t sign;
uint_least8_t sign;
int exponent;
uint64_t mantissa;

in.f = value;

/* Decompose input value */
sign = (uint8_t)((in.i >> 31) & 1);
sign = (uint_least8_t)((in.i >> 31) & 1);
exponent = (int)((in.i >> 23) & 0xFF) - 127;
mantissa = in.i & 0x7FFFFF;

Expand Down
11 changes: 11 additions & 0 deletions tests/regression/issue_485/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Check that the int8_t and uint8_t types are not used in the nanopb core.

Import("env")
env.Match('pb_decode_c.matched', ["$NANOPB/pb_decode.c", 'uint8.expected'])
env.Match('pb_decode_h.matched', ["$NANOPB/pb_decode.h", 'uint8.expected'])
env.Match('pb_encode_c.matched', ["$NANOPB/pb_encode.c", 'uint8.expected'])
env.Match('pb_encode_h.matched', ["$NANOPB/pb_encode.h", 'uint8.expected'])
env.Match('pb_common_c.matched', ["$NANOPB/pb_common.c", 'uint8.expected'])
env.Match('pb_common_h.matched', ["$NANOPB/pb_common.h", 'uint8.expected'])
env.Match('pb_h.matched', ["$NANOPB/pb.h", 'uint8.expected'])

3 changes: 3 additions & 0 deletions tests/regression/issue_485/uint8.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
! ^\s*[^/* ].*uint8_t
! ^\s*[^/* ].*int8_t

0 comments on commit f131926

Please sign in to comment.