Skip to content

Commit

Permalink
clean type hinting and raise ValueError
Browse files Browse the repository at this point in the history
  • Loading branch information
remicollet committed Jan 24, 2024
1 parent 205dcaa commit ad06ead
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 57 deletions.
36 changes: 14 additions & 22 deletions src/dio.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ PHP_FUNCTION(dio_dup)
}

if ((f = (php_fd_t *) zend_fetch_resource(Z_RES_P(r_fd), le_fd_name, le_fd)) == NULL) {
RETURN_FALSE;
RETURN_THROWS();
}

dfd = dup(f->fd);
Expand Down Expand Up @@ -195,12 +195,12 @@ PHP_FUNCTION(dio_read)
}

if ((f = (php_fd_t *) zend_fetch_resource(Z_RES_P(r_fd), le_fd_name, le_fd)) == NULL) {
RETURN_FALSE;
RETURN_THROWS();
}

if (bytes <= 0) {
php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0.");
RETURN_FALSE;
zend_argument_value_error(1, "must be greater than 0");
RETURN_THROWS();
}

data = emalloc(bytes + 1);
Expand Down Expand Up @@ -233,12 +233,12 @@ PHP_FUNCTION(dio_write)
}

if (trunc_len < 0 || trunc_len > data_len) {
php_error_docref(NULL, E_WARNING, "length must be greater or equal to zero and less than the length of the specified string.");
RETURN_FALSE;
zend_argument_value_error(3, "must be greater or equal to zero and less than the length of the specified string");
RETURN_THROWS();
}

if ((f = (php_fd_t *) zend_fetch_resource(Z_RES_P(r_fd), le_fd_name, le_fd)) == NULL) {
RETURN_FALSE;
RETURN_THROWS();
}


Expand Down Expand Up @@ -266,7 +266,7 @@ PHP_FUNCTION(dio_truncate)
}

if ((f = (php_fd_t *) zend_fetch_resource(Z_RES_P(r_fd), le_fd_name, le_fd)) == NULL) {
RETURN_FALSE;
RETURN_THROWS();
}

if (ftruncate(f->fd, offset) == -1) {
Expand Down Expand Up @@ -294,7 +294,7 @@ PHP_FUNCTION(dio_stat)
}

if ((f = (php_fd_t *) zend_fetch_resource(Z_RES_P(r_fd), le_fd_name, le_fd)) == NULL) {
RETURN_FALSE;
RETURN_THROWS();
}

if (fstat(f->fd, &s) == -1) {
Expand Down Expand Up @@ -335,7 +335,7 @@ PHP_FUNCTION(dio_seek)
}

if ((f = (php_fd_t *) zend_fetch_resource(Z_RES_P(r_fd), le_fd_name, le_fd)) == NULL) {
RETURN_FALSE;
RETURN_THROWS();
}

RETURN_LONG(zend_lseek(f->fd, offset, whence));
Expand All @@ -358,7 +358,7 @@ PHP_FUNCTION(dio_fcntl)
}

if ((f = (php_fd_t *) zend_fetch_resource(Z_RES_P(r_fd), le_fd_name, le_fd)) == NULL) {
RETURN_FALSE;
RETURN_THROWS();
}

switch (cmd) {
Expand Down Expand Up @@ -457,29 +457,21 @@ PHP_FUNCTION(dio_fcntl)
PHP_FUNCTION(dio_tcsetattr)
{
zval *r_fd;
zval *arg = NULL;
php_fd_t *f;
struct termios newtio;
int Baud_Rate, Data_Bits=8, Stop_Bits=1, Parity=0, Flow_Control=1, Is_Canonical=1;
long BAUD,DATABITS,STOPBITS,PARITYON,PARITY;
HashTable *fh;
zval *element;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &r_fd, &arg) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rh", &r_fd, &fh) == FAILURE) {
RETURN_THROWS();
}

if ((f = (php_fd_t *) zend_fetch_resource(Z_RES_P(r_fd), le_fd_name, le_fd)) == NULL) {
RETURN_FALSE;
}

