Fix compilation error on platforms without uint8_t, add test. (#485)
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py
index 85dd36b..0b365a2 100755
--- a/generator/nanopb_generator.py
+++ b/generator/nanopb_generator.py
@@ -1092,7 +1092,7 @@
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
diff --git a/pb.h b/pb.h
index 9e74e3e..8b89dba 100644
--- a/pb.h
+++ b/pb.h
@@ -684,12 +684,12 @@
#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.
@@ -709,20 +709,20 @@
#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
diff --git a/pb_common.c b/pb_common.c
index 2bbbeb8..dfc5a05 100644
--- a/pb_common.c
+++ b/pb_common.c
@@ -9,8 +9,8 @@
{
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;
@@ -24,7 +24,7 @@
{
/* 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);
}
@@ -35,7 +35,7 @@
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);
}
@@ -48,7 +48,7 @@
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;
}
@@ -62,7 +62,7 @@
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;
}
diff --git a/pb_decode.c b/pb_decode.c
index 5085d20..e31b537 100644
--- a/pb_decode.c
+++ b/pb_decode.c
@@ -204,7 +204,7 @@
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))
{
@@ -1135,7 +1135,7 @@
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");
}
@@ -1666,7 +1666,7 @@
#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;
@@ -1676,7 +1676,7 @@
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 */
diff --git a/pb_encode.c b/pb_encode.c
index b851bfa..409fec3 100644
--- a/pb_encode.c
+++ b/pb_encode.c
@@ -912,14 +912,14 @@
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;
diff --git a/tests/regression/issue_485/SConscript b/tests/regression/issue_485/SConscript
new file mode 100644
index 0000000..f2d7692
--- /dev/null
+++ b/tests/regression/issue_485/SConscript
@@ -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'])
+
diff --git a/tests/regression/issue_485/uint8.expected b/tests/regression/issue_485/uint8.expected
new file mode 100644
index 0000000..291ab81
--- /dev/null
+++ b/tests/regression/issue_485/uint8.expected
@@ -0,0 +1,3 @@
+! ^\s*[^/* ].*uint8_t
+! ^\s*[^/* ].*int8_t
+