Compare commits

..

9 Commits

Author SHA1 Message Date
Petteri Aimonen
607cb998b5 More configuration options for dynamic alloc 2014-03-17 17:25:58 +02:00
Petteri Aimonen
ab62402059 Documentation updates 2014-03-16 15:52:19 +02:00
Petteri Aimonen
108864963f Finish the alltypes_pointer testcase, use valgrind if available. 2014-03-15 09:39:27 +02:00
Petteri Aimonen
9be2cfe968 Get rid of pb_bytes_ptr_t, just allocate pb_bytes_array_t dynamically.
This makes the internal logic much simpler, and also keeps the datatypes
more similar between STATIC/POINTER cases. It will still be a bit cumbersome
to use because of variable length array member. Macros PB_BYTES_ARRAY_T(n) and
PB_BYTES_ARRAY_T_ALLOCSIZE(n) have been added to make life a bit easier.

This has the drawback that it is no longer as easy to use externally allocated
byte array as input for bytes field in pointer mode. However, this is still
easy to do using callbacks, so it shouldn't be a large issue.
2014-03-15 08:45:58 +02:00
Petteri Aimonen
9c196b89ba Add pb_release() function 2014-03-12 21:08:35 +02:00
Petteri Aimonen
bf61d2337b More fixes for dynamic allocation 2014-03-10 18:19:38 +02:00
Petteri Aimonen
48ac461372 Bugfixes for dynamic allocation 2014-02-25 19:58:11 +02:00
Petteri Aimonen
011a30af9c Beginnings of malloc support in pb_decode 2014-02-24 21:09:25 +02:00
Petteri Aimonen
842c960d5d Setting version to 0.2.7-dev 2014-02-15 17:15:54 +02:00
11 changed files with 594 additions and 65 deletions

View File

@@ -28,6 +28,8 @@ NANOPB_INTERNALS Set this to expose the field encoder functions
that are hidden since nanopb-0.1.3. Starting that are hidden since nanopb-0.1.3. Starting
with nanopb-0.2.4, this flag does nothing. Use with nanopb-0.2.4, this flag does nothing. Use
the newer functions that have better interface. the newer functions that have better interface.
PB_ENABLE_MALLOC Set this to enable dynamic allocation support
in the decoder.
PB_MAX_REQUIRED_FIELDS Maximum number of required fields to check for PB_MAX_REQUIRED_FIELDS Maximum number of required fields to check for
presence. Default value is 64. Increases stack presence. Default value is 64. Increases stack
usage 1 byte per every 8 fields. Compiler usage 1 byte per every 8 fields. Compiler
@@ -77,8 +79,9 @@ max_count Allocated number of entries in arrays
(*repeated* fields). (*repeated* fields).
type Type of the generated field. Default value type Type of the generated field. Default value
is *FT_DEFAULT*, which selects automatically. is *FT_DEFAULT*, which selects automatically.
You can use *FT_CALLBACK*, *FT_STATIC* or You can use *FT_CALLBACK*, *FT_POINTER*,
*FT_IGNORE* to force a callback field, a static *FT_STATIC* or *FT_IGNORE* to force a callback
field, a dynamically allocated field, a static
field or to completely ignore the field. field or to completely ignore the field.
long_names Prefix the enum name to the enum value in long_names Prefix the enum name to the enum value in
definitions, i.e. *EnumName_EnumValue*. Enabled definitions, i.e. *EnumName_EnumValue*. Enabled
@@ -417,6 +420,17 @@ Encodes the contents of a structure as a protocol buffers message and writes it
Normally pb_encode simply walks through the fields description array and serializes each field in turn. However, submessages must be serialized twice: first to calculate their size and then to actually write them to output. This causes some constraints for callback fields, which must return the same data on every call. Normally pb_encode simply walks through the fields description array and serializes each field in turn. However, submessages must be serialized twice: first to calculate their size and then to actually write them to output. This causes some constraints for callback fields, which must return the same data on every call.
pb_encode_delimited
-------------------
Calculates the length of the message, encodes it as varint and then encodes the message. ::
bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct);
(parameters are the same as for `pb_encode`_.)
A common way to indicate the message length in Protocol Buffers is to prefix it with a varint.
This function does this, and it is compatible with *parseDelimitedFrom* in Google's protobuf library.
.. sidebar:: Encoding fields manually .. sidebar:: Encoding fields manually
The functions with names *pb_encode_\** are used when dealing with callback fields. The typical reason for using callbacks is to have an array of unlimited size. In that case, `pb_encode`_ will call your callback function, which in turn will call *pb_encode_\** functions repeatedly to write out values. The functions with names *pb_encode_\** are used when dealing with callback fields. The typical reason for using callbacks is to have an array of unlimited size. In that case, `pb_encode`_ will call your callback function, which in turn will call *pb_encode_\** functions repeatedly to write out values.
@@ -579,6 +593,10 @@ In addition to EOF, the pb_decode implementation supports terminating a message
For optional fields, this function applies the default value and sets *has_<field>* to false if the field is not present. For optional fields, this function applies the default value and sets *has_<field>* to false if the field is not present.
If *PB_ENABLE_MALLOC* is defined, this function may allocate storage for any pointer type fields.
In this case, you have to call `pb_release`_ to release the memory after you are done with the message.
On error return `pb_decode` will release the memory itself.
pb_decode_noinit pb_decode_noinit
---------------- ----------------
Same as `pb_decode`_, except does not apply the default values to fields. :: Same as `pb_decode`_, except does not apply the default values to fields. ::
@@ -589,6 +607,35 @@ Same as `pb_decode`_, except does not apply the default values to fields. ::
The destination structure should be filled with zeros before calling this function. Doing a *memset* manually can be slightly faster than using `pb_decode`_ if you don't need any default values. The destination structure should be filled with zeros before calling this function. Doing a *memset* manually can be slightly faster than using `pb_decode`_ if you don't need any default values.
In addition to decoding a single message, this function can be used to merge two messages, so that
values from previous message will remain if the new message does not contain a field.
This function *will not* release the message even on error return. If you use *PB_ENABLE_MALLOC*,
you will need to call `pb_release`_ yourself.
pb_decode_delimited
-------------------
Same as `pb_decode`_, except that it first reads a varint with the length of the message. ::
bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
(parameters are the same as for `pb_decode`_.)
A common method to indicate message size in Protocol Buffers is to prefix it with a varint.
This function is compatible with *writeDelimitedTo* in the Google's Protocol Buffers library.
pb_release
----------
Releases any dynamically allocated fields.
void pb_release(const pb_field_t fields[], void *dest_struct);
:fields: A field description array. Usually autogenerated.
:dest_struct: Pointer to structure where data will be stored.
This function is only available if *PB_ENABLE_MALLOC* is defined. It will release any
pointer type fields in the structure and set the pointers to NULL.
pb_skip_varint pb_skip_varint
-------------- --------------
Skip a varint_ encoded integer without decoding it. :: Skip a varint_ encoded integer without decoding it. ::