if (Z_TYPE_P(arg) != IS_ARRAY) {
php_error_docref(NULL, E_WARNING,"tcsetattr, third argument should be an associative array");
return;
RETURN_THROWS();
}

fh = HASH_OF(arg);

if ((element = zend_hash_str_find(fh, "baud", sizeof("baud") - 1)) == NULL) {
Baud_Rate = 9600;
} else {
Expand Down Expand Up @@ -677,7 +669,7 @@ PHP_FUNCTION(dio_close)
}

if ((f = (php_fd_t *) zend_fetch_resource(Z_RES_P(r_fd), le_fd_name, le_fd)) == NULL) {
RETURN_FALSE;
RETURN_THROWS();
}

zend_list_close(Z_RES_P(r_fd));
Expand Down
8 changes: 4 additions & 4 deletions src/dio.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function dio_stat($fd): array|false {}
/**
* @param resource $fd
*/
function dio_seek($fd, int $pos, int $whence=SEEK_SET): int|false {}
function dio_seek($fd, int $pos, int $whence=SEEK_SET): int {}

#ifndef PHP_WIN32
/**
Expand All @@ -51,17 +51,17 @@ function dio_fcntl($fd, int $cmd, $arg=NULL) {}
/**
* @param resource $fd
*/
function dio_read($fd, int $n=1024): int|null|false {}
function dio_read($fd, int $n=1024): ?string {}

/**
* @param resource $fd
*/
function dio_write($fd, string $data, int $len=0): int|false {}
function dio_write($fd, string $data, int $len=0): int {}

/**
* @param resource $fd
*/
function dio_close($fd): null|false {}
function dio_close($fd): void {}

#ifndef PHP_WIN32
/**
Expand Down
10 changes: 5 additions & 5 deletions src/dio_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: c960cbb93c3f8966636813ca2346e35030451bf4 */
* Stub hash: 25b531f56c4e91cb135e005ba7efd4fa94a2e9ef */

ZEND_BEGIN_ARG_INFO_EX(arginfo_dio_open, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
Expand Down Expand Up @@ -28,7 +28,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_dio_stat, 0, 1, MAY_BE_ARRAY|MAY
ZEND_ARG_INFO(0, fd)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_dio_seek, 0, 2, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_dio_seek, 0, 2, IS_LONG, 0)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_TYPE_INFO(0, pos, IS_LONG, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, whence, IS_LONG, 0, "SEEK_SET")
Expand All @@ -42,18 +42,18 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_dio_fcntl, 0, 0, 2)
ZEND_END_ARG_INFO()
#endif

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_dio_read, 0, 1, MAY_BE_LONG|MAY_BE_NULL|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_dio_read, 0, 1, IS_STRING, 1)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, n, IS_LONG, 0, "1024")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_dio_write, 0, 2, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_dio_write, 0, 2, IS_LONG, 0)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, len, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_dio_close, 0, 1, IS_FALSE, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_dio_close, 0, 1, IS_VOID, 0)
ZEND_ARG_INFO(0, fd)
ZEND_END_ARG_INFO()

