Skip to content

Commit 75766ec

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 cb256f4 commit 75766ec

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
@@ -5479,7 +5479,13 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode
54795479
}
54805480
#endif
54815481

5482-
env->me_path = strdup(path);
5482+
size_t path_len = strlen(path) + 1;
5483+
env->me_path = malloc(path_len);
5484+
if (env->me_path == NULL) {
5485+
rc = ENOMEM;
5486+
goto leave;
5487+
}
5488+
memcpy(env->me_path, path, path_len);
54835489
env->me_dbxs = calloc(env->me_maxdbs, sizeof(MDB_dbx));
54845490
env->me_dbflags = calloc(env->me_maxdbs, sizeof(uint16_t));
54855491
env->me_dbiseqs = calloc(env->me_maxdbs, sizeof(unsigned int));
@@ -10698,9 +10704,12 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
1069810704
return EACCES;
1069910705
}
1070010706

10707+
size_t name_len = strlen(name) + 1;
10708+
namedup = malloc(name_len);
1070110709
/* Done here so we cannot fail after creating a new DB */
10702-
if ((namedup = strdup(name)) == NULL)
10710+
if (namedup == NULL)
1070310711
return ENOMEM;
10712+
memcpy(namedup, name, name_len);
1070410713

1070510714
if (rc) {
1070610715
/* MDB_NOTFOUND and MDB_CREATE: Create new DB */

0 commit comments

Comments
 (0)