static void test_bson_corrupt_binary (void) { uint8_t *buf; bson_t b; size_t buflen = 1024; char *str; int fd; ssize_t r; buf = bson_malloc0(buflen); fd = bson_open(BINARY_DIR"/test57.bson", O_RDONLY); BSON_ASSERT(-1 != fd); r = bson_read(fd, buf, buflen); BSON_ASSERT(r == 26); r = bson_init_static(&b, buf, (uint32_t)r); BSON_ASSERT(r); str = bson_as_json(&b, NULL); BSON_ASSERT(!str); bson_destroy(&b); bson_free(buf); }
/* *------------------------------------------------------------------------- * * mongoc_server_description_new_copy -- * * A copy of a server description that you must destroy, or NULL. * *------------------------------------------------------------------------- */ mongoc_server_description_t * mongoc_server_description_new_copy (const mongoc_server_description_t *description) { mongoc_server_description_t *copy; if (!description) { return NULL; } copy = (mongoc_server_description_t *)bson_malloc0(sizeof (*copy)); copy->id = description->id; memcpy (©->host, &description->host, sizeof (copy->host)); copy->round_trip_time = -1; copy->connection_address = copy->host.host_and_port; /* wait for handle_ismaster to fill these in properly */ copy->has_is_master = false; bson_init_static (©->hosts, kMongocEmptyBson, sizeof (kMongocEmptyBson)); bson_init_static (©->passives, kMongocEmptyBson, sizeof (kMongocEmptyBson)); bson_init_static (©->arbiters, kMongocEmptyBson, sizeof (kMongocEmptyBson)); bson_init_static (©->tags, kMongocEmptyBson, sizeof (kMongocEmptyBson)); bson_init (©->last_is_master); if (description->has_is_master) { mongoc_server_description_handle_ismaster (copy, &description->last_is_master, description->round_trip_time, NULL); } /* Preserve the error */ memcpy (©->error, &description->error, sizeof copy->error); return copy; }
static void test_bson_as_json_stack_overflow (void) { uint8_t *buf; bson_t b; size_t buflen = 1024 * 1024 * 17; char *str; int fd; ssize_t r; buf = bson_malloc0(buflen); fd = bson_open(BINARY_DIR"/stackoverflow.bson", O_RDONLY); BSON_ASSERT(-1 != fd); r = bson_read(fd, buf, buflen); BSON_ASSERT(r == 16777220); r = bson_init_static(&b, buf, 16777220); BSON_ASSERT(r); str = bson_as_json(&b, NULL); BSON_ASSERT(str); r = !!strstr(str, "..."); BSON_ASSERT(str); bson_free(str); bson_destroy(&b); bson_free(buf); }
void mongoc_uri_append_host (mongoc_uri_t *uri, const char *host, uint16_t port) { mongoc_host_list_t *iter; mongoc_host_list_t *link_; link_ = (mongoc_host_list_t *)bson_malloc0(sizeof *link_); mongoc_uri_lowercase_hostname(host, link_->host, sizeof link_->host); if (strchr (host, ':')) { bson_snprintf (link_->host_and_port, sizeof link_->host_and_port, "[%s]:%hu", host, port); link_->family = AF_INET6; } else { bson_snprintf (link_->host_and_port, sizeof link_->host_and_port, "%s:%hu", host, port); link_->family = strstr (host, ".sock") ? AF_UNIX : AF_INET; } link_->host_and_port[sizeof link_->host_and_port - 1] = '\0'; link_->port = port; if ((iter = uri->hosts)) { for (; iter && iter->next; iter = iter->next) {} iter->next = link_; } else { uri->hosts = link_; } }
static void test_bson_writer_null_realloc_2 (void) { const uint8_t testdata[] = { 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0 }; bson_writer_t *writer; uint8_t *buf = bson_malloc0(32); size_t buflen = 32; bson_t *b; int r; int i; writer = bson_writer_new(&buf, &buflen, 0, NULL, NULL); for (i=0; i<5; i++) { assert(bson_writer_begin(writer, &b)); bson_writer_end(writer); } assert(bson_writer_begin(writer, &b)); assert(!bson_append_int32(b, "a", -1, 123)); bson_writer_end(writer); r = memcmp(buf, testdata, 32); assert(r == 0); bson_writer_destroy(writer); bson_free (buf); }
mongoc_uri_t * mongoc_uri_new (const char *uri_string) { mongoc_uri_t *uri; uri = bson_malloc0(sizeof *uri); bson_init(&uri->options); bson_init(&uri->credentials); bson_init(&uri->read_prefs); if (!uri_string) { uri_string = "mongodb://127.0.0.1/"; } if (!mongoc_uri_parse(uri, uri_string)) { mongoc_uri_destroy(uri); return NULL; } _mongoc_uri_build_write_concern (uri); if (!_mongoc_write_concern_is_valid(uri->write_concern)) { mongoc_uri_destroy(uri); return NULL; } uri->str = bson_strdup(uri_string); return uri; }
mongoc_cursor_t * _mongoc_cursor_cmd_new (mongoc_client_t *client, const char *db_and_coll, const bson_t *cmd, const bson_t *opts, const mongoc_read_prefs_t *user_prefs, const mongoc_read_prefs_t *default_prefs, const mongoc_read_concern_t *read_concern) { mongoc_cursor_t *cursor; data_cmd_t *data = bson_malloc0 (sizeof (*data)); cursor = _mongoc_cursor_new_with_opts ( client, db_and_coll, opts, user_prefs, default_prefs, read_concern); _mongoc_cursor_response_legacy_init (&data->response_legacy); _mongoc_cursor_check_and_copy_to (cursor, "command", cmd, &data->cmd); bson_init (&data->response.reply); cursor->impl.prime = _prime; cursor->impl.pop_from_batch = _pop_from_batch; cursor->impl.get_next_batch = _get_next_batch; cursor->impl.destroy = _destroy; cursor->impl.clone = _clone; cursor->impl.data = (void *) data; return cursor; }
static void test_mongoc_buffer_basic (void) { mongoc_stream_t *stream; mongoc_buffer_t buf; uint8_t *data = bson_malloc0(1024); bson_error_t error = { 0 }; ssize_t r; mongoc_fd_t fd; fd = mongoc_open("tests/binary/reply1.dat", O_RDONLY); ASSERT(mongoc_fd_is_valid(fd)); stream = mongoc_stream_unix_new(fd); ASSERT(stream); _mongoc_buffer_init(&buf, data, 1024, bson_realloc); r = _mongoc_buffer_fill(&buf, stream, 537, 0, &error); ASSERT_CMPINT((int)r, ==, -1); r = _mongoc_buffer_fill(&buf, stream, 536, 0, &error); ASSERT_CMPINT((int)r, ==, 536); ASSERT(buf.len == 536); _mongoc_buffer_destroy(&buf); _mongoc_buffer_destroy(&buf); _mongoc_buffer_destroy(&buf); _mongoc_buffer_destroy(&buf); mongoc_stream_destroy(stream); }
mongoc_stream_t * mongoc_stream_socket_new (mongoc_socket_t *sock) /* IN */ { mongoc_stream_socket_t *stream; BSON_ASSERT (sock); stream = (mongoc_stream_socket_t *) bson_malloc0 (sizeof *stream); stream->vtable.type = MONGOC_STREAM_SOCKET; stream->vtable.close = _mongoc_stream_socket_close; stream->vtable.destroy = _mongoc_stream_socket_destroy; stream->vtable.failed = _mongoc_stream_socket_failed; stream->vtable.flush = _mongoc_stream_socket_flush; stream->vtable.readv = _mongoc_stream_socket_readv; stream->vtable.writev = _mongoc_stream_socket_writev; stream->vtable.setsockopt = _mongoc_stream_socket_setsockopt; stream->vtable.check_closed = _mongoc_stream_socket_check_closed; stream->vtable.timed_out = _mongoc_stream_socket_timed_out; stream->vtable.should_retry = _mongoc_stream_socket_should_retry; stream->vtable.poll = _mongoc_stream_socket_poll; stream->sock = sock; mongoc_counter_streams_active_inc (); return (mongoc_stream_t *) stream; }
mongoc_uri_t * mongoc_uri_copy (const mongoc_uri_t *uri) { mongoc_uri_t *copy; mongoc_host_list_t *iter; BSON_ASSERT (uri); copy = (mongoc_uri_t *)bson_malloc0(sizeof (*copy)); copy->str = bson_strdup (uri->str); copy->username = bson_strdup (uri->username); copy->password = bson_strdup (uri->password); copy->database = bson_strdup (uri->database); copy->read_prefs = mongoc_read_prefs_copy (uri->read_prefs); copy->read_concern = mongoc_read_concern_copy (uri->read_concern); copy->write_concern = mongoc_write_concern_copy (uri->write_concern); for (iter = uri->hosts; iter; iter = iter->next) { mongoc_uri_append_host (copy, iter->host, iter->port); } bson_copy_to (&uri->options, ©->options); bson_copy_to (&uri->credentials, ©->credentials); return copy; }
mongoc_cursor_t * _mongoc_cursor_clone (const mongoc_cursor_t *cursor) { mongoc_cursor_t *clone; ENTRY; BSON_ASSERT (cursor); clone = bson_malloc0 (sizeof *clone); clone->client = cursor->client; clone->is_command = cursor->is_command; clone->flags = cursor->flags; clone->skip = cursor->skip; clone->batch_size = cursor->batch_size; clone->limit = cursor->limit; clone->nslen = cursor->nslen; if (cursor->read_prefs) { clone->read_prefs = mongoc_read_prefs_copy (cursor->read_prefs); } bson_copy_to (&cursor->query, &clone->query); bson_copy_to (&cursor->fields, &clone->fields); bson_strcpy_w_null(clone->ns, cursor->ns, sizeof clone->ns); _mongoc_buffer_init (&clone->buffer, NULL, 0, NULL); mongoc_counter_cursors_active_inc (); RETURN(clone); }
static void multi_download_setup (perf_test_t *test) { multi_download_test_t *download_test; multi_download_thread_context_t *ctx; mongoc_uri_t *uri; int i; _setup_load_gridfs_files (); perf_test_setup (test); download_test = (multi_download_test_t *) test; uri = mongoc_uri_new (NULL); download_test->pool = mongoc_client_pool_new (uri); download_test->cnt = 50; /* DANGER!: assumes test corpus won't change */ download_test->contexts = (multi_download_thread_context_t *) bson_malloc0 ( download_test->cnt * sizeof (multi_download_thread_context_t)); for (i = 0; i < download_test->cnt; i++) { ctx = &download_test->contexts[i]; ctx->filename = bson_strdup_printf ("file%d.txt", i); ctx->path = bson_strdup_printf ("%s/%s", test->data_path, ctx->filename); } mongoc_uri_destroy (uri); }
mongoc_database_t * _mongoc_database_new (mongoc_client_t *client, const char *name, const mongoc_read_prefs_t *read_prefs, const mongoc_write_concern_t *write_concern) { mongoc_database_t *db; ENTRY; bson_return_val_if_fail(client, NULL); bson_return_val_if_fail(name, NULL); db = bson_malloc0(sizeof *db); db->client = client; db->write_concern = write_concern ? mongoc_write_concern_copy(write_concern) : mongoc_write_concern_new(); db->read_prefs = read_prefs ? mongoc_read_prefs_copy(read_prefs) : mongoc_read_prefs_new(MONGOC_READ_PRIMARY); bson_strncpy (db->name, name, sizeof db->name); RETURN(db); }
mongoc_database_t * _mongoc_database_new (mongoc_client_t *client, const char *name, const mongoc_read_prefs_t *read_prefs, const mongoc_read_concern_t *read_concern, const mongoc_write_concern_t *write_concern) { mongoc_database_t *db; ENTRY; BSON_ASSERT (client); BSON_ASSERT (name); db = (mongoc_database_t *)bson_malloc0(sizeof *db); db->client = client; db->write_concern = write_concern ? mongoc_write_concern_copy(write_concern) : mongoc_write_concern_new(); db->read_concern = read_concern ? mongoc_read_concern_copy(read_concern) : mongoc_read_concern_new(); db->read_prefs = read_prefs ? mongoc_read_prefs_copy(read_prefs) : mongoc_read_prefs_new(MONGOC_READ_PRIMARY); bson_strncpy (db->name, name, sizeof db->name); RETURN(db); }
char * bson_strdupv_printf (const char *format, va_list args) { va_list my_args; char *buf; int len = 32; int n; bson_return_val_if_fail(format, NULL); buf = bson_malloc0(len); while (TRUE) { va_copy(my_args, args); n = vsnprintf(buf, len, format, my_args); va_end(my_args); if (n > -1 && n < len) { return buf; } if (n > -1) { len = n + 1; } else { len *= 2; } buf = bson_realloc(buf, len); } }
mongoc_apm_callbacks_t * mongoc_apm_callbacks_new (void) { size_t s = sizeof (mongoc_apm_callbacks_t); return (mongoc_apm_callbacks_t *) bson_malloc0 (s); }
/** * mongoc_stream_new_from_unix: * @fd: A unix style file-descriptor. * * Create a new mongoc_stream_t for a UNIX file descriptor. This is * expected to be a socket of some sort (such as a UNIX or TCP socket). * * This may be useful after having connected to a peer to provide a * higher level API for reading and writing. It also allows for * interoperability with external stream abstractions in higher level * languages. * * Returns: A newly allocated mongoc_stream_t that should be freed with * mongoc_stream_destroy(). */ mongoc_stream_t * mongoc_stream_new_from_unix (int fd) { mongoc_stream_unix_t *stream; int flags; bson_return_val_if_fail(fd != -1, NULL); /* * If we cannot put the file-descriptor in O_NONBLOCK mode, there isn't much * we can do. Just fail. */ flags = fcntl(fd, F_GETFD); if ((flags & O_NONBLOCK) != O_NONBLOCK) { if (fcntl(fd, F_SETFD, flags | O_NONBLOCK) == -1) { MONGOC_WARNING("Failed to set O_NONBLOCK on file descriptor!"); return NULL; } } stream = bson_malloc0(sizeof *stream); stream->fd = fd; stream->stream.destroy = mongoc_stream_unix_destroy; stream->stream.close = mongoc_stream_unix_close; stream->stream.flush = mongoc_stream_unix_flush; stream->stream.writev = mongoc_stream_unix_writev; stream->stream.readv = mongoc_stream_unix_readv; stream->stream.cork = mongoc_stream_unix_cork; stream->stream.uncork = mongoc_stream_unix_uncork; return (mongoc_stream_t *)stream; }
bson_string_t * bson_string_new (const char *str) /* IN */ { bson_string_t *ret; ret = bson_malloc0 (sizeof *ret); ret->len = str ? (int)strlen (str) : 0; ret->alloc = ret->len + 1; if (!bson_is_power_of_two (ret->alloc)) { ret->alloc = (uint32_t)bson_next_power_of_two ((size_t)ret->alloc); } BSON_ASSERT (ret->alloc >= 1); ret->str = bson_malloc (ret->alloc); if (str) { memcpy (ret->str, str, ret->len); } ret->str [ret->len] = '\0'; ret->str [ret->len] = '\0'; return ret; }
char * bson_strdupv_printf (const char *format, /* IN */ va_list args) /* IN */ { va_list my_args; char *buf; int len = 32; int n; BSON_ASSERT (format); buf = bson_malloc0 (len); while (true) { va_copy (my_args, args); n = bson_vsnprintf (buf, len, format, my_args); va_end (my_args); if (n > -1 && n < len) { return buf; } if (n > -1) { len = n + 1; } else { len *= 2; } buf = bson_realloc (buf, len); } }
mongoc_stream_t * debug_stream_new (mongoc_stream_t *stream, debug_stream_stats_t *stats) { mongoc_stream_debug_t *debug_stream; if (!stream) { return NULL; } debug_stream = (mongoc_stream_debug_t *) bson_malloc0 (sizeof *debug_stream); debug_stream->vtable.type = MONGOC_STREAM_DEBUG; debug_stream->vtable.close = _mongoc_stream_debug_close; debug_stream->vtable.destroy = _mongoc_stream_debug_destroy; debug_stream->vtable.failed = _mongoc_stream_debug_failed; debug_stream->vtable.flush = _mongoc_stream_debug_flush; debug_stream->vtable.readv = _mongoc_stream_debug_readv; debug_stream->vtable.writev = _mongoc_stream_debug_writev; debug_stream->vtable.setsockopt = _mongoc_stream_debug_setsockopt; debug_stream->vtable.check_closed = _mongoc_stream_debug_check_closed; debug_stream->vtable.timed_out = _mongoc_stream_debug_timed_out; debug_stream->vtable.get_base_stream = _mongoc_stream_debug_get_base_stream; debug_stream->wrapped = stream; debug_stream->stats = stats; return (mongoc_stream_t *) debug_stream; }
static void test_rtt_calculation_cb (bson_t *test) { mongoc_server_description_t *description; bson_iter_t iter; BSON_ASSERT (test); description = (mongoc_server_description_t *)bson_malloc0(sizeof *description); mongoc_server_description_init(description, "localhost:27017", 1); /* parse RTT into server description */ assert(bson_iter_init_find(&iter, test, "avg_rtt_ms")); description->round_trip_time_msec = bson_iter_int64(&iter); /* update server description with new rtt */ assert(bson_iter_init_find(&iter, test, "new_rtt_ms")); mongoc_server_description_update_rtt(description, bson_iter_int64(&iter)); /* ensure new RTT was calculated correctly */ assert(bson_iter_init_find(&iter, test, "new_avg_rtt")); assert(description->round_trip_time_msec == bson_iter_int64(&iter)); mongoc_server_description_destroy(description); }
/** * _mongoc_gridfs_file_new: * * Create a new empty gridfs file */ mongoc_gridfs_file_t * _mongoc_gridfs_file_new (mongoc_gridfs_t *gridfs, mongoc_gridfs_file_opt_t *opt) { mongoc_gridfs_file_t *file; mongoc_gridfs_file_opt_t default_opt = { 0 }; ENTRY; BSON_ASSERT (gridfs); if (!opt) { opt = &default_opt; } file = (mongoc_gridfs_file_t *)bson_malloc0 (sizeof *file); file->gridfs = gridfs; file->is_dirty = 1; if (opt->chunk_size) { file->chunk_size = opt->chunk_size; } else { /* * The default chunk size is now 255kb. This used to be 256k but has been * reduced to allow for them to fit within power of two sizes in mongod. * * See CDRIVER-322. */ file->chunk_size = (1 << 18) - 1024; } file->files_id.value_type = BSON_TYPE_OID; bson_oid_init (&file->files_id.value.v_oid, NULL); file->upload_date = time (NULL) * 1000; if (opt->md5) { file->md5 = bson_strdup (opt->md5); } if (opt->filename) { file->filename = bson_strdup (opt->filename); } if (opt->content_type) { file->content_type = bson_strdup (opt->content_type); } if (opt->aliases) { bson_copy_to (opt->aliases, &(file->aliases)); } if (opt->metadata) { bson_copy_to (opt->metadata, &(file->metadata)); } RETURN (file); }
static void _clone (mongoc_cursor_impl_t *dst, const mongoc_cursor_impl_t *src) { data_find_opquery_t *data_dst = bson_malloc0 (sizeof (data_find_opquery_t)); data_find_opquery_t *data_src = (data_find_opquery_t *) src->data; _mongoc_cursor_response_legacy_init (&data_dst->response_legacy); bson_copy_to (&data_src->filter, &data_dst->filter); dst->data = data_dst; }
static void multi_upload_setup (perf_test_t *test) { multi_upload_test_t *upload_test; multi_upload_thread_context_t *ctx; mongoc_uri_t *uri; char *data_dir; DIR *dirp; struct dirent *dp; int i; perf_test_setup (test); upload_test = (multi_upload_test_t *) test; uri = mongoc_uri_new (NULL); upload_test->pool = mongoc_client_pool_new (uri); data_dir = bson_strdup_printf ("%s/%s", g_test_dir, test->data_path); dirp = opendir(data_dir); if (!dirp) { perror ("opening data path"); abort (); } i = 0; while ((dp = readdir(dirp)) != NULL) { if (!strcmp (get_ext (dp->d_name), "txt")) { ++i; } } upload_test->cnt = i; upload_test->contexts = (multi_upload_thread_context_t *) bson_malloc0 ( i * sizeof (multi_upload_thread_context_t)); rewinddir (dirp); i = 0; while ((dp = readdir (dirp)) != NULL) { if (!strcmp (get_ext (dp->d_name), "txt")) { ctx = &upload_test->contexts[i]; ctx->filename = bson_strdup (dp->d_name); ctx->path = bson_strdup_printf ( "%s/%s/%s", g_test_dir, test->data_path, dp->d_name); ++i; } } assert (i == upload_test->cnt); closedir (dirp); bson_free (data_dir); mongoc_uri_destroy (uri); }
/** * mongoc_read_concern_new: * * Create a new mongoc_read_concern_t. * * Returns: A newly allocated mongoc_read_concern_t. This should be freed * with mongoc_read_concern_destroy(). */ mongoc_read_concern_t * mongoc_read_concern_new (void) { mongoc_read_concern_t *read_concern; read_concern = (mongoc_read_concern_t *)bson_malloc0 (sizeof *read_concern); return read_concern; }
/** * bson_reader_new_from_fd: * @fd: A file-descriptor to read from. * @close_fd: If the file-descriptor should be closed when done. * * Allocates and initializes a new bson_reader_t that will read BSON documents * into bson_t structures from an underlying file-descriptor. * * If you would like the reader to call close() on @fd in * bson_reader_destroy(), then specify TRUE for close_fd. * * Returns: (transfer full): A newly allocated bson_reader_t that should be * freed with bson_reader_destroy(). */ bson_reader_t * bson_reader_new_from_fd (int fd, bson_bool_t close_fd) { bson_reader_fd_t *real; real = bson_malloc0 (sizeof *real); real->type = BSON_READER_FD; real->data = bson_malloc0 (1024); real->fd = fd; real->len = 1024; real->offset = 0; bson_reader_set_read_func ((bson_reader_t *)real, bson_read); _bson_reader_fd_fill_buffer (real); return (bson_reader_t *)real; }
mongoc_session_opt_t * mongoc_session_opts_new (void) { mongoc_session_opt_t *opts = bson_malloc0 (sizeof (mongoc_session_opt_t)); /* Driver Sessions Spec: causal consistency is true by default */ mongoc_session_opts_set_causal_consistency (opts, true); return opts; }
static perf_test_t * upload_perf_new (void) { upload_test_t *upload_test; upload_test = (upload_test_t *) bson_malloc0 (sizeof (upload_test_t)); upload_init (upload_test); return (perf_test_t *) upload_test; }
static void * mongoc_sasl_mutex_alloc (void) { mongoc_mutex_t *mutex; mutex = (mongoc_mutex_t *)bson_malloc0 (sizeof (mongoc_mutex_t)); mongoc_mutex_init (mutex); return (void *) mutex; }
static perf_test_t * download_perf_new (void) { download_test_t *download_test; download_test = (download_test_t *) bson_malloc0 (sizeof (download_test_t)); download_init (download_test); return (perf_test_t *) download_test; }