Expand Down
10 changes: 2 additions & 8 deletions src/dio_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ void dio_init_stream_data(php_dio_stream_data *data) {
/* {{{ dio_assoc_array_get_basic_options
* Retrieves the basic open option values from an associative array
*/
void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data) {
void dio_assoc_array_get_basic_options(HashTable *opthash, php_dio_stream_data *data) {
#if defined(DIO_HAS_FILEPERMS) || defined(DIO_NONBLOCK)
zval *tmpzval;
HashTable *opthash;

opthash = HASH_OF(options);
#endif

#ifdef DIO_HAS_FILEPERMS
Expand Down Expand Up @@ -97,11 +94,8 @@ void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data)
/* {{{ dio_assoc_array_get_serial_options
* Retrieves the serial open option values from an associative array
*/
void dio_assoc_array_get_serial_options(zval *options, php_dio_stream_data *data) {
void dio_assoc_array_get_serial_options(HashTable *opthash, php_dio_stream_data *data) {
zval *tmpzval;
HashTable *opthash;

opthash = HASH_OF(options);

if ((tmpzval = zend_hash_str_find(opthash, "data_rate", sizeof("data_rate") -1)) != NULL) {
data->data_rate = zval_get_long(tmpzval);
Expand Down
19 changes: 4 additions & 15 deletions src/dio_stream_wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ php_stream_wrapper php_dio_raw_stream_wrapper = {
* Opens a raw direct IO stream.
*/
PHP_FUNCTION(dio_raw) {
zval *options = NULL;
HashTable *options = NULL;
php_dio_stream_data *data;
php_stream *stream;

Expand All @@ -198,15 +198,10 @@ PHP_FUNCTION(dio_raw) {
char *mode;
size_t mode_len;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|z", &filename, &filename_len, &mode, &mode_len, &options) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|h!", &filename, &filename_len, &mode, &mode_len, &options) == FAILURE) {
RETURN_THROWS();
}

/* Check the third argument is an array. */
if (options && (Z_TYPE_P(options) != IS_ARRAY)) {
RETURN_FALSE;
}

/* Check we can actually access the file. */
if (php_check_open_basedir(filename) || DIO_SAFE_MODE_CHECK(filename, mode)) {
RETURN_FALSE;
Expand Down Expand Up @@ -349,7 +344,7 @@ php_stream_wrapper php_dio_serial_stream_wrapper = {
* Opens a serial direct IO stream.
*/
PHP_FUNCTION(dio_serial) {
zval *options = NULL;
HashTable *options = NULL;
php_dio_stream_data *data;
php_stream *stream;

Expand All @@ -358,16 +353,10 @@ PHP_FUNCTION(dio_serial) {
char *mode;
size_t mode_len;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|z", &filename, &filename_len, &mode, &mode_len, &options) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|h!", &filename, &filename_len, &mode, &mode_len, &options) == FAILURE) {
RETURN_THROWS();
}

/* Check the third argument is an array. */
if (options && (Z_TYPE_P(options) != IS_ARRAY)) {
php_error_docref(NULL, E_WARNING,"dio_serial, the third argument should be an array of options");
RETURN_FALSE;
}

/* Check we can actually access the file. */
if (php_check_open_basedir(filename) || DIO_SAFE_MODE_CHECK(filename, mode)) {
RETURN_FALSE;
Expand Down
4 changes: 2 additions & 2 deletions src/php_dio_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ php_dio_stream_data * dio_create_stream_data(void);

void dio_init_stream_data(php_dio_stream_data *data);

void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data);
void dio_assoc_array_get_basic_options(HashTable *opthash, php_dio_stream_data *data);

void dio_assoc_array_get_serial_options(zval *options, php_dio_stream_data *data);
void dio_assoc_array_get_serial_options(HashTable *opthash, php_dio_stream_data *data);

void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_stream_data *data);

Expand Down
22 changes: 21 additions & 1 deletion tests/dio_raw_stream_007.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,29 @@ Test dio_raw() call

$f = dio_raw($filename, "r+");
if ($f) {
echo "dio_raw passed";
echo "dio_raw passed\n";
fclose($f);
}
$f = dio_raw($filename, "r+", ['is_blocking' => 1]);
if ($f) {
echo "dio_raw passed\n";
fclose($f);
}
$f = dio_raw($filename, "r+", NULL);
if ($f) {
echo "dio_raw passed\n";
fclose($f);
}
try {
$f = dio_raw($filename, "r+", "foo");
} catch(TypeError $e) {
echo $e->getMessage() . "\n";
}
?>
Done
--EXPECT--
dio_raw passed
dio_raw passed
dio_raw passed
dio_raw(): Argument #3 ($options) must be of type ?array, string given
Done

0 comments on commit ad06ead

Please sign in to comment.