View File

@@ -1,7 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
'''Generate header file for nanopb from a ProtoBuf FileDescriptorSet.''' '''Generate header file for nanopb from a ProtoBuf FileDescriptorSet.'''
nanopb_version = "nanopb-0.2.6" nanopb_version = "nanopb-0.2.7-dev"
import sys import sys
@@ -246,7 +246,7 @@ class Field:
self.ctype = self.struct_name + self.name + 't' self.ctype = self.struct_name + self.name + 't'
self.enc_size = varint_max_size(self.max_size) + self.max_size self.enc_size = varint_max_size(self.max_size) + self.max_size
elif self.allocation == 'POINTER': elif self.allocation == 'POINTER':
self.ctype = 'pb_bytes_ptr_t' self.ctype = 'pb_bytes_array_t'
elif desc.type == FieldD.TYPE_MESSAGE: elif desc.type == FieldD.TYPE_MESSAGE:
self.pbtype = 'MESSAGE' self.pbtype = 'MESSAGE'
self.ctype = self.submsgname = names_from_type_name(desc.type_name) self.ctype = self.submsgname = names_from_type_name(desc.type_name)
@@ -266,8 +266,8 @@ class Field:
if self.pbtype == 'MESSAGE': if self.pbtype == 'MESSAGE':
# Use struct definition, so recursive submessages are possible # Use struct definition, so recursive submessages are possible
result += ' struct _%s *%s;' % (self.ctype, self.name) result += ' struct _%s *%s;' % (self.ctype, self.name)
elif self.rules == 'REPEATED' and self.pbtype == 'STRING': elif self.rules == 'REPEATED' and self.pbtype in ['STRING', 'BYTES']:
# String arrays need to be defined as pointers to pointers # String/bytes arrays need to be defined as pointers to pointers
result += ' %s **%s;' % (self.ctype, self.name) result += ' %s **%s;' % (self.ctype, self.name)
else: else:
result += ' %s *%s;' % (self.ctype, self.name) result += ' %s *%s;' % (self.ctype, self.name)

32
pb.h
View File

@@ -10,6 +10,9 @@
* uncommenting the lines, or on the compiler command line. * * uncommenting the lines, or on the compiler command line. *
*****************************************************************/ *****************************************************************/
/* Enable support for dynamically allocated fields */
/* #define PB_ENABLE_MALLOC 1 */
/* Define this if your CPU architecture is big endian, i.e. it /* Define this if your CPU architecture is big endian, i.e. it
* stores the most-significant byte first. */ * stores the most-significant byte first. */
/* #define __BIG_ENDIAN__ 1 */ /* #define __BIG_ENDIAN__ 1 */
@@ -43,7 +46,7 @@
/* Version of the nanopb library. Just in case you want to check it in /* Version of the nanopb library. Just in case you want to check it in
* your own program. */ * your own program. */
#define NANOPB_VERSION nanopb-0.2.6 #define NANOPB_VERSION nanopb-0.2.7-dev
/* Include all the system headers needed by nanopb. You will need the /* Include all the system headers needed by nanopb. You will need the
* definitions of the following: * definitions of the following:
@@ -63,6 +66,10 @@
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#ifdef PB_ENABLE_MALLOC
#include <stdlib.h>
#endif
#endif #endif
/* Macro for defining packed structures (compiler dependent). /* Macro for defining packed structures (compiler dependent).
@@ -234,21 +241,15 @@ STATIC_ASSERT(sizeof(uint64_t) == 8, UINT64_T_WRONG_SIZE)
* It has the number of bytes in the beginning, and after that an array. * It has the number of bytes in the beginning, and after that an array.
* Note that actual structs used will have a different length of bytes array. * Note that actual structs used will have a different length of bytes array.
*/ */
#define PB_BYTES_ARRAY_T(n) struct { size_t size; uint8_t bytes[n]; }
#define PB_BYTES_ARRAY_T_ALLOCSIZE(n) ((size_t)n + offsetof(pb_bytes_array_t, bytes))
struct _pb_bytes_array_t { struct _pb_bytes_array_t {
size_t size; size_t size;
uint8_t bytes[1]; uint8_t bytes[1];
}; };
typedef struct _pb_bytes_array_t pb_bytes_array_t; typedef struct _pb_bytes_array_t pb_bytes_array_t;
/* Same, except for pointer-type fields. There is no need to variable struct
* length in this case.
*/
struct _pb_bytes_ptr_t {
size_t size;
uint8_t *bytes;
};
typedef struct _pb_bytes_ptr_t pb_bytes_ptr_t;
/* This structure is used for giving the callback function. /* This structure is used for giving the callback function.
* It is stored in the message structure and filled in by the method that * It is stored in the message structure and filled in by the method that
* calls pb_decode. * calls pb_decode.
@@ -342,6 +343,17 @@ struct _pb_extension_t {
pb_extension_t *next; pb_extension_t *next;
}; };
/* Memory allocation functions to use. You can define pb_realloc and
* pb_free to custom functions if you want. */
#ifdef PB_ENABLE_MALLOC
# ifndef pb_realloc
# define pb_realloc(ptr, size) realloc(ptr, size)
# endif
# ifndef pb_free
# define pb_free(ptr) free(ptr)
# endif
#endif
/* These macros are used to declare pb_field_t's in the constant array. */ /* These macros are used to declare pb_field_t's in the constant array. */
/* Size of a structure member, in bytes. */ /* Size of a structure member, in bytes. */
#define pb_membersize(st, m) (sizeof ((st*)0)->m) #define pb_membersize(st, m) (sizeof ((st*)0)->m)

