Skip to content

Commit

Permalink
Add dedicated IO exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Girgias committed Mar 4, 2021
1 parent f52c323 commit 37c5ff4
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Zend/zend_exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ ZEND_API zend_class_entry *zend_ce_value_error;
ZEND_API zend_class_entry *zend_ce_arithmetic_error;
ZEND_API zend_class_entry *zend_ce_division_by_zero_error;
ZEND_API zend_class_entry *zend_ce_unhandled_match_error;
ZEND_API zend_class_entry *zend_ce_io;

/* Internal pseudo-exception that is not exposed to userland. */
static zend_class_entry zend_ce_unwind_exit;
Expand Down Expand Up @@ -791,6 +792,8 @@ void zend_register_default_exception(void) /* {{{ */
zend_ce_unhandled_match_error->create_object = zend_default_exception_new;

INIT_CLASS_ENTRY(zend_ce_unwind_exit, "UnwindExit", NULL);

zend_ce_io = register_class_IO(zend_ce_throwable);
}
/* }}} */

Expand Down
3 changes: 3 additions & 0 deletions Zend/zend_exceptions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,6 @@ class DivisionByZeroError extends ArithmeticError
class UnhandledMatchError extends Error
{
}

interface IO extends Throwable {}

18 changes: 17 additions & 1 deletion Zend/zend_exceptions_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: f322ba2ed3e636b6e99400edfbff98102b7e3d06 */
* Stub hash: b987c82b0a663e3a8ef20c85362bb77d6612446d */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Throwable_getMessage, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -186,6 +186,11 @@ static const zend_function_entry class_UnhandledMatchError_methods[] = {
ZEND_FE_END
};


static const zend_function_entry class_IO_methods[] = {
ZEND_FE_END
};

