Skip to content

Commit 421c56d

Browse files
authored
Use zend_string for DBA path (#10698)
1 parent 115afee commit 421c56d

13 files changed

+55
-36
lines changed

ext/dba/dba.c

+31-12
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,10 @@ static void dba_close(dba_info *info)
262262
if (info->hnd) {
263263
info->hnd->close(info);
264264
}
265-
if (info->path) {
266-
pefree(info->path, info->flags&DBA_PERSISTENT);
267-
}
265+
ZEND_ASSERT(info->path);
266+
zend_string_release_ex(info->path, info->flags&DBA_PERSISTENT);
267+
info->path = NULL;
268+
268269
if (info->fp && info->fp != info->lock.fp) {
269270
if (info->flags & DBA_PERSISTENT) {
270271
php_stream_pclose(info->fp);
@@ -431,7 +432,7 @@ static void php_dba_update(INTERNAL_FUNCTION_PARAMETERS, int mode)
431432
/* }}} */
432433

433434
/* {{{ php_find_dbm */
434-
static dba_info *php_dba_find(const char* path)
435+
static dba_info *php_dba_find(const zend_string *path)
435436
{
436437
zend_resource *le;
437438
dba_info *info;
@@ -444,7 +445,7 @@ static dba_info *php_dba_find(const char* path)
444445
}
445446
if (le->type == le_db || le->type == le_pdb) {
446447
info = (dba_info *)(le->ptr);
447-
if (!strcmp(info->path, path)) {
448+
if (zend_string_equals(path, info->path)) {
448449
return (dba_info *)(le->ptr);
449450
}
450451
}
@@ -454,6 +455,20 @@ static dba_info *php_dba_find(const char* path)
454455
}
455456
/* }}} */
456457

458+
static zend_always_inline zend_string *php_dba_zend_string_dup_safe(zend_string *s, bool persistent)
459+
{
460+
if (ZSTR_IS_INTERNED(s) && !persistent) {
461+
return s;
462+
} else {
463+
zend_string *duplicated_str = zend_string_init(ZSTR_VAL(s), ZSTR_LEN(s), persistent);
464+
if (persistent) {
465+
GC_MAKE_PERSISTENT_LOCAL(duplicated_str);
466+
}
467+
return duplicated_str;
468+
}
469+
}
470+
471+
457472
#define FREE_PERSISTENT_RESOURCE_KEY() if (persistent_resource_key) {zend_string_release_ex(persistent_resource_key, false);}
458473

459474
/* {{{ php_dba_open */
@@ -467,7 +482,6 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
467482
const char *file_mode;
468483
const char *lock_file_mode = NULL;
469484
int persistent_flag = persistent ? STREAM_OPEN_PERSISTENT : 0;
470-
zend_string *opened_path = NULL;
471485
char *lock_name;
472486
#ifdef PHP_WIN32
473487
bool restarted = 0;
@@ -724,7 +738,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
724738

725739
info = pemalloc(sizeof(dba_info), persistent);
726740
memset(info, 0, sizeof(dba_info));
727-
info->path = pestrdup(ZSTR_VAL(path), persistent);
741+
info->path = php_dba_zend_string_dup_safe(path, persistent);
728742
info->mode = modenr;
729743
info->file_permission = permission;
730744
info->map_size = map_size;
@@ -753,8 +767,9 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
753767
if (is_db_lock) {
754768
lock_name = ZSTR_VAL(path);
755769
} else {
756-
spprintf(&lock_name, 0, "%s.lck", info->path);
770+
spprintf(&lock_name, 0, "%s.lck", ZSTR_VAL(info->path));
757771
if (!strcmp(file_mode, "r")) {
772+
zend_string *opened_path = NULL;
758773
/* when in read only mode try to use existing .lck file first */
759774
/* do not log errors for .lck file while in read only mode on .lck file */
760775
lock_file_mode = "rb";
@@ -769,13 +784,17 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
769784
}
770785
}
771786
if (!info->lock.fp) {
787+
zend_string *opened_path = NULL;
772788
info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, &opened_path);
773789
if (info->lock.fp) {
774790
if (is_db_lock) {
791+
ZEND_ASSERT(opened_path);
775792
/* replace the path info with the real path of the opened file */
776-
pefree(info->path, persistent);
777-
info->path = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
793+
zend_string_release(info->path);
794+
info->path = php_dba_zend_string_dup_safe(opened_path, persistent);
778795
}
796+
}
797+
if (opened_path) {
779798
zend_string_release_ex(opened_path, 0);
780799
}
781800
}
@@ -801,7 +820,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
801820
if (info->lock.fp && is_db_lock) {
802821
info->fp = info->lock.fp; /* use the same stream for locking and database access */
803822
} else {
804-
info->fp = php_stream_open_wrapper(info->path, file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, NULL);
823+
info->fp = php_stream_open_wrapper(ZSTR_VAL(info->path), file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, NULL);
805824
}
806825
if (!info->fp) {
807826
dba_close(info);
@@ -1207,7 +1226,7 @@ PHP_FUNCTION(dba_list)
12071226
}
12081227
if (le->type == le_db || le->type == le_pdb) {
12091228
info = (dba_info *)(le->ptr);
1210-
add_index_string(return_value, i, info->path);
1229+
add_index_str(return_value, i, zend_string_copy(info->path));
12111230
}
12121231
}
12131232
}

