diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bson/bson.c | 30 | ||||
-rw-r--r-- | src/bson/bson.h | 47 |
2 files changed, 64 insertions, 13 deletions
diff --git a/src/bson/bson.c b/src/bson/bson.c index 6ecde34..b177fac 100644 --- a/src/bson/bson.c +++ b/src/bson/bson.c @@ -806,28 +806,48 @@ void bson_iterator_subiterator(const bson_iterator *i, bson_iterator *sub) { BUILDING ------------------------------ */ -static void _bson_init_size(bson *b, int size) { +static int _bson_init_size(bson *b, int size) { if (size == 0) { b->data = NULL; } else { b->data = (char *) bson_malloc(size); + if (!b->data) { + bson_fatal_msg(!!b->data, "malloc() failed"); + return BSON_ERROR; + } } b->dataSize = size; b->cur = b->data + 4; bson_reset(b); + + return BSON_OK; } void bson_init(bson *b) { - _bson_init_size(b, initialBufferSize); + (void)_bson_init_size(b, initialBufferSize); } void bson_init_as_query(bson *b) { - bson_init(b); - b->flags |= BSON_FLAG_QUERY_MODE; + (void)bson_safe_init_as_query(b); } void bson_init_size(bson *b, int size) { - _bson_init_size(b, size); + (void)_bson_init_size(b, size); +} + +int bson_safe_init(bson *b) { + return _bson_init_size(b, initialBufferSize); +} + +int bson_safe_init_as_query(bson *b) { + if (_bson_init_size(b, initialBufferSize) == BSON_ERROR) + return BSON_ERROR; + b->flags |= BSON_FLAG_QUERY_MODE; + return BSON_OK; +} + +int bson_safe_init_size(bson *b, int size) { + return _bson_init_size(b, size); } void bson_init_on_stack(bson *b, char *bstack, int mincapacity, int maxonstack) { diff --git a/src/bson/bson.h b/src/bson/bson.h index 76dbac4..41f7757 100644 --- a/src/bson/bson.h +++ b/src/bson/bson.h @@ -60,8 +60,9 @@ enum bson_validity_t { enum bson_binary_subtype_t { BSON_BIN_BINARY = 0, BSON_BIN_FUNC = 1, - BSON_BIN_BINARY_OLD = 2, - BSON_BIN_UUID = 3, + BSON_BIN_BINARY_OLD = 2, /**< Deprecated */ + BSON_BIN_UUID_OLD = 3, /**< Deprecated */ + BSON_BIN_UUID = 4, BSON_BIN_MD5 = 5, BSON_BIN_USER = 128 }; @@ -78,7 +79,7 @@ typedef enum { BSON_OBJECT = 3, BSON_ARRAY = 4, BSON_BINDATA = 5, - BSON_UNDEFINED = 6, + BSON_UNDEFINED = 6, /**< Deprecated */ BSON_OID = 7, BSON_BOOL = 8, BSON_DATE = 9, @@ -86,7 +87,7 @@ typedef enum { BSON_REGEX = 11, BSON_DBREF = 12, /**< Deprecated. */ BSON_CODE = 13, - BSON_SYMBOL = 14, + BSON_SYMBOL = 14, /**< Deprecated. */ BSON_CODEWSCOPE = 15, BSON_INT = 16, BSON_TIMESTAMP = 17, @@ -639,19 +640,49 @@ EJDB_EXPORT int bson_init_data(bson *b, char *data); EJDB_EXPORT int bson_init_finished_data(bson *b, const char *data); /** - * Initialize a BSON object, and set its - * buffer to the given size. + * Initialize a BSON object and set its buffer to the given size. * * @param b the BSON object to initialize. * @param size the initial size of the buffer. - * - * @return BSON_OK or BSON_ERROR. */ EJDB_EXPORT void bson_init_size(bson *b, int size); EJDB_EXPORT void bson_init_on_stack(bson *b, char *bstack, int mincapacity, int maxonstack); /** + * Initialize a BSON object. If not created with bson_new, you must + * initialize each new bson object using this function. Report + * problems with memory allocation. + * + * @param b the BSON object to initialize. + * + * @return BSON_OK or BSON_ERROR. + */ +EJDB_EXPORT int bson_safe_init(bson *b); + +/** + * Initialize a BSON object and set its buffer to the given size. + * Report problems with memory allocation. + * + * @param b the BSON object to initialize. + * @param size the inintial size of the buffer. + * + * @return BSON_OK or BSON_ERROR. + */ +EJDB_EXPORT int bson_safe_init_size(bson *b, int size); + +/** + * Intialize a BSON object. In query contruction mode allowing dot and + * dollar chars in field names. Report problems with memory + * allocation. + * + * @param b + * + * @return BSON_OK or BSON_ERROR + */ +EJDB_EXPORT int bson_safe_init_as_query(bson *b); + +/** * Grow a bson object. * * @param b the bson to grow. |