Skip to content

Commit d5c9558

Browse files
committed
snprintf: bson_snprintf() terminates always with \0.
1 parent b04fd67 commit d5c9558

File tree

7 files changed

+16
-28
lines changed

7 files changed

+16
-28
lines changed

src/mongoc/mongoc-client.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,6 @@ mongoc_client_command (mongoc_client_t *client,
11271127
}
11281128

11291129
bson_snprintf (ns, sizeof ns, "%s.$cmd", db_name);
1130-
ns[sizeof ns - 1] = '\0';
11311130

11321131
return _mongoc_cursor_new (client, ns, flags, skip, limit, batch_size, true,
11331132
query, fields, read_prefs);

src/mongoc/mongoc-cluster.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
const char *dot = strchr(name, '.'); \
8787
if (!dot || ((dot - name) > (sizeof outstr - 6))) { \
8888
bson_snprintf(outstr, sizeof outstr, "admin.$cmd"); \
89-
outstr[sizeof outstr - 1] = '\0'; \
9089
} else { \
9190
memcpy(outstr, name, dot - name); \
9291
memcpy(outstr + (dot - name), ".$cmd", 6); \
@@ -878,7 +877,6 @@ _mongoc_cluster_run_command (mongoc_cluster_t *cluster,
878877
BSON_ASSERT(command);
879878

880879
bson_snprintf(ns, sizeof ns, "%s.$cmd", db_name);
881-
ns[sizeof ns - 1] = '\0';
882880

883881
rpc.query.msg_len = 0;
884882
rpc.query.request_id = ++cluster->request_id;

src/mongoc/mongoc-collection.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,11 @@ _mongoc_collection_new (mongoc_client_t *client,
8484
mongoc_read_prefs_copy(read_prefs) :
8585
mongoc_read_prefs_new(MONGOC_READ_PRIMARY);
8686

87-
bson_snprintf(col->ns, sizeof col->ns - 1, "%s.%s",
88-
db, collection);
89-
bson_snprintf(col->db, sizeof col->db - 1, "%s", db);
90-
bson_snprintf(col->collection, sizeof col->collection - 1,
91-
"%s", collection);
92-
93-
col->ns[sizeof col->ns-1] = '\0';
94-
col->db[sizeof col->db-1] = '\0';
95-
col->collection[sizeof col->collection-1] = '\0';
87+
bson_snprintf (col->ns, sizeof col->ns - 1, "%s.%s",
88+
db, collection);
89+
bson_snprintf (col->db, sizeof col->db - 1, "%s", db);
90+
bson_snprintf (col->collection, sizeof col->collection - 1,
91+
"%s", collection);
9692

9793
col->collectionlen = (uint32_t)strlen(col->collection);
9894
col->nslen = (uint32_t)strlen(col->ns);
@@ -714,8 +710,7 @@ _mongoc_collection_insert_bulk_raw (mongoc_collection_t *collection,
714710
rpc.insert.documents = documents;
715711
rpc.insert.n_documents = n_documents;
716712

717-
bson_snprintf(ns, sizeof ns, "%s.$cmd", collection->db);
718-
ns[sizeof ns - 1] = '\0';
713+
bson_snprintf (ns, sizeof ns, "%s.$cmd", collection->db);
719714

720715
if (!(hint = _mongoc_client_sendv(collection->client, &rpc, 1, 0,
721716
write_concern, NULL, error))) {

src/mongoc/mongoc-counters.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,12 @@ mongoc_counters_calc_size (void)
126126
static void
127127
mongoc_counters_destroy (void)
128128
{
129-
char name[32];
129+
char name [32];
130130
int pid;
131131

132-
pid = getpid();
133-
bson_snprintf(name, sizeof name, "/mongoc-%u", pid);
134-
name[sizeof name - 1] = '\0';
135-
shm_unlink(name);
132+
pid = getpid ();
133+
bson_snprintf (name, sizeof name, "/mongoc-%u", pid);
134+
shm_unlink (name);
136135
}
137136
#endif
138137

@@ -159,9 +158,8 @@ mongoc_counters_alloc (size_t size)
159158
goto use_malloc;
160159
}
161160

162-
pid = getpid();
163-
bson_snprintf(name, sizeof name, "/mongoc-%u", pid);
164-
name[sizeof name - 1] = '\0';
161+
pid = getpid ();
162+
bson_snprintf (name, sizeof name, "/mongoc-%u", pid);
165163

166164
if (-1 == (fd = shm_open(name, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR))) {
167165
goto use_malloc;

src/mongoc/mongoc-database.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ mongoc_database_has_collection (mongoc_database_t *database,
571571
}
572572

573573
bson_snprintf (ns, sizeof ns, "%s.%s", database->name, name);
574-
ns[sizeof ns - 1] = '\0';
575574

576575
read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY);
577576
collection = mongoc_client_get_collection (database->client,

src/mongoc/mongoc-read-prefs.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ mongoc_read_prefs_add_tag (mongoc_read_prefs_t *read_prefs,
8989

9090
key = bson_count_keys (&read_prefs->tags);
9191
bson_snprintf (str, sizeof str, "%d", key);
92-
str[sizeof str - 1] = '\0';
9392

9493
if (tag) {
9594
bson_append_document (&read_prefs->tags, str, -1, tag);

src/mongoc/mongoc-uri.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ mongoc_uri_append_host (mongoc_uri_t *uri,
7171
link_ = bson_malloc0(sizeof *link_);
7272
bson_strncpy (link_->host, host, sizeof link_->host);
7373
if (strchr (host, ':')) {
74-
bson_snprintf(link_->host_and_port, sizeof link_->host_and_port,
75-
"[%s]:%hu", host, port);
74+
bson_snprintf (link_->host_and_port, sizeof link_->host_and_port,
75+
"[%s]:%hu", host, port);
7676
link_->family = AF_INET6;
7777
} else {
78-
bson_snprintf(link_->host_and_port, sizeof link_->host_and_port,
79-
"%s:%hu", host, port);
78+
bson_snprintf (link_->host_and_port, sizeof link_->host_and_port,
79+
"%s:%hu", host, port);
8080
link_->family = strstr (host, ".sock") ? AF_UNIX : AF_INET;
8181
}
8282
link_->host_and_port[sizeof link_->host_and_port - 1] = '\0';

0 commit comments

Comments
 (0)