ext/dba/dba_cdb.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ DBA_OPEN_FUNC(cdb)
7373
make = 0;
7474
file = info->fp;
7575
#else
76-
file = VCWD_OPEN(info->path, O_RDONLY);
76+
file = VCWD_OPEN(ZSTR_VAL(info->path), O_RDONLY);
7777
if (file < 0) {
7878
*error = "Unable to open file";
7979
return FAILURE;

ext/dba/dba_db1.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ DBA_OPEN_FUNC(db1)
6161
return FAILURE; /* not possible */
6262
}
6363

64-
db = dbopen((char *)info->path, gmode, filemode, DB_HASH, NULL);
64+
db = dbopen((char *)ZSTR_VAL(info->path), gmode, filemode, DB_HASH, NULL);
6565

6666
if (db == NULL) {
6767
return FAILURE;

ext/dba/dba_db2.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ DBA_OPEN_FUNC(db2)
4141
int gmode = 0;
4242
int filemode = info->file_permission;
4343
struct stat check_stat;
44-
int s = VCWD_STAT(info->path, &check_stat);
44+
int s = VCWD_STAT(ZSTR_VAL(info->path), &check_stat);
4545

4646
if (!s && !check_stat.st_size) {
4747
info->mode = DBA_TRUNC; /* force truncate */
@@ -61,7 +61,7 @@ DBA_OPEN_FUNC(db2)
6161
return FAILURE;/* not possible */
6262
}
6363

64-
if (db_open(info->path, type, gmode, filemode, NULL, NULL, &dbp)) {
64+
if (db_open(ZSTR_VAL(info->path), type, gmode, filemode, NULL, NULL, &dbp)) {
6565
return FAILURE;
6666
}
6767

ext/dba/dba_db3.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ DBA_OPEN_FUNC(db3)
5353
int gmode = 0, err;
5454
int filemode = info->file_permission;
5555
struct stat check_stat;
56-
int s = VCWD_STAT(info->path, &check_stat);
56+
int s = VCWD_STAT(ZSTR_VAL(info->path), &check_stat);
5757

5858
if (!s && !check_stat.st_size) {
5959
info->mode = DBA_TRUNC; /* force truncate */
@@ -81,9 +81,9 @@ DBA_OPEN_FUNC(db3)
8181
dbp->set_errcall(dbp, php_dba_db3_errcall_fcn);
8282
if(
8383
#if (DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1))
84-
(err=dbp->open(dbp, 0, info->path, NULL, type, gmode, filemode)) == 0) {
84+
(err=dbp->open(dbp, 0, ZSTR_VAL(info->path), NULL, type, gmode, filemode)) == 0) {
8585
#else
86-
(err=dbp->open(dbp, info->path, NULL, type, gmode, filemode)) == 0) {
86+
(err=dbp->open(dbp, ZSTR_VAL(info->path), NULL, type, gmode, filemode)) == 0) {
8787
#endif
8888
dba_db3_data *data;
8989

ext/dba/dba_db4.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ DBA_OPEN_FUNC(db4)
6767
int gmode = 0, err;
6868
int filemode = info->file_permission;
6969
struct stat check_stat;
70-
int s = VCWD_STAT(info->path, &check_stat);
70+
int s = VCWD_STAT(ZSTR_VAL(info->path), &check_stat);
7171

7272
#if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR <= 7) /* Bug 51086 */
7373
if (!s && !check_stat.st_size) {
@@ -110,9 +110,9 @@ DBA_OPEN_FUNC(db4)
110110
dbp->set_errcall(dbp, php_dba_db4_errcall_fcn);
111111
if (
112112
#if (DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1))
113-
(err=dbp->open(dbp, 0, info->path, NULL, type, gmode, filemode)) == 0) {
113+
(err=dbp->open(dbp, 0, ZSTR_VAL(info->path), NULL, type, gmode, filemode)) == 0) {
114114
#else
115-
(err=dbp->open(dbp, info->path, NULL, type, gmode, filemode)) == 0) {
115+
(err=dbp->open(dbp, ZSTR_VAL(info->path), NULL, type, gmode, filemode)) == 0) {
116116
#endif
117117
dba_db4_data *data;
118118

ext/dba/dba_dbm.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include <fcntl.h>
3737

3838
#define TRUNC_IT(extension, mode) \
39-
snprintf(buf, MAXPATHLEN, "%s" extension, info->path); \
39+
snprintf(buf, MAXPATHLEN, "%s" extension, ZSTR_VAL(info->path)); \
4040
buf[MAXPATHLEN-1] = '\0'; \
4141
if((fd = VCWD_OPEN_MODE(buf, O_CREAT | mode | O_WRONLY, filemode)) == -1) \
4242
return FAILURE; \
@@ -67,7 +67,7 @@ DBA_OPEN_FUNC(dbm)
6767
TRUNC_IT(".dir", 0);
6868
}
6969

70-
if(dbminit((char *) info->path) == -1) {
70+
if(dbminit((char *) ZSTR_VAL(info->path)) == -1) {
7171
return FAILURE;
7272
}
7373

ext/dba/dba_gdbm.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ DBA_OPEN_FUNC(gdbm)
4646
if(gmode == -1)
4747
return FAILURE; /* not possible */
4848

49-
dbf = gdbm_open(info->path, /* int block_size */ 0, gmode, filemode, NULL);
49+
dbf = gdbm_open(ZSTR_VAL(info->path), /* int block_size */ 0, gmode, filemode, NULL);
5050

5151
if(dbf) {
5252
info->dbf = pemalloc(sizeof(dba_gdbm_data), info->flags&DBA_PERSISTENT);

ext/dba/dba_lmdb.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ DBA_OPEN_FUNC(lmdb)
8181
}
8282
}
8383

84-
rc = mdb_env_open(env, info->path, flags, mode);
84+
rc = mdb_env_open(env, ZSTR_VAL(info->path), flags, mode);
8585
if (rc) {
8686
/* If this function [mdb_env_open()] fails, mdb_env_close() must be called to discard the MDB_env handle.
8787
* http://www.lmdb.tech/doc/group__mdb.html#ga32a193c6bf4d7d5c5d579e71f22e9340 */

ext/dba/dba_ndbm.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ DBA_OPEN_FUNC(ndbm)
5252
return FAILURE; /* not possible */
5353
}
5454

55-
dbf = dbm_open(info->path, gmode, filemode);
55+
dbf = dbm_open(ZSTR_VAL(info->path), gmode, filemode);
5656

5757
pinfo->dbf = dbf;
5858
return SUCCESS;

ext/dba/dba_qdbm.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ DBA_OPEN_FUNC(qdbm)
3737

3838
switch(info->mode) {
3939
case DBA_READER:
40-
dbf = dpopen(info->path, DP_OREADER, 0);
40+
dbf = dpopen(ZSTR_VAL(info->path), DP_OREADER, 0);
4141
break;
4242
case DBA_WRITER:
43-
dbf = dpopen(info->path, DP_OWRITER, 0);
43+
dbf = dpopen(ZSTR_VAL(info->path), DP_OWRITER, 0);
4444
break;
4545
case DBA_CREAT:
46-
dbf = dpopen(info->path, DP_OWRITER | DP_OCREAT, 0);
46+
dbf = dpopen(ZSTR_VAL(info->path), DP_OWRITER | DP_OCREAT, 0);
4747
break;
4848
case DBA_TRUNC:
49-
dbf = dpopen(info->path, DP_OWRITER | DP_OCREAT | DP_OTRUNC, 0);
49+
dbf = dpopen(ZSTR_VAL(info->path), DP_OWRITER | DP_OCREAT | DP_OTRUNC, 0);
5050
break;
5151
default:
5252
return FAILURE;

ext/dba/dba_tcadb.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ DBA_OPEN_FUNC(tcadb)
3939
if (tcadb) {
4040
switch(info->mode) {
4141
case DBA_READER:
42-
spprintf(&path_string, 0, "%s#mode=r", info->path);
42+
spprintf(&path_string, 0, "%s#mode=r", ZSTR_VAL(info->path));
4343
break;
4444
case DBA_WRITER:
45-
spprintf(&path_string, 0, "%s#mode=w", info->path);
45+
spprintf(&path_string, 0, "%s#mode=w", ZSTR_VAL(info->path));
4646
break;
4747
case DBA_CREAT:
48-
spprintf(&path_string, 0, "%s#mode=wc", info->path);
48+
spprintf(&path_string, 0, "%s#mode=wc", ZSTR_VAL(info->path));
4949
break;
5050
case DBA_TRUNC:
51-
spprintf(&path_string, 0, "%s#mode=wct", info->path);
51+
spprintf(&path_string, 0, "%s#mode=wct", ZSTR_VAL(info->path));
5252
break;
5353
default:
5454
tcadbdel(tcadb);

ext/dba/php_dba.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ typedef struct dba_lock {
3838
typedef struct dba_info {
3939
/* public */
4040
void *dbf; /* ptr to private data or whatever */
41-
char *path;
41+
zend_string *path;
4242
dba_mode_t mode;
4343
php_stream *fp; /* this is the database stream for builtin handlers */
4444
int fd;

0 commit comments

Comments
 (0)