Skip to content

Commit 16cf9b1

Browse files
committed
Refactor dba_(p)open() to be more sensible
Actually use ZPP Throw ValueErrors for invalid values Use dedicated struc members for file permission and map size instead of a zval stack
1 parent 8ca7220 commit 16cf9b1

17 files changed

+287
-258
lines changed

ext/dba/dba.c

Lines changed: 145 additions & 119 deletions
Large diffs are not rendered by default.

ext/dba/dba.stub.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,11 @@
22

33
/** @generate-class-entries */
44

5-
/**
6-
* @param string $path
7-
* @param string $mode
8-
* @param string $handler
9-
* @param string $handler_params
10-
* @return resource|false
11-
*/
12-
function dba_popen($path, $mode, $handler = UNKNOWN, ...$handler_params) {}
5+
/** @return resource|false */
6+
function dba_popen(string $path, string $mode, ?string $handler = null, int $permission = 0o644, int $map_size = 0) {}
137

14-
/**
15-
* @param string $path
16-
* @param string $mode
17-
* @param string $handler
18-
* @param string $handler_params
19-
* @return resource|false
20-
*/
21-
function dba_open($path, $mode, $handler = UNKNOWN, ...$handler_params) {}
8+
/** @return resource|false */
9+
function dba_open(string $path, string $mode, ?string $handler = null, int $permission = 0o644, int $map_size = 0) {}
2210

2311
/** @param resource $dba */
2412
function dba_close($dba): void {}

ext/dba/dba_arginfo.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: b5e8100725ae7e93f3ae4a599d88e6578884d0d4 */
2+
* Stub hash: db8b6f51859bc3334abafcc0ee645e614c6f1c59 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_popen, 0, 0, 2)
5-
ZEND_ARG_INFO(0, path)
6-
ZEND_ARG_INFO(0, mode)
7-
ZEND_ARG_INFO(0, handler)
8-
ZEND_ARG_VARIADIC_INFO(0, handler_params)
5+
ZEND_ARG_TYPE_INFO(0, path, IS_STRING, 0)
6+
ZEND_ARG_TYPE_INFO(0, mode, IS_STRING, 0)
7+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, handler, IS_STRING, 1, "null")
8+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, permission, IS_LONG, 0, "00")
9+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, map_size, IS_LONG, 0, "0")
910
ZEND_END_ARG_INFO()
1011

1112
#define arginfo_dba_open arginfo_dba_popen

ext/dba/dba_db1.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ DBA_OPEN_FUNC(db1)
4141
DB *db;
4242

4343
int gmode;
44-
int filemode = 0644;
45-
46-
if (info->argc > 0) {
47-
filemode = zval_get_long(&info->argv[0]);
48-
}
44+
int filemode = info->file_permission;
4945

5046
gmode = 0;
5147
switch (info->mode) {

ext/dba/dba_db2.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ DBA_OPEN_FUNC(db2)
3939
DB *dbp;
4040
DBTYPE type;
4141
int gmode = 0;
42-
int filemode = 0644;
42+
int filemode = info->file_permission;
4343
struct stat check_stat;
4444
int s = VCWD_STAT(info->path, &check_stat);
4545

@@ -61,10 +61,6 @@ DBA_OPEN_FUNC(db2)
6161
return FAILURE;/* not possible */
6262
}
6363

64-
if (info->argc > 0) {
65-
filemode = zval_get_long(&info->argv[0]);
66-
}
67-
6864
if (db_open(info->path, type, gmode, filemode, NULL, NULL, &dbp)) {
6965
return FAILURE;
7066
}

ext/dba/dba_db3.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ DBA_OPEN_FUNC(db3)
5151
DB *dbp = NULL;
5252
DBTYPE type;
5353
int gmode = 0, err;
54-
int filemode = 0644;
54+
int filemode = info->file_permission;
5555
struct stat check_stat;
5656
int s = VCWD_STAT(info->path, &check_stat);
5757