View File

@@ -359,6 +359,10 @@ static bool pb_field_next(pb_field_iterator_t *iter)
{ {
prev_size *= iter->pos->array_size; prev_size *= iter->pos->array_size;
} }
else if (PB_ATYPE(iter->pos->type) == PB_ATYPE_POINTER)
{
prev_size = sizeof(void*);
}
if (iter->pos->tag == 0) if (iter->pos->tag == 0)
return false; /* Only happens with empty message types */ return false; /* Only happens with empty message types */
@@ -465,6 +469,136 @@ static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t
} }
} }
#ifdef PB_ENABLE_MALLOC
/* Allocate storage for the field and store the pointer at iter->pData.
* array_size is the number of entries to reserve in an array. */
static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
{
void *ptr = *(void**)pData;
size_t size = array_size * data_size;
/* Allocate new or expand previous allocation */
/* Note: on failure the old pointer will remain in the structure,
* the message must be freed by caller also on error return. */
ptr = pb_realloc(ptr, size);
if (ptr == NULL)
PB_RETURN_ERROR(stream, "realloc failed");
*(void**)pData = ptr;
return true;
}
/* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
static void initialize_pointer_field(void *pItem, pb_field_iterator_t *iter)
{
if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
{
*(void**)pItem = NULL;
}
else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
{
pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
}
}
#endif
static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
{
#ifndef PB_ENABLE_MALLOC
UNUSED(wire_type);
UNUSED(iter);
PB_RETURN_ERROR(stream, "no malloc support");
#else
pb_type_t type;
pb_decoder_t func;
type = iter->pos->type;
func = PB_DECODERS[PB_LTYPE(type)];
switch (PB_HTYPE(type))
{
case PB_HTYPE_REQUIRED:
case PB_HTYPE_OPTIONAL:
if (PB_LTYPE(type) == PB_LTYPE_STRING ||
PB_LTYPE(type) == PB_LTYPE_BYTES)
{
return func(stream, iter->pos, iter->pData);
}
else
{
if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
return false;
initialize_pointer_field(*(void**)iter->pData, iter);
return func(stream, iter->pos, *(void**)iter->pData);
}
case PB_HTYPE_REPEATED:
if (wire_type == PB_WT_STRING
&& PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
{
/* Packed array, multiple items come in at once. */
bool status = true;
size_t *size = (size_t*)iter->pSize;
size_t allocated_size = *size;
void *pItem;
pb_istream_t substream;
if (!pb_make_string_substream(stream, &substream))
return false;
while (substream.bytes_left)
{
if (*size + 1 > allocated_size)
{
/* Allocate more storage. This tries to guess the
* number of remaining entries. Round the division
* upwards. */
allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
{
status = false;
break;
}
}
/* Decode the array entry */
pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
initialize_pointer_field(pItem, iter);
if (!func(&substream, iter->pos, pItem))
{
status = false;
break;
}
(*size)++;
}
pb_close_string_substream(stream, &substream);
return status;
}
else
{
/* Normal repeated field, i.e. only one item at a time. */
size_t *size = (size_t*)iter->pSize;
void *pItem;
(*size)++;
if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
return false;
pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
initialize_pointer_field(pItem, iter);
return func(stream, iter->pos, pItem);
}
default:
PB_RETURN_ERROR(stream, "invalid field type");
}
#endif
}
static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter) static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
{ {
pb_callback_t *pCallback = (pb_callback_t*)iter->pData; pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
@@ -519,6 +653,9 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
case PB_ATYPE_STATIC: case PB_ATYPE_STATIC:
return decode_static_field(stream, wire_type, iter); return decode_static_field(stream, wire_type, iter);
case PB_ATYPE_POINTER:
return decode_pointer_field(stream, wire_type, iter);
case PB_ATYPE_CALLBACK: case PB_ATYPE_CALLBACK:
return decode_callback_field(stream, wire_type, iter); return decode_callback_field(stream, wire_type, iter);
@@ -597,45 +734,60 @@ static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_str
pb_field_iterator_t iter; pb_field_iterator_t iter;
pb_field_init(&iter, fields, dest_struct); pb_field_init(&iter, fields, dest_struct);
/* Initialize size/has fields and apply default values */
do do
{ {
pb_type_t type; pb_type_t type;
type = iter.pos->type; type = iter.pos->type;
/* Avoid crash on empty message types (zero fields) */
if (iter.pos->tag == 0) if (iter.pos->tag == 0)
continue; continue;
if (PB_ATYPE(type) == PB_ATYPE_STATIC) if (PB_ATYPE(type) == PB_ATYPE_STATIC)
{ {
/* Initialize the size field for optional/repeated fields to 0. */
if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL) if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
{ {
/* Set has_field to false. Still initialize the optional field
* itself also. */
*(bool*)iter.pSize = false; *(bool*)iter.pSize = false;
} }
else if (PB_HTYPE(type) == PB_HTYPE_REPEATED) else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
{ {
/* Set array count to 0, no need to initialize contents. */
*(size_t*)iter.pSize = 0; *(size_t*)iter.pSize = 0;
continue; /* Array is empty, no need to initialize contents */ continue;
} }
/* Initialize field contents to default value */
if (PB_LTYPE(iter.pos->type) == PB_LTYPE_SUBMESSAGE) if (PB_LTYPE(iter.pos->type) == PB_LTYPE_SUBMESSAGE)
{ {
/* Initialize submessage to defaults */
pb_message_set_to_defaults((const pb_field_t *) iter.pos->ptr, iter.pData); pb_message_set_to_defaults((const pb_field_t *) iter.pos->ptr, iter.pData);
} }
else if (iter.pos->ptr != NULL) else if (iter.pos->ptr != NULL)
{ {
/* Initialize to default value */
memcpy(iter.pData, iter.pos->ptr, iter.pos->data_size); memcpy(iter.pData, iter.pos->ptr, iter.pos->data_size);
} }
else else
{ {
/* Initialize to zeros */
memset(iter.pData, 0, iter.pos->data_size); memset(iter.pData, 0, iter.pos->data_size);
} }
} }
else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
{
/* Initialize the pointer to NULL. */
*(void**)iter.pData = NULL;
/* Initialize array count to 0. */
if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
{
*(size_t*)iter.pSize = 0;
}
}
else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK) else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
{ {
continue; /* Don't overwrite callback */ /* Don't overwrite callback */
} }
} while (pb_field_next(&iter)); } while (pb_field_next(&iter));
} }
@@ -742,8 +894,16 @@ bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[
bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct) bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
{ {
bool status;
pb_message_set_to_defaults(fields, dest_struct); pb_message_set_to_defaults(fields, dest_struct);
return pb_decode_noinit(stream, fields, dest_struct); status = pb_decode_noinit(stream, fields, dest_struct);
#ifdef PB_ENABLE_MALLOC
if (!status)
pb_release(fields, dest_struct);
#endif
return status;
} }
bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct) bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
@@ -759,6 +919,62 @@ bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *
return status; return status;
} }
#ifdef PB_ENABLE_MALLOC
void pb_release(const pb_field_t fields[], void *dest_struct)
{
pb_field_iterator_t iter;
pb_field_init(&iter, fields, dest_struct);
do
{
pb_type_t type;
type = iter.pos->type;
/* Avoid crash on empty message types (zero fields) */
if (iter.pos->tag == 0)
continue;
if (PB_ATYPE(type) == PB_ATYPE_POINTER)
{
if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
(PB_LTYPE(type) == PB_LTYPE_STRING ||
PB_LTYPE(type) == PB_LTYPE_BYTES))
{
/* Release entries in repeated string or bytes array */
void **pItem = *(void***)iter.pData;
size_t count = *(size_t*)iter.pSize;
while (count--)
{
pb_free(*pItem);
*pItem++ = NULL;
}
}
else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
{
/* Release fields in submessages */
void *pItem = *(void**)iter.pData;
size_t count = (pItem ? 1 : 0);
if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
{
count = *(size_t*)iter.pSize;
}
while (count--)
{
pb_release((const pb_field_t*)iter.pos->ptr, pItem);
pItem = (uint8_t*)pItem + iter.pos->data_size;
}
}
/* Release main item */
pb_free(*(void**)iter.pData);
*(void**)iter.pData = NULL;
}
} while (pb_field_next(&iter));
}
#endif
/* Field decoders */ /* Field decoders */
bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest) bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
@@ -881,30 +1097,59 @@ bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, v
bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest) bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
{ {
pb_bytes_array_t *x = (pb_bytes_array_t*)dest; uint32_t size;
pb_bytes_array_t *bdest;
uint32_t temp; if (!pb_decode_varint32(stream, &size))
if (!pb_decode_varint32(stream, &temp))
return false; return false;
x->size = temp;
/* Check length, noting the space taken by the size_t header. */ if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
if (x->size > field->data_size - offsetof(pb_bytes_array_t, bytes)) {
#ifndef PB_ENABLE_MALLOC
PB_RETURN_ERROR(stream, "no malloc support");
#else
if (!allocate_field(stream, dest, PB_BYTES_ARRAY_T_ALLOCSIZE(size), 1))
return false;
bdest = *(pb_bytes_array_t**)dest;
#endif
}
else
{
if (PB_BYTES_ARRAY_T_ALLOCSIZE(size) > field->data_size)
PB_RETURN_ERROR(stream, "bytes overflow"); PB_RETURN_ERROR(stream, "bytes overflow");
bdest = (pb_bytes_array_t*)dest;
}
return pb_read(stream, x->bytes, x->size); bdest->size = size;
return pb_read(stream, bdest->bytes, size);
} }
bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest) bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
{ {
uint32_t size; uint32_t size;
size_t alloc_size;
bool status; bool status;
if (!pb_decode_varint32(stream, &size)) if (!pb_decode_varint32(stream, &size))
return false; return false;
/* Check length, noting the null terminator */ /* Space for null terminator */
if (size + 1 > field->data_size) alloc_size = size + 1;
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
{
#ifndef PB_ENABLE_MALLOC
PB_RETURN_ERROR(stream, "no malloc support");
#else
if (!allocate_field(stream, dest, alloc_size, 1))
return false;
dest = *(void**)dest;
#endif
}
else
{
if (alloc_size > field->data_size)
PB_RETURN_ERROR(stream, "string overflow"); PB_RETURN_ERROR(stream, "string overflow");
}
status = pb_read(stream, (uint8_t*)dest, size); status = pb_read(stream, (uint8_t*)dest, size);
*((uint8_t*)dest + size) = 0; *((uint8_t*)dest + size) = 0;

View File

@@ -73,6 +73,9 @@ bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struc
* *
* This can also be used for 'merging' two messages, i.e. update only the * This can also be used for 'merging' two messages, i.e. update only the
* fields that exist in the new message. * fields that exist in the new message.
*
* Note: If this function returns with an error, it will not release any
* dynamically allocated fields. You will need to call pb_release() yourself.
*/ */
bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
@@ -82,6 +85,14 @@ bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *des
*/ */
bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
#ifdef PB_ENABLE_MALLOC
/* Release any allocated pointer fields. If you use dynamic allocation, you should
* call this for any successfully decoded message when you are done with it. If
* pb_decode() returns with an error, the message is already released.
*/
void pb_release(const pb_field_t fields[], void *dest_struct);
#endif
/************************************** /**************************************
* Functions for manipulating streams * * Functions for manipulating streams *

View File

@@ -174,11 +174,12 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie
return false; return false;
/* Normally the data is stored directly in the array entries, but /* Normally the data is stored directly in the array entries, but
* for pointer-type string fields, the array entries are actually * for pointer-type string and bytes fields, the array entries are
* string pointers. So we have to dereference once more to get to * actually pointers themselves also. So we have to dereference once
* the character data. */ * more to get to the actual data. */
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER && if (PB_ATYPE(field->type) == PB_ATYPE_POINTER &&
PB_LTYPE(field->type) == PB_LTYPE_STRING) (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
PB_LTYPE(field->type) == PB_LTYPE_BYTES))
{ {
if (!func(stream, field, *(const void* const*)p)) if (!func(stream, field, *(const void* const*)p))
return false; return false;
@@ -602,21 +603,23 @@ bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, c
} }
bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src) bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
{
const pb_bytes_ptr_t *bytes = (const pb_bytes_ptr_t*)src;
return pb_encode_string(stream, bytes->bytes, bytes->size);
}
else
{ {
const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src; const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src;
if (bytes->size + offsetof(pb_bytes_array_t, bytes) > field->data_size)
if (src == NULL)
{
/* Threat null pointer as an empty bytes field */
return pb_encode_string(stream, NULL, 0);
}
if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
PB_BYTES_ARRAY_T_ALLOCSIZE(bytes->size) > field->data_size)
{
PB_RETURN_ERROR(stream, "bytes size exceeded"); PB_RETURN_ERROR(stream, "bytes size exceeded");
}
return pb_encode_string(stream, bytes->bytes, bytes->size); return pb_encode_string(stream, bytes->bytes, bytes->size);
} }
}
bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src) bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{ {
@@ -628,11 +631,18 @@ bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, co
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER) if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
max_size = (size_t)-1; max_size = (size_t)-1;
if (src == NULL)
{
size = 0; /* Threat null pointer as an empty string */
}
else
{
while (size < max_size && *p != '\0') while (size < max_size && *p != '\0')
{ {
size++; size++;
p++; p++;
} }
}
return pb_encode_string(stream, (const uint8_t*)src, size); return pb_encode_string(stream, (const uint8_t*)src, size);
} }

