Skip to content

Commit cea3a90

Browse files
committed
zlib: use new stream error API
1 parent 993e209 commit cea3a90

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

ext/zlib/tests/zlib_wrapper_level_errors.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ var_dump($size_oob);
3838

3939
?>
4040
--EXPECTF--
41+
Warning: fopen(): zlib "level" context option must be of type int, string given in %s on line %d
4142
int(%d)
4243

43-
Warning: Object of class stdClass could not be converted to int in %s on line %d
44+
Warning: fopen(): zlib "level" context option must be of type int, stdClass given in %s on line %d
4445
int(%d)
4546
int(0)

ext/zlib/zlib_fopen_wrapper.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ struct php_gz_stream_data_t {
3131
static void php_gziop_report_errors(php_stream *stream, size_t count, const char *verb)
3232
{
3333
if (!(stream->flags & PHP_STREAM_FLAG_SUPPRESS_ERRORS)) {
34-
struct php_gz_stream_data_t *self = stream->abstract;
34+
const struct php_gz_stream_data_t *self = stream->abstract;
3535
int error = 0;
3636
gzerror(self->gz_file, &error);
3737
if (error == Z_ERRNO) {
38-
php_error_docref(NULL, E_NOTICE, "%s of %zu bytes failed with errno=%d %s", verb, count, errno, strerror(errno));
38+
php_stream_notice(stream, ReadFailed,
39+
"%s of %zu bytes failed with errno=%d %s",
40+
verb, count, errno, strerror(errno));
3941
}
4042
}
4143
}
@@ -98,12 +100,13 @@ static ssize_t php_gziop_write(php_stream *stream, const char *buf, size_t count
98100

99101
static int php_gziop_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
100102
{
101-
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
103+
const struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
102104

103-
assert(self != NULL);
105+
ZEND_ASSERT(self != NULL);
104106

105107
if (whence == SEEK_END) {
106-
php_error_docref(NULL, E_WARNING, "SEEK_END is not supported");
108+
php_stream_wrapper_warn(NULL, PHP_STREAM_CONTEXT(stream), REPORT_ERRORS,
109+
SeekNotSupported, "SEEK_END is not supported");
107110
return -1;
108111
}
109112

@@ -171,14 +174,12 @@ const php_stream_ops php_stream_gzio_ops = {
171174
php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, const char *mode, int options,
172175
zend_string **opened_path, php_stream_context *context STREAMS_DC)
173176
{
174-
struct php_gz_stream_data_t *self;
175177
php_stream *stream = NULL, *innerstream = NULL;
176178

177179
/* sanity check the stream: it can be either read-only or write-only */
178180
if (strchr(mode, '+')) {
179-
if (options & REPORT_ERRORS) {
180-
php_error_docref(NULL, E_WARNING, "Cannot open a zlib stream for reading and writing at the same time!");
181-
}
181+
php_stream_wrapper_log_warn(wrapper, context, REPORT_ERRORS, ModeNotSupported,
182+
"Cannot open a zlib stream for reading and writing at the same time!");
182183
return NULL;
183184
}
184185

@@ -194,14 +195,23 @@ php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, con
194195
php_socket_t fd;
195196

196197
if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
197-
self = emalloc(sizeof(*self));
198+
struct php_gz_stream_data_t *self = emalloc(sizeof(*self));
198199
self->stream = innerstream;
199200
self->gz_file = gzdopen(dup(fd), mode);
200201

201202
if (self->gz_file) {
202-
zval *zlevel = context ? php_stream_context_get_option(context, "zlib", "level") : NULL;
203-
if (zlevel && (Z_OK != gzsetparams(self->gz_file, zval_get_long(zlevel), Z_DEFAULT_STRATEGY))) {
204-
php_error(E_WARNING, "failed setting compression level");
203+
const zval *zlevel = context ? php_stream_context_get_option(context, "zlib", "level") : NULL;
204+
205+
if (zlevel) {
206+
bool failed = true;
207+
const zend_long level = zval_try_get_long(zlevel, &failed);
208+
if (UNEXPECTED(failed)) {
209+
php_stream_wrapper_log_warn(wrapper, context, REPORT_ERRORS, InvalidParam,
210+
"zlib \"level\" context option must be of type int, %s given", zend_zval_type_name(zlevel));
211+
} else if (Z_OK != gzsetparams(self->gz_file, level, Z_DEFAULT_STRATEGY)) {
212+
php_stream_wrapper_log_warn(wrapper, context, REPORT_ERRORS, Generic,
213+
"failed setting compression level");
214+
}
205215
}
206216

207217
stream = php_stream_alloc_rel(&php_stream_gzio_ops, self, 0, mode);
@@ -214,9 +224,9 @@ php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, con
214224
}
215225

216226
efree(self);
217-
if (options & REPORT_ERRORS) {
218-
php_error_docref(NULL, E_WARNING, "gzopen failed");
219-
}
227+
228+
php_stream_wrapper_log_warn(wrapper, context, options, OpenFailed,
229+
"gzopen failed");
220230
}
221231

222232
php_stream_close(innerstream);

0 commit comments

Comments
 (0)