Skip to content

Commit 15a9c26

Browse files
committed
Eliminate usage of strdup from mdb.c
`strdup` internally uses glibc malloc to allocate the returned char *. This means that if application is using some other allocator, say jemalloc, `free`-ing result of `strdup` is an undefined behavior. References: https://stackoverflow.com/questions/32944390/what-is-the-rationale-for-not-including-strdup-in-the-c-standard jemalloc/jemalloc#365 rust-lang/rust#9925
1 parent 223c560 commit 15a9c26

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

libraries/liblmdb/mdb.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5462,7 +5462,13 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode
54625462
}
54635463
#endif
54645464

5465-
env->me_path = strdup(path);
5465+
size_t path_len = strlen(path) + 1;
5466+
env->me_path = malloc(path_len);
5467+
if (env->me_path == NULL) {
5468+
rc = ENOMEM;
5469+
goto leave;
5470+
}
5471+
memcpy(env->me_path, path, path_len);
54665472
env->me_dbxs = calloc(env->me_maxdbs, sizeof(MDB_dbx));
54675473
env->me_dbflags = calloc(env->me_maxdbs, sizeof(uint16_t));
54685474
env->me_dbiseqs = calloc(env->me_maxdbs, sizeof(unsigned int));
@@ -10677,9 +10683,12 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
1067710683
return EACCES;
1067810684
}
1067910685

10686+
size_t name_len = strlen(name) + 1;
10687+
namedup = malloc(name_len);
1068010688
/* Done here so we cannot fail after creating a new DB */
10681-
if ((namedup = strdup(name)) == NULL)
10689+
if (namedup == NULL)
1068210690
return ENOMEM;
10691+
memcpy(namedup, name, name_len);
1068310692

1068410693
if (rc) {
1068510694
/* MDB_NOTFOUND and MDB_CREATE: Create new DB */

0 commit comments

Comments
 (0)