View File

@@ -70,7 +70,7 @@ if not env.GetOption('clean'):
if 'gcc' in env['CC']: if 'gcc' in env['CC']:
if conf.CheckLib('mudflap'): if conf.CheckLib('mudflap'):
conf.env.Append(CCFLAGS = '-fmudflap') conf.env.Append(CCFLAGS = '-fmudflap')
conf.env.Append(LINKFLAGS = '-lmudflap -fmudflap') conf.env.Append(LINKFLAGS = '-fmudflap')
# Check if we can use extra strict warning flags (only with GCC) # Check if we can use extra strict warning flags (only with GCC)
extra = '-Wcast-qual -Wlogical-op -Wconversion' extra = '-Wcast-qual -Wlogical-op -Wconversion'

View File

@@ -3,21 +3,46 @@
Import("env") Import("env")
# We need our own pb_decode.o for the malloc support
env = env.Clone()
env.Append(CPPDEFINES = {'PB_ENABLE_MALLOC': 1});
# Disable libmudflap, because it will confuse valgrind
# and other memory leak detection tools.
if '-fmudflap' in env["CCFLAGS"]:
env["CCFLAGS"].remove("-fmudflap")
env["LINKFLAGS"].remove("-fmudflap")
env["LIBS"].remove("mudflap")
strict = env.Clone()
strict.Append(CFLAGS = strict['CORECFLAGS'])
strict.Object("pb_decode_with_malloc.o", "$NANOPB/pb_decode.c")
strict.Object("pb_encode_with_malloc.o", "$NANOPB/pb_encode.c")
c = Copy("$TARGET", "$SOURCE") c = Copy("$TARGET", "$SOURCE")
env.Command("alltypes.proto", "#alltypes/alltypes.proto", c) env.Command("alltypes.proto", "#alltypes/alltypes.proto", c)
env.NanopbProto(["alltypes", "alltypes.options"]) env.NanopbProto(["alltypes", "alltypes.options"])
enc = env.Program(["encode_alltypes_pointer.c", "alltypes.pb.c", "$COMMON/pb_encode.o"]) enc = env.Program(["encode_alltypes_pointer.c", "alltypes.pb.c", "pb_encode_with_malloc.o"])
dec = env.Program(["decode_alltypes_pointer.c", "alltypes.pb.c", "pb_decode_with_malloc.o"])
refdec = "$BUILD/alltypes/decode_alltypes$PROGSUFFIX" # Encode and compare results to non-pointer alltypes test case
# Encode and compare results
env.RunTest(enc) env.RunTest(enc)
env.RunTest("decode_alltypes.output", [refdec, "encode_alltypes_pointer.output"])
env.Compare(["encode_alltypes_pointer.output", "$BUILD/alltypes/encode_alltypes.output"]) env.Compare(["encode_alltypes_pointer.output", "$BUILD/alltypes/encode_alltypes.output"])
# Decode (under valgrind if available)
valgrind = env.WhereIs('valgrind')
kwargs = {}
if valgrind:
kwargs['COMMAND'] = valgrind
kwargs['ARGS'] = ["-q", dec[0].abspath]
env.RunTest("decode_alltypes.output", [dec, "encode_alltypes_pointer.output"], **kwargs)
# Do the same thing with the optional fields present # Do the same thing with the optional fields present
env.RunTest("optionals.output", enc, ARGS = ['1']) env.RunTest("optionals.output", enc, ARGS = ['1'])
env.RunTest("optionals.decout", [refdec, "optionals.output"], ARGS = ['1'])
env.Compare(["optionals.output", "$BUILD/alltypes/optionals.output"]) env.Compare(["optionals.output", "$BUILD/alltypes/optionals.output"])
kwargs['ARGS'] = kwargs.get('ARGS', []) + ['1']
env.RunTest("optionals.decout", [dec, "optionals.output"], **kwargs)

