Skip to content

Commit e8ba800

Browse files
committed
Use zend_string for DBA path
1 parent dcc3255 commit e8ba800

13 files changed

+42
-36
lines changed

ext/dba/dba.c

+18-12
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,11 @@ 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+
// Cannot use zend_string_release_ex(info->path, info->flags&DBA_PERSISTENT); as this fails GC assertion?
267+
zend_string_free(info->path);
268+
info->path = NULL;
269+
268270
if (info->fp && info->fp != info->lock.fp) {
269271
if (info->flags & DBA_PERSISTENT) {
270272
php_stream_pclose(info->fp);
@@ -431,7 +433,7 @@ static void php_dba_update(INTERNAL_FUNCTION_PARAMETERS, int mode)
431433
/* }}} */
432434

433435
/* {{{ php_find_dbm */
434-
static dba_info *php_dba_find(const char* path)
436+
static dba_info *php_dba_find(const zend_string *path)
435437
{
436438
zend_resource *le;
437439
dba_info *info;
@@ -444,7 +446,7 @@ static dba_info *php_dba_find(const char* path)
444446
}
445447
if (le->type == le_db || le->type == le_pdb) {
446448
info = (dba_info *)(le->ptr);
447-
if (!strcmp(info->path, path)) {
449+
if (zend_string_equals(path, info->path)) {
448450
return (dba_info *)(le->ptr);
449451
}
450452
}
@@ -467,7 +469,6 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
467469
const char *file_mode;
468470
const char *lock_file_mode = NULL;
469471
int persistent_flag = persistent ? STREAM_OPEN_PERSISTENT : 0;
470-
zend_string *opened_path = NULL;
471472
char *lock_name;
472473
#ifdef PHP_WIN32
473474
bool restarted = 0;
@@ -724,7 +725,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
724725

725726
info = pemalloc(sizeof(dba_info), persistent);
726727
memset(info, 0, sizeof(dba_info));
727-
info->path = pestrdup(ZSTR_VAL(path), persistent);
728+
info->path = zend_string_dup(path, persistent);
728729
info->mode = modenr;
729730
info->file_permission = permission;
730731
info->map_size = map_size;
@@ -753,8 +754,9 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
753754
if (is_db_lock) {
754755
lock_name = ZSTR_VAL(path);
755756
} else {
756-
spprintf(&lock_name, 0, "%s.lck", info->path);
757+
spprintf(&lock_name, 0, "%s.lck", ZSTR_VAL(info->path));
757758
if (!strcmp(file_mode, "r")) {
759+
zend_string *opened_path = NULL;
758760
/* when in read only mode try to use existing .lck file first */
759761
/* do not log errors for .lck file while in read only mode on .lck file */
760762
lock_file_mode = "rb";
@@ -769,13 +771,17 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
769771
}
770772
}
771773
if (!info->lock.fp) {
774+
zend_string *opened_path = NULL;
772775
info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, &opened_path);
773776
if (info->lock.fp) {
774777
if (is_db_lock) {
778+
ZEND_ASSERT(opened_path);
775779
/* 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);
780+
zend_string_release_ex(info->path, persistent);
781+
info->path = zend_string_dup(opened_path, persistent);
778782
}
783+
}
784+
if (opened_path) {
779785
zend_string_release_ex(opened_path, 0);
780786
}
781787
}
@@ -801,7 +807,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
801807
if (info->lock.fp && is_db_lock) {
802808
info->fp = info->lock.fp; /* use the same stream for locking and database access */
803809
} else {
804-
info->fp = php_stream_open_wrapper(info->path, file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, NULL);
810+
info->fp = php_stream_open_wrapper(ZSTR_VAL(info->path), file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, NULL);
805811
}
806812
if (!info->fp) {
807813
dba_close(info);
@@ -1207,7 +1213,7 @@ PHP_FUNCTION(dba_list)
12071213
}
12081214
if (le->type == le_db || le->type == le_pdb) {
12091215
info = (dba_info *)(le->ptr);
1210-
add_index_string(return_value, i, info->path);
1216+
add_index_str(return_value, i, zend_string_copy(info->path));
12111217
}
12121218
}
12131219
}

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)