Skip to content

Commit 3482065

Browse files
committed
Create separate macro for non-optional bson_t return value
Also updates doc blocks to better clarify return types
1 parent 7573e61 commit 3482065

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/MongoDB/ClientEncryption.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,22 @@ static void phongo_clientencryption_create_datakey(php_phongo_clientencryption_t
4040
static void phongo_clientencryption_encrypt(php_phongo_clientencryption_t* clientencryption, zval* zvalue, zval* zciphertext, zval* options);
4141
static void phongo_clientencryption_decrypt(php_phongo_clientencryption_t* clientencryption, zval* zciphertext, zval* zvalue);
4242

43-
#define RETVAL_OPTIONAL_BSON_T(reply) \
44-
do { \
45-
RETVAL_NULL(); \
46-
if (!bson_empty(&(reply))) { \
47-
php_phongo_bson_state state = { 0 }; \
48-
if (!php_phongo_bson_to_zval_ex(bson_get_data(&(reply)), (reply).len, &state)) { \
49-
zval_ptr_dtor(&state.zchild); \
50-
goto cleanup; \
51-
} \
52-
RETVAL_ZVAL(&state.zchild, 0, 1); \
53-
} \
43+
#define RETVAL_BSON_T(reply) \
44+
do { \
45+
php_phongo_bson_state state = { 0 }; \
46+
if (!php_phongo_bson_to_zval_ex(bson_get_data(&(reply)), (reply).len, &state)) { \
47+
zval_ptr_dtor(&state.zchild); \
48+
goto cleanup; \
49+
} \
50+
RETVAL_ZVAL(&state.zchild, 0, 1); \
51+
} while (0)
52+
53+
#define RETVAL_OPTIONAL_BSON_T(reply) \
54+
do { \
55+
RETVAL_NULL(); \
56+
if (!bson_empty(&(reply))) { \
57+
RETVAL_BSON_T(reply); \
58+
} \
5459
} while (0)
5560

5661
/* Returns true if keyid is a UUID Binary value with an appropriate data length;
@@ -133,7 +138,8 @@ static PHP_METHOD(MongoDB_Driver_ClientEncryption, addKeyAltName)
133138
} /* }}} */
134139

135140
/* {{{ proto MongoDB\BSON\Binary MongoDB\Driver\ClientEncryption::createDataKey(string $kmsProvider[, array $options])
136-
Creates a new key document and inserts into the key vault collection. */
141+
Creates a new key document and inserts into the key vault collection and
142+
returns its identifier (UUID as a BSON binary with subtype 0x04). */
137143
static PHP_METHOD(MongoDB_Driver_ClientEncryption, createDataKey)
138144
{
139145
char* kms_provider = NULL;
@@ -183,7 +189,12 @@ static PHP_METHOD(MongoDB_Driver_ClientEncryption, deleteKey)
183189
goto cleanup;
184190
}
185191

186-
RETVAL_OPTIONAL_BSON_T(reply);
192+
if (bson_empty(&reply)) {
193+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "mongoc_client_encryption_delete_key returned an empty document");
194+
goto cleanup;
195+
}
196+
197+
RETVAL_BSON_T(reply);
187198

188199
cleanup:
189200
bson_value_destroy(&keyid);

0 commit comments

Comments
 (0)