View File

@@ -0,0 +1,173 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pb_decode.h>
#include "alltypes.pb.h"
#include "test_helpers.h"
#define TEST(x) if (!(x)) { \
fprintf(stderr, "Test " #x " failed.\n"); \
status = false; \
}
/* This function is called once from main(), it handles
the decoding and checks the fields. */
bool check_alltypes(pb_istream_t *stream, int mode)
{
bool status = true;
AllTypes alltypes;
/* Fill with garbage to better detect initialization errors */
memset(&alltypes, 0xAA, sizeof(alltypes));
if (!pb_decode(stream, AllTypes_fields, &alltypes))
return false;
TEST(alltypes.req_int32 && *alltypes.req_int32 == -1001);
TEST(alltypes.req_int64 && *alltypes.req_int64 == -1002);
TEST(alltypes.req_uint32 && *alltypes.req_uint32 == 1003);
TEST(alltypes.req_uint64 && *alltypes.req_uint64 == 1004);
TEST(alltypes.req_sint32 && *alltypes.req_sint32 == -1005);
TEST(alltypes.req_sint64 && *alltypes.req_sint64 == -1006);
TEST(alltypes.req_bool && *alltypes.req_bool == true);
TEST(alltypes.req_fixed32 && *alltypes.req_fixed32 == 1008);
TEST(alltypes.req_sfixed32 && *alltypes.req_sfixed32 == -1009);
TEST(alltypes.req_float && *alltypes.req_float == 1010.0f);
TEST(alltypes.req_fixed64 && *alltypes.req_fixed64 == 1011);
TEST(alltypes.req_sfixed64 && *alltypes.req_sfixed64 == -1012);
TEST(alltypes.req_double && *alltypes.req_double == 1013.0f);
TEST(alltypes.req_string && strcmp(alltypes.req_string, "1014") == 0);
TEST(alltypes.req_bytes && alltypes.req_bytes->size == 4);
TEST(alltypes.req_bytes && memcmp(&alltypes.req_bytes->bytes, "1015", 4) == 0);
TEST(alltypes.req_submsg && alltypes.req_submsg->substuff1
&& strcmp(alltypes.req_submsg->substuff1, "1016") == 0);
TEST(alltypes.req_submsg && alltypes.req_submsg->substuff2
&& *alltypes.req_submsg->substuff2 == 1016);
TEST(*alltypes.req_enum == MyEnum_Truth);
TEST(alltypes.rep_int32_count == 5 && alltypes.rep_int32[4] == -2001 && alltypes.rep_int32[0] == 0);
TEST(alltypes.rep_int64_count == 5 && alltypes.rep_int64[4] == -2002 && alltypes.rep_int64[0] == 0);
TEST(alltypes.rep_uint32_count == 5 && alltypes.rep_uint32[4] == 2003 && alltypes.rep_uint32[0] == 0);
TEST(alltypes.rep_uint64_count == 5 && alltypes.rep_uint64[4] == 2004 && alltypes.rep_uint64[0] == 0);
TEST(alltypes.rep_sint32_count == 5 && alltypes.rep_sint32[4] == -2005 && alltypes.rep_sint32[0] == 0);
TEST(alltypes.rep_sint64_count == 5 && alltypes.rep_sint64[4] == -2006 && alltypes.rep_sint64[0] == 0);
TEST(alltypes.rep_bool_count == 5 && alltypes.rep_bool[4] == true && alltypes.rep_bool[0] == false);
TEST(alltypes.rep_fixed32_count == 5 && alltypes.rep_fixed32[4] == 2008 && alltypes.rep_fixed32[0] == 0);
TEST(alltypes.rep_sfixed32_count == 5 && alltypes.rep_sfixed32[4] == -2009 && alltypes.rep_sfixed32[0] == 0);
TEST(alltypes.rep_float_count == 5 && alltypes.rep_float[4] == 2010.0f && alltypes.rep_float[0] == 0.0f);
TEST(alltypes.rep_fixed64_count == 5 && alltypes.rep_fixed64[4] == 2011 && alltypes.rep_fixed64[0] == 0);
TEST(alltypes.rep_sfixed64_count == 5 && alltypes.rep_sfixed64[4] == -2012 && alltypes.rep_sfixed64[0] == 0);
TEST(alltypes.rep_double_count == 5 && alltypes.rep_double[4] == 2013.0 && alltypes.rep_double[0] == 0.0);
TEST(alltypes.rep_string_count == 5 && strcmp(alltypes.rep_string[4], "2014") == 0 && alltypes.rep_string[0][0] == '\0');
TEST(alltypes.rep_bytes_count == 5 && alltypes.rep_bytes[4]->size == 4 && alltypes.rep_bytes[0]->size == 0);
TEST(memcmp(&alltypes.rep_bytes[4]->bytes, "2015", 4) == 0);
TEST(alltypes.rep_submsg_count == 5);
TEST(strcmp(alltypes.rep_submsg[4].substuff1, "2016") == 0 && alltypes.rep_submsg[0].substuff1[0] == '\0');
TEST(*alltypes.rep_submsg[4].substuff2 == 2016 && *alltypes.rep_submsg[0].substuff2 == 0);
TEST(*alltypes.rep_submsg[4].substuff3 == 2016 && alltypes.rep_submsg[0].substuff3 == NULL);
TEST(alltypes.rep_enum_count == 5 && alltypes.rep_enum[4] == MyEnum_Truth && alltypes.rep_enum[0] == MyEnum_Zero);
TEST(alltypes.rep_emptymsg_count == 5);
if (mode == 0)
{
/* Expect that optional values are not present */
TEST(alltypes.opt_int32 == NULL);
TEST(alltypes.opt_int64 == NULL);
TEST(alltypes.opt_uint32 == NULL);
TEST(alltypes.opt_uint64 == NULL);
TEST(alltypes.opt_sint32 == NULL);
TEST(alltypes.opt_sint64 == NULL);
TEST(alltypes.opt_bool == NULL);
TEST(alltypes.opt_fixed32 == NULL);
TEST(alltypes.opt_sfixed32 == NULL);
TEST(alltypes.opt_float == NULL);
TEST(alltypes.opt_fixed64 == NULL);
TEST(alltypes.opt_sfixed64 == NULL);
TEST(alltypes.opt_double == NULL);
TEST(alltypes.opt_string == NULL);
TEST(alltypes.opt_bytes == NULL);
TEST(alltypes.opt_submsg == NULL);
TEST(alltypes.opt_enum == NULL);
}
else
{
/* Expect filled-in values */
TEST(alltypes.opt_int32 && *alltypes.opt_int32 == 3041);
TEST(alltypes.opt_int64 && *alltypes.opt_int64 == 3042);
TEST(alltypes.opt_uint32 && *alltypes.opt_uint32 == 3043);
TEST(alltypes.opt_uint64 && *alltypes.opt_uint64 == 3044);
TEST(alltypes.opt_sint32 && *alltypes.opt_sint32 == 3045);
TEST(alltypes.opt_sint64 && *alltypes.opt_sint64 == 3046);
TEST(alltypes.opt_bool && *alltypes.opt_bool == true);
TEST(alltypes.opt_fixed32 && *alltypes.opt_fixed32 == 3048);
TEST(alltypes.opt_sfixed32 && *alltypes.opt_sfixed32== 3049);
TEST(alltypes.opt_float && *alltypes.opt_float == 3050.0f);
TEST(alltypes.opt_fixed64 && *alltypes.opt_fixed64 == 3051);
TEST(alltypes.opt_sfixed64 && *alltypes.opt_sfixed64== 3052);
TEST(alltypes.opt_double && *alltypes.opt_double == 3053.0);
TEST(alltypes.opt_string && strcmp(alltypes.opt_string, "3054") == 0);
TEST(alltypes.opt_bytes && alltypes.opt_bytes->size == 4);
TEST(alltypes.opt_bytes && memcmp(&alltypes.opt_bytes->bytes, "3055", 4) == 0);
TEST(alltypes.opt_submsg && strcmp(alltypes.opt_submsg->substuff1, "3056") == 0);
TEST(alltypes.opt_submsg && *alltypes.opt_submsg->substuff2 == 3056);
TEST(alltypes.opt_enum && *alltypes.opt_enum == MyEnum_Truth);
TEST(alltypes.opt_emptymsg);
}
TEST(alltypes.req_limits->int32_min && *alltypes.req_limits->int32_min == INT32_MIN);
TEST(alltypes.req_limits->int32_max && *alltypes.req_limits->int32_max == INT32_MAX);
TEST(alltypes.req_limits->uint32_min && *alltypes.req_limits->uint32_min == 0);
TEST(alltypes.req_limits->uint32_max && *alltypes.req_limits->uint32_max == UINT32_MAX);
TEST(alltypes.req_limits->int64_min && *alltypes.req_limits->int64_min == INT64_MIN);
TEST(alltypes.req_limits->int64_max && *alltypes.req_limits->int64_max == INT64_MAX);
TEST(alltypes.req_limits->uint64_min && *alltypes.req_limits->uint64_min == 0);
TEST(alltypes.req_limits->uint64_max && *alltypes.req_limits->uint64_max == UINT64_MAX);
TEST(alltypes.req_limits->enum_min && *alltypes.req_limits->enum_min == HugeEnum_Negative);
TEST(alltypes.req_limits->enum_max && *alltypes.req_limits->enum_max == HugeEnum_Positive);
TEST(alltypes.end && *alltypes.end == 1099);
pb_release(AllTypes_fields, &alltypes);
return status;
}
int main(int argc, char **argv)
{
uint8_t buffer[1024];
size_t count;
pb_istream_t stream;
/* Whether to expect the optional values or the default values. */
int mode = (argc > 1) ? atoi(argv[1]) : 0;
/* Read the data into buffer */
SET_BINARY_MODE(stdin);
count = fread(buffer, 1, sizeof(buffer), stdin);
/* Construct a pb_istream_t for reading from the buffer */
stream = pb_istream_from_buffer(buffer, count);
/* Decode and verify the message */
if (!check_alltypes(&stream, mode))
{
fprintf(stderr, "Test failed: %s\n", PB_GET_ERROR(&stream));
return 1;
}
else
{
return 0;
}
}

