Skip to content

Commit 4e3cf00

Browse files
authored
feat: add null checks & comments to all commmon C bindings (#91)
1 parent 1b1e66a commit 4e3cf00

File tree

13 files changed

+182
-61
lines changed

13 files changed

+182
-61
lines changed

libs/common/include/launchdarkly/bindings/c/array_builder.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ LD_EXPORT(void) LDArrayBuilder_Free(LDArrayBuilder array_builder);
3434
* After calling this method the provider LDValue is consumed. It should not
3535
* be accessed, and the caller doesn't need to call LDValue_Free.
3636
*
37-
* @param array_builder The array builder to add the value to.
38-
* @param val The value to add.
37+
* @param array_builder The array builder to add the value to. Must not be
38+
* NULL.
39+
* @param val The value to add. Must not be NULL.
3940
*/
4041
LD_EXPORT(void) LDArrayBuilder_Add(LDArrayBuilder array_builder, LDValue val);
4142

@@ -45,8 +46,9 @@ LD_EXPORT(void) LDArrayBuilder_Add(LDArrayBuilder array_builder, LDValue val);
4546
* After calling this method the array builder is consumed. It should not be
4647
* used and the caller does not need to call LDArrayBuilder_Free.
4748
*
48-
* @param array_builder The array builder to build an LDValue from.
49-
* @return The built LDValue.
49+
* @param array_builder The array builder to build an LDValue from. Must not be
50+
* NULL.
51+
* @return The built LDValue. Must not be NULL.
5052
*/
5153
LD_EXPORT(LDValue) LDArrayBuilder_Build(LDArrayBuilder array_builder);
5254

libs/common/include/launchdarkly/bindings/c/context.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ typedef struct _LDContext_PrivateAttributesIter*
1919
/**
2020
* Check if the context is valid.
2121
*
22-
* @param context The context to check.
22+
* @param context The context to check. Must not be NULL.
2323
* @return True if the context is valid.
2424
*/
2525
LD_EXPORT(bool) LDContext_Valid(LDContext context);
@@ -40,10 +40,12 @@ LD_EXPORT(void) LDContext_Free(LDContext context);
4040
* Do not access a returned LDValue from a context after the context has been
4141
* freed.
4242
*
43-
* @param context The context to get an attribute from
44-
* @param kind The kind within the context to get the attribute from.
45-
* @param ref An attribute reference representing the attribute to get.
46-
* @return The attribute value or a null pointer.
43+
* @param context The context to get an attribute from. Must not be NULL.
44+
* @param kind The kind within the context to get the attribute from. Must not
45+
* be NULL.
46+
* @param ref An attribute reference representing the attribute to get. Must not
47+
* be NULL.
48+
* @return The attribute value or a NULL pointer.
4749
*/
4850
LD_EXPORT(LDValue)
4951
LDContext_Get(LDContext context, char const* kind, char const* ref);
@@ -54,7 +56,7 @@ LDContext_Get(LDContext context, char const* kind, char const* ref);
5456
*
5557
* The lifetime of the returned string is tied to the LDContext.
5658
*
57-
* @param context The context to check for validity.
59+
* @param context The context to check for validity. Must not be NULL.
5860
* @return A string explaining why the context is not valid.
5961
*/
6062
LD_EXPORT(char const*) LDContext_Errors(LDContext context);
@@ -64,38 +66,38 @@ LD_EXPORT(char const*) LDContext_Errors(LDContext context);
6466
* context kind. If there is no such context kind, then a null pointer will
6567
* be returned.
6668
*
67-
* The iterator must be destroyed with LDContext_DestroyPrivateAttributesIter.
69+
* The iterator must be destroyed with LDContext_PrivateAttributesIter_Free.
6870
*
6971
* An iterator must not be used after the associated Context has been
7072
* freed.
7173
*
72-
* @param context The context containing the kind.
73-
* @param kind The kind to iterate private attributes for.
74+
* @param context The context containing the kind. Must not be NULL.
75+
* @param kind The kind to iterate private attributes for. Must not be NULL.
7476
* @return A private attributes iterator.
7577
*/
7678
LD_EXPORT(LDContext_PrivateAttributesIter)
77-
LDContext_CreatePrivateAttributesIter(LDContext context, char const* kind);
79+
LDContext_PrivateAttributesIter_New(LDContext context, char const* kind);
7880

7981
/**
8082
* Destroy the iterator.
8183
*
8284
* @param iter The iterator to destroy.
8385
*/
8486
LD_EXPORT(void)
85-
LDContext_DestroyPrivateAttributesIter(LDContext_PrivateAttributesIter iter);
87+
LDContext_PrivateAttributesIter_Free(LDContext_PrivateAttributesIter iter);
8688

8789
/**
8890
* Move the iterator to the next item.
8991
*
90-
* @param iter The iterator to increment.
92+
* @param iter The iterator to increment. Must not be NULL.
9193
*/
9294
LD_EXPORT(void)
9395
LDContext_PrivateAttributesIter_Next(LDContext_PrivateAttributesIter iter);
9496

9597
/**
9698
* Check if the iterator is at the end.
9799
*
98-
* @param iter The iterator to check.
100+
* @param iter The iterator to check. Must not be NULL.
99101
* @return True if the iterator is at the end.
100102
*/
101103
LD_EXPORT(bool)
@@ -106,7 +108,7 @@ LDContext_PrivateAttributesIter_End(LDContext_PrivateAttributesIter iter);
106108
*
107109
* The lifetime of the returned value is the same as the Context.
108110
*
109-
* @param iter The iterator to get a value for.
111+
* @param iter The iterator to get a value for. Must not be NULL.
110112
* @return The attribute reference as a string.
111113
*/
112114
LD_EXPORT(char const*)

libs/common/include/launchdarkly/bindings/c/context_builder.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ LD_EXPORT(void) LDContextBuilder_Free(LDContextBuilder builder);
4141
* When building a context using LDContextBuilder_Build the builder will be
4242
* consumed and you do not need to call LDContextBuilder_Free.
4343
*
44-
* @param builder The builder to build a context from.
44+
* @param builder The builder to build a context from. Must not be NULL.
4545
* @return The built context.
4646
*/
4747
LD_EXPORT(LDContext) LDContextBuilder_Build(LDContextBuilder builder);
@@ -58,9 +58,9 @@ LD_EXPORT(LDContext) LDContextBuilder_Build(LDContextBuilder builder);
5858
* If you call LDContextBuilder_AddKind a second time, with an already specified
5959
* kind, but a different key, then the key for that kind will be updated.
6060
*
61-
* @param builder The builder to add the kind to.
62-
* @param kind The kind to add.
63-
* @param key The key for that kind.
61+
* @param builder The builder to add the kind to. Must not be NULL.
62+
* @param kind The kind to add. Must not be NULL.
63+
* @param key The key for that kind. Must not be NULL.
6464
*/
6565
LD_EXPORT(void)
6666
LDContextBuilder_AddKind(LDContextBuilder builder,
@@ -74,9 +74,9 @@ LDContextBuilder_AddKind(LDContextBuilder builder,
7474
* You should not access the value after adding it to the builder, and you
7575
* do not need to call LDValue_Free on the value.
7676
*
77-
* @param kind The kind to add the attribute to.
78-
* @param attr_key The key of the attribute to add.
79-
* @param val The value of the attribute to add.
77+
* @param kind The kind to add the attribute to. Must not be NULL.
78+
* @param attr_key The key of the attribute to add. Must not be NULL.
79+
* @param val The value of the attribute to add. Must not be NULL.
8080
*/
8181
LD_EXPORT(bool)
8282
LDContextBuilder_Attributes_Set(LDContextBuilder builder,
@@ -99,9 +99,9 @@ LDContextBuilder_Attributes_Set(LDContextBuilder builder,
9999
* convenience which also adds the attribute to the private attributes list,
100100
* as if using LDContextBuilder_Attributes_AddPrivateAttribute.
101101
*
102-
* @param kind The kind to set the private attribute for.
103-
* @param attr_key The key of the private attribute.
104-
* @param val The value of the private attribute.
102+
* @param kind The kind to set the private attribute for. Must not be NULL.
103+
* @param attr_key The key of the private attribute. Must not be NULL.
104+
* @param val The value of the private attribute. Must not be NULL.
105105
*/
106106
LD_EXPORT(bool)
107107
LDContextBuilder_Attributes_SetPrivate(LDContextBuilder builder,
@@ -117,8 +117,8 @@ LDContextBuilder_Attributes_SetPrivate(LDContextBuilder builder,
117117
* This method will make a copy of the name string, and the caller remains
118118
* responsible for the original name string.
119119
*
120-
* @param kind The kind to set the name for.
121-
* @param name The name to set.
120+
* @param kind The kind to set the name for. Must not be NULL.
121+
* @param name The name to set. Must not be NULL.
122122
*/
123123
LD_EXPORT(bool)
124124
LDContextBuilder_Attributes_SetName(LDContextBuilder builder,
@@ -131,7 +131,7 @@ LDContextBuilder_Attributes_SetName(LDContextBuilder builder,
131131
* If true, the context will _not_ appear on the Contexts page in the
132132
* LaunchDarkly dashboard.
133133
*
134-
* @param kind The kind to set the anonymous attribute for.
134+
* @param kind The kind to set the anonymous attribute for. Must not be NULL.
135135
* @param anonymous The value to set the anonymous attribute to.
136136
*/
137137
LD_EXPORT(bool)
@@ -182,8 +182,8 @@ LDContextBuilder_Attributes_SetAnonymous(LDContextBuilder builder,
182182
* This method will make a copy of the attr_ref string, and the caller remains
183183
* responsible for the original name string.
184184
*
185-
* @param kind The kind to set the attribute as private for.
186-
* @param attr_ref An attribute reference.
185+
* @param kind The kind to set the attribute as private for. Must not be NULL.
186+
* @param attr_ref An attribute reference. Must not be NULL.
187187
*/
188188
LD_EXPORT(bool)
189189
LDContextBuilder_Attributes_AddPrivateAttribute(LDContextBuilder builder,

libs/common/include/launchdarkly/bindings/c/object_builder.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
#pragma once
44

5-
#include <launchdarkly/bindings/c/value.h>
65
#include <launchdarkly/bindings/c/export.h>
6+
#include <launchdarkly/bindings/c/value.h>
77

88
#ifdef __cplusplus
99
extern "C" { // only need to export C interface if
@@ -36,8 +36,8 @@ LD_EXPORT(void) LDObjectBuilder_Free(LDObjectBuilder builder);
3636
* be copied.
3737
*
3838
* @param array_builder The object builder to add the value to.
39-
* @param key The key for the value being added.
40-
* @param val The value to add.
39+
* @param key The key for the value being added. Must not be NULL.
40+
* @param val The value to add. Must not be NULL.
4141
*/
4242
LD_EXPORT(void)
4343
LDObjectBuilder_Add(LDObjectBuilder builder, char const* key, LDValue val);
@@ -48,7 +48,7 @@ LDObjectBuilder_Add(LDObjectBuilder builder, char const* key, LDValue val);
4848
* After calling this method the object builder is consumed. It should not be
4949
* used and the caller does not need to call LDObjectBuilder_Free.
5050
*
51-
* @param builder The object builder to build an LDValue from.
51+
* @param builder The object builder to build an LDValue from. Must not be NULL.
5252
* @return The built LDValue.
5353
*/
5454
LD_EXPORT(LDValue) LDObjectBuilder_Build(LDObjectBuilder builder);

libs/common/include/launchdarkly/bindings/c/value.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ LD_EXPORT(LDValue) LDValue_NewNumber(double val);
109109
* The input string will be copied. To avoid the copy, see
110110
* LDValue_ConstantString.
111111
*
112-
* @param val Constant reference to a string. The string is copied. Cannot be
112+
* @param val Constant reference to a string. The string is copied. Must not be
113113
* NULL.
114114
* @return New LDValue.
115115
*/
@@ -118,7 +118,7 @@ LD_EXPORT(LDValue) LDValue_NewString(char const* val);
118118
/**
119119
* Allocates an LDValue by cloning an existing LDValue.
120120
*
121-
* @param source Source LDValue. Must not be `NULL`.
121+
* @param source Source LDValue. Must not be NULL.
122122
* @return New LDValue.
123123
*/
124124
LD_EXPORT(LDValue) LDValue_NewValue(LDValue val);
@@ -129,13 +129,13 @@ LD_EXPORT(LDValue) LDValue_NewValue(LDValue val);
129129
* An LDValue should only be freed when directly owned by the caller, i.e.,
130130
* it was never moved into an LDArray or LDObject.
131131
*
132-
* @param value LDValue to free. No-op if NULL.
132+
* @param value LDValue to free.
133133
*/
134134
LD_EXPORT(void) LDValue_Free(LDValue val);
135135

136136
/**
137137
* Returns the type of an LDValue.
138-
* @param value LDValue to inspect. Cannot be NULL.
138+
* @param value LDValue to inspect. Must not be NULL.
139139
* @return Type of the LDValue, or LDValueType_Unrecognized if the type is
140140
* unrecognized.
141141
*/
@@ -144,14 +144,14 @@ LD_EXPORT(enum LDValueType) LDValue_Type(LDValue val);
144144
/**
145145
* Obtain value of a boolean-type LDValue, otherwise returns LDBooleanFalse.
146146
*
147-
* @param value Target LDValue. Cannot be NULL.
147+
* @param value Target LDValue. Must not be NULL.
148148
* @return Boolean value, or false if not boolean-type.
149149
*/
150150
LD_EXPORT(bool) LDValue_GetBool(LDValue val);
151151

152152
/**
153153
* Obtain value of a number-type LDValue, otherwise return 0.
154-
* @param value Target LDValue. Cannot be NULL.
154+
* @param value Target LDValue. Must not be NULL.
155155
* @return Number value, or 0 if not number-type.
156156
*/
157157
LD_EXPORT(double) LDValue_GetNumber(LDValue val);
@@ -162,7 +162,7 @@ LD_EXPORT(double) LDValue_GetNumber(LDValue val);
162162
* the LDValue. If you need the string outside this lifetime, then a copy
163163
* should be made.
164164
*
165-
* @param value Target LDValue. Cannot be NULL.
165+
* @param value Target LDValue. Must not be NULL.
166166
* @return String value, or empty string if not string-type.
167167
*/
168168
LD_EXPORT(char const*) LDValue_GetString(LDValue val);
@@ -173,7 +173,7 @@ LD_EXPORT(char const*) LDValue_GetString(LDValue val);
173173
*
174174
* If not an array-type or object-type, returns 0.
175175
*
176-
* @param value Target LDValue. Cannot be NULL.
176+
* @param value Target LDValue. Must not be NULL.
177177
* @return Count of LDValue elements, or 0 if not array-type/object-type.
178178
*/
179179
LD_EXPORT(unsigned int) LDValue_Count(LDValue val);
@@ -183,7 +183,7 @@ LD_EXPORT(unsigned int) LDValue_Count(LDValue val);
183183
*
184184
* The iterator starts at the first element.
185185
*
186-
* @param value Target LDValue. Cannot be NULL.
186+
* @param value Target LDValue. Must not be NULL.
187187
* @return Iterator, or NULL if not an array-type. The iterator
188188
* must should be destroyed with LDValue_DestroyArrayIter.
189189
*/
@@ -193,22 +193,22 @@ LD_EXPORT(LDValue_ArrayIter) LDValue_CreateArrayIter(LDValue val);
193193
* Move the array-type iterator to the next item. Should only be done for an
194194
* iterator which is not at the end.
195195
*
196-
* @param iter The iterator to advance.
196+
* @param iter The iterator to advance. Must not be NULL.
197197
*/
198198
LD_EXPORT(void) LDValue_ArrayIter_Next(LDValue_ArrayIter iter);
199199

200200
/**
201201
* Check if an array-type iterator is at the end.
202202
*
203-
* @param iter The iterator to check.
203+
* @param iter The iterator to check. Must not be NULL.
204204
* @return True if the iterator is at the end.
205205
*/
206206
LD_EXPORT(bool) LDValue_ArrayIter_End(LDValue_ArrayIter iter);
207207

208208
/**
209209
* Get the value for the array-type iterator.
210210
*
211-
* @param iter The iterator to get a value for.
211+
* @param iter The iterator to get a value for. Must not be NULL.
212212
* @return The value.
213213
*/
214214
LD_EXPORT(LDValue) LdValue_ArrayIter_Value(LDValue_ArrayIter iter);
@@ -224,7 +224,7 @@ LD_EXPORT(void) LDValue_DestroyArrayIter(LDValue_ArrayIter iter);
224224
*
225225
* The iterator starts at the first element.
226226
*
227-
* @param value Target LDValue. Cannot be NULL.
227+
* @param value Target LDValue. Must not be NULL.
228228
* @return Iterator, or NULL if not an object-type. The iterator
229229
* must should be destroyed with LDValue_DestroyObjectIter.
230230
*/
@@ -234,22 +234,22 @@ LD_EXPORT(LDValue_ObjectIter) LDValue_CreateObjectIter(LDValue val);
234234
* Move the object-type iterator to the next item. Should only be done for an
235235
* iterator which is not at the end.
236236
*
237-
* @param iter The iterator to advance.
237+
* @param iter The iterator to advance. Must not be NULL.
238238
*/
239239
LD_EXPORT(void) LDValue_ObjectIter_Next(LDValue_ObjectIter iter);
240240

241241
/**
242242
* Check if an object-type iterator is at the end.
243243
*
244-
* @param iter The iterator to check.
244+
* @param iter The iterator to check. Must not be NULL.
245245
* @return True if the iterator is at the end.
246246
*/
247247
LD_EXPORT(bool) LDValue_ObjectIter_End(LDValue_ObjectIter iter);
248248

249249
/**
250250
* Get the value for an object-type iterator.
251251
*
252-
* @param iter The iterator to get a value for.
252+
* @param iter The iterator to get a value for. Must not be NULL.
253253
* @return The value.
254254
*/
255255
LD_EXPORT(LDValue) LdValue_ObjectIter_Value(LDValue_ObjectIter iter);
@@ -259,7 +259,7 @@ LD_EXPORT(LDValue) LdValue_ObjectIter_Value(LDValue_ObjectIter iter);
259259
*
260260
* The returned key has a lifetime attached to that of the LDValue.
261261
*
262-
* @param iter The iterator to get a key for.
262+
* @param iter The iterator to get a key for. Must not be NULL.
263263
* @return The key.
264264
*/
265265
LD_EXPORT(char const*) LdValue_ObjectIter_Key(LDValue_ObjectIter iter);

libs/common/src/bindings/c/array_builder.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <launchdarkly/bindings/c/array_builder.h>
55
#include <launchdarkly/value.hpp>
6+
#include "../../c_binding_helpers.hpp"
67

78
#include <vector>
89

@@ -21,12 +22,17 @@ LD_EXPORT(void) LDArrayBuilder_Free(LDArrayBuilder array_builder) {
2122
}
2223

2324
LD_EXPORT(void) LDArrayBuilder_Add(LDArrayBuilder array_builder, LDValue val) {
25+
LD_ASSERT_NOT_NULL(array_builder);
26+
LD_ASSERT_NOT_NULL(val);
27+
2428
auto vector = AS_VECTOR(array_builder);
2529
vector->emplace_back(std::move(*AS_VALUE(val)));
2630
LDValue_Free(val);
2731
}
2832

2933
LD_EXPORT(LDValue) LDArrayBuilder_Build(LDArrayBuilder array_builder) {
34+
LD_ASSERT_NOT_NULL(array_builder);
35+
3036
auto vector = AS_VECTOR(array_builder);
3137
auto value = new Value(std::move(*vector));
3238
delete vector;

0 commit comments

Comments
 (0)