@@ -73,10 +73,6 @@ DBA_OPEN_FUNC(db3)
7373
return FAILURE; /* not possible */
7474
}
7575

76-
if (info->argc > 0) {
77-
filemode = zval_get_long(&info->argv[0]);
78-
}
79-
8076
#ifdef DB_FCNTL_LOCKING
8177
gmode |= DB_FCNTL_LOCKING;
8278
#endif

ext/dba/dba_db4.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ DBA_OPEN_FUNC(db4)
6565
DB *dbp = NULL;
6666
DBTYPE type;
6767
int gmode = 0, err;
68-
int filemode = 0644;
68+
int filemode = info->file_permission;
6969
struct stat check_stat;
7070
int s = VCWD_STAT(info->path, &check_stat);
7171

@@ -106,10 +106,6 @@ DBA_OPEN_FUNC(db4)
106106
gmode |= DB_THREAD;
107107
}
108108

109-
if (info->argc > 0) {
110-
filemode = zval_get_long(&info->argv[0]);
111-
}
112-
113109
if ((err=db_create(&dbp, NULL, 0)) == 0) {
114110
dbp->set_errcall(dbp, php_dba_db4_errcall_fcn);
115111
if (

ext/dba/dba_dbm.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ typedef struct {
5050
DBA_OPEN_FUNC(dbm)
5151
{
5252
int fd;
53-
int filemode = 0644;
54-
55-
if(info->argc > 0) {
56-
filemode = zval_get_long(&info->argv[0]);
57-
}
53+
int filemode = info->file_permission;
5854

5955
if(info->mode == DBA_TRUNC) {
6056
char buf[MAXPATHLEN];

ext/dba/dba_gdbm.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ DBA_OPEN_FUNC(gdbm)
3636
{
3737
GDBM_FILE dbf;
3838
int gmode = 0;
39-
int filemode = 0644;
39+
int filemode = info->file_permission;
4040

4141
gmode = info->mode == DBA_READER ? GDBM_READER :
4242
info->mode == DBA_WRITER ? GDBM_WRITER :
@@ -46,10 +46,6 @@ DBA_OPEN_FUNC(gdbm)
4646
if(gmode == -1)
4747
return FAILURE; /* not possible */
4848

49-
if(info->argc > 0) {
50-
filemode = zval_get_long(&info->argv[0]);
51-
}
52-
5349
dbf = gdbm_open(info->path, 0, gmode, filemode, NULL);
5450

5551
if(dbf) {

ext/dba/dba_lmdb.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,20 @@ DBA_OPEN_FUNC(lmdb)
4040
{
4141
MDB_env *env;
4242
MDB_txn *txn;
43-
int rc, mode = 0644, flags = MDB_NOSUBDIR;
44-
zend_long mapsize = 0;
45-
46-
if(info->argc > 0) {
47-
mode = zval_get_long(&info->argv[0]);
48-
49-
if (info->argc > 1) {
50-
mapsize = zval_get_long(&info->argv[1]);
51-
if (mapsize < 0) {
52-
*error = "mapsize must be greater than or equal to zero";
53-
return FAILURE;
54-
}
55-
}
56-
/* TODO implement handling of the additional flags. */
57-
}
43+
int rc, flags = MDB_NOSUBDIR;
44+
int mode = info->file_permission;
45+
zend_long map_size = info->map_size;
46+
47+
ZEND_ASSERT(map_size > 0);
5848

5949
rc = mdb_env_create(&env);
6050
if (rc) {
6151
*error = mdb_strerror(rc);
6252
return FAILURE;
6353
}
6454

65-
if (mapsize > 0) {
66-
rc = mdb_env_set_mapsize(env, (size_t) mapsize);
55+
if (map_size > 0) {
56+
rc = mdb_env_set_mapsize(env, (size_t) map_size);
6757
if (rc) {
6858
*error = mdb_strerror(rc);
6959
return FAILURE;

ext/dba/dba_ndbm.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ DBA_OPEN_FUNC(ndbm)
3232
{
3333
DBM *dbf;
3434
int gmode = 0;
35-
int filemode = 0644;
35+
int filemode = info->file_permission;
3636
dba_info *pinfo = (dba_info *) info;
3737

3838
switch(info->mode) {
@@ -52,10 +52,6 @@ DBA_OPEN_FUNC(ndbm)
5252
return FAILURE; /* not possible */
5353
}
5454

55-
if(info->argc > 0) {
56-
filemode = zval_get_long(&info->argv[0]);
57-
}
58-
5955
dbf = dbm_open(info->path, gmode, filemode);
6056

6157
pinfo->dbf = dbf;

ext/dba/php_dba.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ typedef struct dba_info {
4343
dba_mode_t mode;
4444
php_stream *fp; /* this is the database stream for builtin handlers */
4545
int fd;
46-
/* arg[cv] are only available when the dba_open handler is called! */
47-
int argc;
48-
zval *argv;
46+
int file_permission;
47+
zend_long map_size;
4948
/* private */
5049
int flags; /* whether and how dba did locking and other flags*/
5150
struct dba_handler *hnd;

ext/dba/tests/bug72157.phpt

Lines changed: 0 additions & 24 deletions
This file was deleted.

ext/dba/tests/dba011.phpt

Lines changed: 113 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,100 @@ die("info $HND handler used");
1212
require_once(__DIR__ .'/test.inc');
1313
echo "database handler: $handler\n";
1414
var_dump(dba_open($db_file, 'n'));
15+
16+
echo '=== Invalid arguments dba_open() ===', \PHP_EOL;
1517
var_dump(dba_open($db_file, 'n', 'bogus'));
16-
var_dump(dba_open($db_file, 'q', $handler));
17-
var_dump(dba_open($db_file, 'nq', $handler));
18-
var_dump(dba_open($db_file, 'n', $handler, 2, 3, 4, 5, 6, 7, 8));
18+
19+
try {
20+
var_dump(dba_open('', 'nq'));
21+
} catch (\ValueError $e) {
22+
echo $e->getMessage(), \PHP_EOL;
23+
}
24+
try {
25+
var_dump(dba_open($db_file, ''));
26+
} catch (\ValueError $e) {
27+
echo $e->getMessage(), \PHP_EOL;
28+
}
29+
try {
30+
var_dump(dba_open($db_file, 'nq', ''));
31+
} catch (\ValueError $e) {
32+
echo $e->getMessage(), \PHP_EOL;
33+
}
34+
35+
36+
try {
37+
var_dump(dba_open($db_file, 'q'));
38+
} catch (\ValueError $e) {
39+
echo $e->getMessage(), \PHP_EOL;
40+
}
41+
try {
42+
var_dump(dba_open($db_file, 'nq'));
43+
} catch (\ValueError $e) {
44+
echo $e->getMessage(), \PHP_EOL;
45+
}
46+
try {
47+
var_dump(dba_open($db_file, 'rdq'));
48+
} catch (\ValueError $e) {
49+
echo $e->getMessage(), \PHP_EOL;
50+
}
51+
try {
52+
var_dump(dba_open($db_file, 'n-t'));
53+
} catch (\ValueError $e) {
54+
echo $e->getMessage(), \PHP_EOL;
55+
}
56+
57+
try {
58+
var_dump(dba_open($db_file, 'r', $handler, 0o644, -10));
59+
} catch (\ValueError $e) {
60+
echo $e->getMessage(), \PHP_EOL;
61+
}
62+
63+
echo '=== Invalid arguments dba_popen() ===', \PHP_EOL;
64+
var_dump(dba_popen($db_file, 'n', 'bogus'));
65+
66+
try {
67+
var_dump(dba_popen('', 'nq'));
68+
} catch (\ValueError $e) {
69+
echo $e->getMessage(), \PHP_EOL;
70+
}
71+
try {
72+
var_dump(dba_popen($db_file, ''));
73+
} catch (\ValueError $e) {
74+
echo $e->getMessage(), \PHP_EOL;
75+
}
76+
try {
77+
var_dump(dba_popen($db_file, 'nq', ''));
78+
} catch (\ValueError $e) {
79+
echo $e->getMessage(), \PHP_EOL;
80+
}
81+
82+
83+
try {
84+
var_dump(dba_popen($db_file, 'q'));
85+
} catch (\ValueError $e) {
86+
echo $e->getMessage(), \PHP_EOL;
87+
}
88+
try {
89+
var_dump(dba_popen($db_file, 'nq'));
90+
} catch (\ValueError $e) {
91+
echo $e->getMessage(), \PHP_EOL;
92+
}
93+
try {
94+
var_dump(dba_popen($db_file, 'rdq'));
95+
} catch (\ValueError $e) {
96+
echo $e->getMessage(), \PHP_EOL;
97+
}
98+
try {
99+
var_dump(dba_popen($db_file, 'n-t'));
100+
} catch (\ValueError $e) {
101+
echo $e->getMessage(), \PHP_EOL;
102+
}
103+
104+
try {
105+
var_dump(dba_popen($db_file, 'r', $handler, 0o644, -10));
106+
} catch (\ValueError $e) {
107+
echo $e->getMessage(), \PHP_EOL;
108+
}
19109
?>
20110
--CLEAN--
21111
<?php
@@ -24,13 +114,27 @@ require(__DIR__ .'/clean.inc');
24114
--EXPECTF--
25115
database handler: flatfile
26116
resource(%d) of type (dba)
117+
=== Invalid arguments dba_open() ===
27118

28-
Warning: dba_open(%stest0.dbm,n): No such handler: bogus in %sdba011.php on line %d
119+
Warning: dba_open(): Handler "bogus" is not available in %sdba011.php on line %d
29120
bool(false)
121+
dba_open(): Argument #1 ($path) cannot be empty
122+
dba_open(): Argument #2 ($mode) cannot be empty
123+
dba_open(): Argument #3 ($handler) cannot be empty
124+
dba_open(): Argument #2 ($mode) first character must be one of "r","w","c", or "n"
125+
dba_open(): Argument #2 ($mode) second character must be one of "d","l","-", or "t"
126+
dba_open(): Argument #2 ($mode) third character must be "t"
127+
dba_open(): Argument #2 ($mode) cannot combine mode "-" (no lock) and "t" (test lock)
128+
dba_open(): Argument #5 ($map_size) must be greater or equal than 0
129+
=== Invalid arguments dba_popen() ===
30130

31-
Warning: dba_open(%stest0.dbm,q): Illegal DBA mode in %sdba011.php on line %d
131+
Warning: dba_popen(): Handler "bogus" is not available in %sdba011.php on line %d
32132
bool(false)
33-
34-
Warning: dba_open(%stest0.dbm,nq): Illegal DBA mode in %sdba011.php on line %d
35-
bool(false)
36-
resource(%d) of type (dba)
133+
dba_popen(): Argument #1 ($path) cannot be empty
134+
dba_popen(): Argument #2 ($mode) cannot be empty
135+
dba_popen(): Argument #3 ($handler) cannot be empty
136+
dba_popen(): Argument #2 ($mode) first character must be one of "r","w","c", or "n"
137+
dba_popen(): Argument #2 ($mode) second character must be one of "d","l","-", or "t"
138+
dba_popen(): Argument #2 ($mode) third character must be "t"
139+
dba_popen(): Argument #2 ($mode) cannot combine mode "-" (no lock) and "t" (test lock)
140+
dba_popen(): Argument #5 ($map_size) must be greater or equal than 0

ext/dba/tests/dba012.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ Warning: ini_set(): No such handler: does_not_exist in %sdba012.php on line %d
4040
resource(%d) of type (dba)
4141
Test 2
4242

43-
Warning: dba_open(%stest0.dbm,n): No default handler selected in %sdba012.php on line %d
43+
Warning: dba_open(): No default handler selected in %sdba012.php on line %d
4444
bool(false)

0 commit comments

Comments
 (0)