static zend_class_entry *register_class_Throwable(zend_class_entry *class_entry_Stringable)
{
zend_class_entry ce, *class_entry;
Expand Down Expand Up @@ -394,3 +399,14 @@ static zend_class_entry *register_class_UnhandledMatchError(zend_class_entry *cl

return class_entry;
}

static zend_class_entry *register_class_IO(zend_class_entry *class_entry_Throwable)
{
zend_class_entry ce, *class_entry;

INIT_CLASS_ENTRY(ce, "IO", class_IO_methods);
class_entry = zend_register_internal_interface(&ce);
zend_class_implements(class_entry, 1, class_entry_Throwable);

return class_entry;
}
18 changes: 16 additions & 2 deletions ext/reflection/tests/ReflectionExtension_getClassNames_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@ $standard = new ReflectionExtension('standard');
var_dump($standard->getClassNames());
?>
--EXPECT--
array(4) {
array(11) {
[0]=>
string(22) "__PHP_Incomplete_Class"
[1]=>
string(14) "AssertionError"
[2]=>
string(15) "php_user_filter"
string(10) "FileSystem"
[3]=>
string(7) "Network"
[4]=>
string(15) "FileSystemError"
[5]=>
string(12) "FileNotFound"
[6]=>
string(12) "NotDirectory"
[7]=>
string(23) "InsufficientPermissions"
[8]=>
string(16) "TemporaryFailure"
[9]=>
string(15) "php_user_filter"
[10]=>
string(9) "Directory"
}
3 changes: 3 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "ext/session/php_session.h"
#include "zend_exceptions.h"
#include "zend_operators.h"
#include "io_exceptions.h"
#include "ext/standard/php_dns.h"
#include "ext/standard/php_uuencode.h"
#include "ext/standard/php_mt_rand.h"
Expand Down Expand Up @@ -346,6 +347,8 @@ PHP_MINIT_FUNCTION(basic) /* {{{ */
register_html_constants(INIT_FUNC_ARGS_PASSTHRU);
register_string_constants(INIT_FUNC_ARGS_PASSTHRU);

BASIC_MINIT_SUBMODULE(io_exceptions)

BASIC_MINIT_SUBMODULE(var)
BASIC_MINIT_SUBMODULE(file)
BASIC_MINIT_SUBMODULE(pack)
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ PHP_NEW_EXTENSION(standard, array.c base64.c basic_functions.c browscap.c crc32.
http_fopen_wrapper.c php_fopen_wrapper.c credits.c css.c \
var_unserializer.c ftok.c sha1.c user_filters.c uuencode.c \
filters.c proc_open.c streamsfuncs.c http.c password.c \
random.c net.c hrtime.c crc32_x86.c,,,
random.c net.c hrtime.c crc32_x86.c io_exceptions.c,,,
-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)

PHP_ADD_MAKEFILE_FRAGMENT
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ EXTENSION("standard", "array.c base64.c basic_functions.c browscap.c \
url_scanner_ex.c ftp_fopen_wrapper.c http_fopen_wrapper.c \
php_fopen_wrapper.c credits.c css.c var_unserializer.c ftok.c sha1.c \
user_filters.c uuencode.c filters.c proc_open.c password.c \
streamsfuncs.c http.c flock_compat.c random.c hrtime.c", false /* never shared */,
streamsfuncs.c http.c flock_compat.c random.c hrtime.c io_exceptions.c", false /* never shared */,
'/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1');
PHP_STANDARD = "yes";
ADD_MAKEFILE_FRAGMENT();
Expand Down
54 changes: 54 additions & 0 deletions ext/standard/io_exceptions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: George Peter Banyard <[email protected]> |
+----------------------------------------------------------------------+
*/

/* {{{ includes */
#include "php.h"
#include "zend_exceptions.h"
#include "io_exceptions.h"
#include "io_exceptions_arginfo.h"

/* Class entry pointers */
PHPAPI zend_class_entry *zend_ce_filesystem;
PHPAPI zend_class_entry *zend_ce_network;
PHPAPI zend_class_entry *zend_ce_filesystem_error;
PHPAPI zend_class_entry *zend_ce_insufficient_permissions;
PHPAPI zend_class_entry *zend_ce_temporary_failure;

PHP_MINIT_FUNCTION(io_exceptions) {
zend_class_entry ce;

/* Register interfaces */
INIT_CLASS_ENTRY(ce, "FileSystem", class_FileSystem_methods);
zend_ce_filesystem = zend_register_internal_interface(&ce);

INIT_CLASS_ENTRY(ce, "Network", class_Network_methods);
zend_ce_network = zend_register_internal_interface(&ce);

/* Register exceptions */
INIT_CLASS_ENTRY(ce, "FileSystemError", class_FileSystemError_methods);
zend_ce_filesystem_error = zend_register_internal_class_ex(&ce, zend_ce_exception);
zend_class_implements(zend_ce_filesystem_error, 1, zend_ce_filesystem);

INIT_CLASS_ENTRY(ce, "InsufficientPermissions", class_InsufficientPermissions_methods);
zend_ce_insufficient_permissions = zend_register_internal_class_ex(&ce, zend_ce_exception);
zend_class_implements(zend_ce_insufficient_permissions, 1, zend_ce_filesystem);

INIT_CLASS_ENTRY(ce, "TemporaryFailure", class_TemporaryFailure_methods);
zend_ce_temporary_failure = zend_register_internal_class_ex(&ce, zend_ce_exception);
zend_class_implements(zend_ce_temporary_failure, 1, zend_ce_network);

return SUCCESS;
}
32 changes: 32 additions & 0 deletions ext/standard/io_exceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: George Peter Banyard <[email protected]> |
+----------------------------------------------------------------------+
*/

#ifndef PHP_IO_EXCEPTION
#define PHP_IO_EXCEPTION

PHP_MINIT_FUNCTION(io_exceptions);

BEGIN_EXTERN_C()

extern PHPAPI zend_class_entry *zend_ce_filesystem;
extern PHPAPI zend_class_entry *zend_ce_network;
extern PHPAPI zend_class_entry *zend_ce_filesystem_error;
extern PHPAPI zend_class_entry *zend_ce_insufficient_permissions;
extern PHPAPI zend_class_entry *zend_ce_temporary_failure;

END_EXTERN_C()

#endif /* PHP_IO_EXCEPTION */
14 changes: 14 additions & 0 deletions ext/standard/io_exceptions.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/** @generate-function-entries */

interface FileSystem extends IO {}

interface Network extends IO {}

/* Should use more specialized ones instead but for PoC will use this instead */
class FileSystemError extends Exception implements FileSystem {}

class InsufficientPermissions extends Exception implements FileSystem {}

class TemporaryFailure extends Exception implements Network {}
29 changes: 29 additions & 0 deletions ext/standard/io_exceptions_arginfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 68880a94d8393c0d367cee9e9c18c8ac6d16d9f2 */




static const zend_function_entry class_FileSystem_methods[] = {
ZEND_FE_END
};


static const zend_function_entry class_Network_methods[] = {
ZEND_FE_END
};


static const zend_function_entry class_FileSystemError_methods[] = {
ZEND_FE_END
};


static const zend_function_entry class_InsufficientPermissions_methods[] = {
ZEND_FE_END
};


static const zend_function_entry class_TemporaryFailure_methods[] = {
ZEND_FE_END
};

0 comments on commit 37c5ff4

Please sign in to comment.