View File

@@ -27,7 +27,7 @@ int main(int argc, char **argv)
int64_t req_sfixed64 = -1012; int64_t req_sfixed64 = -1012;
double req_double = 1013.0; double req_double = 1013.0;
char* req_string = "1014"; char* req_string = "1014";
pb_bytes_ptr_t req_bytes = {4, (uint8_t*)"1015"}; PB_BYTES_ARRAY_T(4) req_bytes = {4, {'1', '0', '1', '5'}};
static int32_t req_substuff = 1016; static int32_t req_substuff = 1016;
SubMessage req_submsg = {"1016", &req_substuff}; SubMessage req_submsg = {"1016", &req_substuff};
MyEnum req_enum = MyEnum_Truth; MyEnum req_enum = MyEnum_Truth;
@@ -50,7 +50,8 @@ int main(int argc, char **argv)
int64_t rep_sfixed64[5] = {0, 0, 0, 0, -2012}; int64_t rep_sfixed64[5] = {0, 0, 0, 0, -2012};
double rep_double[5] = {0, 0, 0, 0, 2013.0f}; double rep_double[5] = {0, 0, 0, 0, 2013.0f};
char* rep_string[5] = {"", "", "", "", "2014"}; char* rep_string[5] = {"", "", "", "", "2014"};
pb_bytes_ptr_t rep_bytes[5] = {{0,0}, {0,0}, {0,0}, {0,0}, {4, (uint8_t*)"2015"}}; static PB_BYTES_ARRAY_T(4) rep_bytes_4 = {4, {'2', '0', '1', '5'}};
pb_bytes_array_t *rep_bytes[5]= {NULL, NULL, NULL, NULL, (pb_bytes_array_t*)&rep_bytes_4};
static int32_t rep_sub2zero = 0; static int32_t rep_sub2zero = 0;
static int32_t rep_substuff2 = 2016; static int32_t rep_substuff2 = 2016;
static uint32_t rep_substuff3 = 2016; static uint32_t rep_substuff3 = 2016;
@@ -77,7 +78,7 @@ int main(int argc, char **argv)
int64_t opt_sfixed64 = 3052; int64_t opt_sfixed64 = 3052;
double opt_double = 3053.0; double opt_double = 3053.0;
char* opt_string = "3054"; char* opt_string = "3054";
pb_bytes_ptr_t opt_bytes = {4, (uint8_t*)"3055"}; PB_BYTES_ARRAY_T(4) opt_bytes = {4, {'3', '0', '5', '5'}};
static int32_t opt_substuff = 3056; static int32_t opt_substuff = 3056;
SubMessage opt_submsg = {"3056", &opt_substuff}; SubMessage opt_submsg = {"3056", &opt_substuff};
MyEnum opt_enum = MyEnum_Truth; MyEnum opt_enum = MyEnum_Truth;
@@ -117,7 +118,7 @@ int main(int argc, char **argv)
alltypes.req_sfixed64 = &req_sfixed64; alltypes.req_sfixed64 = &req_sfixed64;
alltypes.req_double = &req_double; alltypes.req_double = &req_double;
alltypes.req_string = req_string; alltypes.req_string = req_string;
alltypes.req_bytes = &req_bytes; alltypes.req_bytes = (pb_bytes_array_t*)&req_bytes;
alltypes.req_submsg = &req_submsg; alltypes.req_submsg = &req_submsg;
alltypes.req_enum = &req_enum; alltypes.req_enum = &req_enum;
alltypes.req_emptymsg = &req_emptymsg; alltypes.req_emptymsg = &req_emptymsg;
@@ -159,7 +160,7 @@ int main(int argc, char **argv)
alltypes.opt_sfixed64 = &opt_sfixed64; alltypes.opt_sfixed64 = &opt_sfixed64;
alltypes.opt_double = &opt_double; alltypes.opt_double = &opt_double;
alltypes.opt_string = opt_string; alltypes.opt_string = opt_string;
alltypes.opt_bytes = &opt_bytes; alltypes.opt_bytes = (pb_bytes_array_t*)&opt_bytes;
alltypes.opt_submsg = &opt_submsg; alltypes.opt_submsg = &opt_submsg;
alltypes.opt_enum = &opt_enum; alltypes.opt_enum = &opt_enum;
alltypes.opt_emptymsg = &opt_emptymsg; alltypes.opt_emptymsg = &opt_emptymsg;

View File

@@ -19,19 +19,24 @@ def add_nanopb_builders(env):
else: else:
infile = None infile = None
if env.has_key("COMMAND"):
args = [env["COMMAND"]]
else:
args = [str(source[0])] args = [str(source[0])]
if env.has_key('ARGS'): if env.has_key('ARGS'):
args.extend(env['ARGS']) args.extend(env['ARGS'])
print 'Command line: ' + str(args)
pipe = subprocess.Popen(args, pipe = subprocess.Popen(args,
stdin = infile, stdin = infile,
stdout = open(str(target[0]), 'w'), stdout = open(str(target[0]), 'w'),
stderr = sys.stderr) stderr = sys.stderr)
result = pipe.wait() result = pipe.wait()
if result == 0: if result == 0:
print '\033[32m[ OK ]\033[0m Ran ' + str(source[0]) print '\033[32m[ OK ]\033[0m Ran ' + args[0]
else: else:
print '\033[31m[FAIL]\033[0m Program ' + str(source[0]) + ' returned ' + str(result) print '\033[31m[FAIL]\033[0m Program ' + args[0] + ' returned ' + str(result)
return result return result
run_test_builder = Builder(action = run_test, run_test_builder = Builder(action = run_test,