Skip to content

Commit dacfd41

Browse files
Andrew WittenkevinAlbs
authored andcommitted
CDRIVER-3513 saved errors in out parameter (#650)
saved errors and added test case
1 parent 402189f commit dacfd41

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

src/libmongoc/src/mongoc/mongoc-cmd.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ mongoc_cmd_parts_append_opts (mongoc_cmd_parts_t *parts,
116116
uint32_t len;
117117
const uint8_t *data;
118118
bson_t read_concern;
119+
const char *to_append;
119120

120121
ENTRY;
121122

@@ -184,7 +185,13 @@ mongoc_cmd_parts_append_opts (mongoc_cmd_parts_t *parts,
184185
continue;
185186
}
186187

187-
if (!bson_append_iter (&parts->extra, bson_iter_key (iter), -1, iter)) {
188+
to_append = bson_iter_key (iter);
189+
if (!bson_append_iter (&parts->extra, to_append, -1, iter)) {
190+
bson_set_error (error,
191+
MONGOC_ERROR_COMMAND,
192+
MONGOC_ERROR_COMMAND_INVALID_ARG,
193+
"Failed to append \"%s\" to create command.",
194+
to_append);
188195
RETURN (false);
189196
}
190197
}

src/libmongoc/src/mongoc/mongoc-host-list.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,31 @@ _mongoc_host_list_from_string_with_err (mongoc_host_list_t *link_,
236236
/* if present, the port should immediately follow after ] */
237237
sport = strchr (close_bracket, ':');
238238
if (sport > close_bracket + 1) {
239+
bson_set_error (error,
240+
MONGOC_ERROR_COMMAND,
241+
MONGOC_ERROR_COMMAND_INVALID_ARG,
242+
"If present, port should immediately follow the \"]\""
243+
"in an IPv6 address"
244+
);
239245
return false;
240246
}
241247

242248
/* otherwise ] should be the last char. */
243249
if (!sport && *(close_bracket + 1) != '\0') {
250+
bson_set_error (error,
251+
MONGOC_ERROR_COMMAND,
252+
MONGOC_ERROR_COMMAND_INVALID_ARG,
253+
"If port is not supplied, \"[\" should be the last"
254+
"character");
244255
return false;
245256
}
246257

247258
if (*address != '[') {
259+
bson_set_error (error,
260+
MONGOC_ERROR_COMMAND,
261+
MONGOC_ERROR_COMMAND_INVALID_ARG,
262+
"Missing matching bracket \"[\""
263+
);
248264
return false;
249265
}
250266

@@ -259,10 +275,20 @@ _mongoc_host_list_from_string_with_err (mongoc_host_list_t *link_,
259275
if (sport) {
260276
if (sport == address) {
261277
/* bad address like ":27017" */
278+
bson_set_error (error,
279+
MONGOC_ERROR_COMMAND,
280+
MONGOC_ERROR_COMMAND_INVALID_ARG,
281+
"Bad address, \":\" should not be first character"
282+
);
262283
return false;
263284
}
264285

265286
if (!mongoc_parse_port (&port, sport + 1)) {
287+
bson_set_error (error,
288+
MONGOC_ERROR_COMMAND,
289+
MONGOC_ERROR_COMMAND_INVALID_ARG,
290+
"Port could not be parsed"
291+
);
266292
return false;
267293
}
268294

src/libmongoc/src/mongoc/mongoc-topology-scanner.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,10 @@ mongoc_topology_scanner_node_connect_unix (mongoc_topology_scanner_node_t *node,
903903
0 /* delay */);
904904
RETURN (true);
905905
}
906+
bson_set_error (error,
907+
MONGOC_ERROR_STREAM,
908+
MONGOC_ERROR_STREAM_CONNECT,
909+
"Failed to create TLS stream");
906910
RETURN (false);
907911
#endif
908912
}

src/libmongoc/src/mongoc/mongoc-uri.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,11 @@ mongoc_uri_apply_options (mongoc_uri_t *uri,
11271127
}
11281128

11291129
if (!mongoc_uri_set_option_as_bool (uri, canon, bval)) {
1130+
bson_set_error (error,
1131+
MONGOC_ERROR_COMMAND,
1132+
MONGOC_ERROR_COMMAND_INVALID_ARG,
1133+
"Failed to set %s to %d",
1134+
canon, bval);
11301135
return false;
11311136
}
11321137
} else {

src/libmongoc/tests/test-mongoc-uri.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,13 @@ test_mongoc_host_list_from_string (void)
923923
MONGOC_LOG_LEVEL_ERROR,
924924
"Could not parse address");
925925

926+
capture_logs (true);
927+
ASSERT (!_mongoc_host_list_from_string (&host_list, "[::1]extra_chars:27017"));
928+
ASSERT_CAPTURED_LOG ("_mongoc_host_list_from_string",
929+
MONGOC_LOG_LEVEL_ERROR,
930+
"If present, port should immediately follow the \"]\""
931+
"in an IPv6 address");
932+
926933
/* normal parsing, host and port are split, host is downcased */
927934
ASSERT (_mongoc_host_list_from_string (&host_list, "localHOST:27019"));
928935
ASSERT_CMPSTR (host_list.host_and_port, "localhost:27019");

0 commit comments

Comments
 (0)