Skip to content

Commit 91dc10c

Browse files
committed
Store SplFileObject open_mode as zend_string
1 parent 9602db3 commit 91dc10c

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

ext/spl/spl_directory.c

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static void spl_filesystem_object_free_storage(zend_object *object) /* {{{ */
130130
break;
131131
case SPL_FS_FILE:
132132
if (intern->u.file.open_mode) {
133-
efree(intern->u.file.open_mode);
133+
zend_string_release(intern->u.file.open_mode);
134134
}
135135
if (intern->orig_path) {
136136
zend_string_release(intern->orig_path);
@@ -304,21 +304,23 @@ static int spl_filesystem_file_open(spl_filesystem_object *intern, int use_inclu
304304
intern->type = SPL_FS_FILE;
305305
php_stat(intern->file_name, FS_IS_DIR, &tmp);
306306
if (Z_TYPE(tmp) == IS_TRUE) {
307+
zend_string_release(intern->u.file.open_mode);
307308
intern->u.file.open_mode = NULL;
308309
intern->file_name = NULL;
309310
zend_throw_exception_ex(spl_ce_LogicException, 0, "Cannot use SplFileObject with directories");
310311
return FAILURE;
311312
}
312313

313314
intern->u.file.context = php_stream_context_from_zval(intern->u.file.zcontext, 0);
314-
intern->u.file.stream = php_stream_open_wrapper_ex(ZSTR_VAL(intern->file_name), intern->u.file.open_mode, (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, intern->u.file.context);
315+
intern->u.file.stream = php_stream_open_wrapper_ex(ZSTR_VAL(intern->file_name), ZSTR_VAL(intern->u.file.open_mode), (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, intern->u.file.context);
315316

316317
if (!ZSTR_LEN(intern->file_name) || !intern->u.file.stream) {
317318
if (!EG(exception)) {
318319
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot open file '%s'", ZSTR_VAL(intern->file_name));
319320
}
320-
intern->file_name = NULL; /* until here it is not a copy */
321+
zend_string_release(intern->u.file.open_mode);
321322
intern->u.file.open_mode = NULL;
323+
intern->file_name = NULL; /* until here it is not a copy */
322324
return FAILURE;
323325
}
324326

@@ -337,8 +339,6 @@ static int spl_filesystem_file_open(spl_filesystem_object *intern, int use_inclu
337339

338340
intern->orig_path = zend_string_init(intern->u.file.stream->orig_path, strlen(intern->u.file.stream->orig_path), 0);
339341

340-
intern->u.file.open_mode = estrndup(intern->u.file.open_mode, intern->u.file.open_mode_len);
341-
342342
/* avoid reference counting in debug mode, thus do it manually */
343343
ZVAL_RES(&intern->u.file.zresource, intern->u.file.stream->res);
344344
/*!!! TODO: maybe bug?
@@ -519,12 +519,11 @@ static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, sp
519519
{
520520
ce = ce ? ce : source->file_class;
521521

522-
char *open_mode = "r";
523-
size_t open_mode_len = 1;
522+
zend_string *open_mode = ZSTR_CHAR('r');
524523
zval *resource = NULL;
525524

526-
if (zend_parse_parameters(num_args, "|sbr!",
527-
&open_mode, &open_mode_len, &use_include_path, &resource) == FAILURE
525+
if (zend_parse_parameters(num_args, "|Sbr!",
526+
&open_mode, &use_include_path, &resource) == FAILURE
528527
) {
529528
return NULL;
530529
}
@@ -538,7 +537,7 @@ static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, sp
538537

539538
if (ce->constructor->common.scope != spl_ce_SplFileObject) {
540539
ZVAL_STR_COPY(&arg1, source->file_name);
541-
ZVAL_STRINGL(&arg2, open_mode, open_mode_len);
540+
ZVAL_STR_COPY(&arg2, open_mode);
542541
zend_call_method_with_2_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1, &arg2);
543542
zval_ptr_dtor(&arg1);
544543
zval_ptr_dtor(&arg2);
@@ -554,8 +553,7 @@ static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, sp
554553
intern->path = zend_string_init(path, path_len, 0);
555554
}
556555

557-
intern->u.file.open_mode = open_mode;
558-
intern->u.file.open_mode_len = open_mode_len;
556+
intern->u.file.open_mode = zend_string_copy(open_mode);
559557
intern->u.file.zcontext = resource;
560558

561559
zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
@@ -658,7 +656,7 @@ static inline HashTable *spl_filesystem_object_get_debug_info(zend_object *objec
658656
}
659657
if (intern->type == SPL_FS_FILE) {
660658
pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "openMode", sizeof("openMode")-1);
661-
ZVAL_STRINGL(&tmp, intern->u.file.open_mode, intern->u.file.open_mode_len);
659+
ZVAL_STR_COPY(&tmp, intern->u.file.open_mode);
662660
zend_symtable_update(rv, pnstr, &tmp);
663661
zend_string_release_ex(pnstr, 0);
664662
stmp[1] = '\0';
@@ -2048,29 +2046,24 @@ static void spl_filesystem_file_rewind(zval * this_ptr, spl_filesystem_object *i
20482046
PHP_METHOD(SplFileObject, __construct)
20492047
{
20502048
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
2049+
zend_string *open_mode = ZSTR_CHAR('r');
20512050
bool use_include_path = 0;
20522051
size_t path_len;
20532052
zend_error_handling error_handling;
20542053

2055-
intern->u.file.open_mode = NULL;
2056-
intern->u.file.open_mode_len = 0;
2054+
intern->u.file.open_mode = ZSTR_CHAR('r');
20572055

2058-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|sbr!",
2059-
&intern->file_name,
2060-
&intern->u.file.open_mode, &intern->u.file.open_mode_len,
2056+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|Sbr!",
2057+
&intern->file_name, &open_mode,
20612058
&use_include_path, &intern->u.file.zcontext) == FAILURE) {
20622059
intern->u.file.open_mode = NULL;
20632060
intern->file_name = NULL;
20642061
RETURN_THROWS();
20652062
}
20662063

2067-
if (intern->u.file.open_mode == NULL) {
2068-
intern->u.file.open_mode = "r";
2069-
intern->u.file.open_mode_len = 1;
2070-
}
2071-
20722064
zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
20732065

2066+
intern->u.file.open_mode = zend_string_copy(open_mode);
20742067
if (spl_filesystem_file_open(intern, use_include_path, 0) == SUCCESS) {
20752068
path_len = strlen(intern->u.file.stream->orig_path);
20762069

@@ -2113,8 +2106,7 @@ PHP_METHOD(SplTempFileObject, __construct)
21132106
file_name = zend_string_init("php://temp", sizeof("php://temp")-1, 0);
21142107
}
21152108
intern->file_name = file_name;
2116-
intern->u.file.open_mode = "wb";
2117-
intern->u.file.open_mode_len = sizeof("wb")-1;
2109+
intern->u.file.open_mode = zend_string_init("wb", sizeof("wb")-1, 0);
21182110

21192111
zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
21202112
if (spl_filesystem_file_open(intern, 0, 0) == SUCCESS) {

ext/spl/spl_directory.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ struct _spl_filesystem_object {
8080
php_stream *stream;
8181
php_stream_context *context;
8282
zval *zcontext;
83-
char *open_mode;
84-
size_t open_mode_len;
83+
zend_string *open_mode;
8584
zval current_zval;
8685
char *current_line;
8786
size_t current_line_len;

0 commit comments

Comments
 (0)