Skip to content

Promote warnings to exceptions in ext/shmop #5986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions ext/shmop/shmop.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ PHP_FUNCTION(shmop_open)
}

if (flags_len != 1) {
php_error_docref(NULL, E_WARNING, "%s is not a valid flag", flags);
RETURN_FALSE;
zend_argument_value_error(2, "must be a valid access mode");
RETURN_THROWS();
}

object_init_ex(return_value, shmop_ce);
Expand Down Expand Up @@ -178,35 +178,35 @@ PHP_FUNCTION(shmop_open)
*/
break;
default:
php_error_docref(NULL, E_WARNING, "Invalid access mode");
zend_argument_value_error(2, "must be a valid access mode");
goto err;
}

if (shmop->shmflg & IPC_CREAT && shmop->size < 1) {
php_error_docref(NULL, E_WARNING, "Shared memory segment size must be greater than zero");
zend_argument_value_error(4, "must be greater than 0 for the \"c\" and \"n\" access modes");
goto err;
}

shmop->shmid = shmget(shmop->key, shmop->size, shmop->shmflg);
if (shmop->shmid == -1) {
php_error_docref(NULL, E_WARNING, "Unable to attach or create shared memory segment '%s'", strerror(errno));
php_error_docref(NULL, E_WARNING, "Unable to attach or create shared memory segment \"%s\"", strerror(errno));
goto err;
}

if (shmctl(shmop->shmid, IPC_STAT, &shm)) {
/* please do not add coverage here: the segment would be leaked and impossible to delete via php */
php_error_docref(NULL, E_WARNING, "Unable to get shared memory segment information '%s'", strerror(errno));
php_error_docref(NULL, E_WARNING, "Unable to get shared memory segment information \"%s\"", strerror(errno));
goto err;
}

if (shm.shm_segsz > ZEND_LONG_MAX) {
php_error_docref(NULL, E_WARNING, "shared memory segment too large to attach");
zend_argument_value_error(4, "is too large");
goto err;
}

shmop->addr = shmat(shmop->shmid, 0, shmop->shmatflg);
if (shmop->addr == (char*) -1) {
php_error_docref(NULL, E_WARNING, "Unable to attach to shared memory segment '%s'", strerror(errno));
php_error_docref(NULL, E_WARNING, "Unable to attach to shared memory segment \"%s\"", strerror(errno));
goto err;
}

Expand Down Expand Up @@ -236,13 +236,13 @@ PHP_FUNCTION(shmop_read)
shmop = Z_SHMOP_P(shmid);

if (start < 0 || start > shmop->size) {
php_error_docref(NULL, E_WARNING, "Start is out of range");
RETURN_FALSE;
zend_argument_value_error(2, "must be between 0 and the segment size");
RETURN_THROWS();
}

if (count < 0 || start > (INT_MAX - count) || start + count > shmop->size) {
php_error_docref(NULL, E_WARNING, "Count is out of range");
RETURN_FALSE;
zend_argument_value_error(3, "is out of range");
RETURN_THROWS();
}

startaddr = shmop->addr + start;
Expand Down Expand Up @@ -297,13 +297,13 @@ PHP_FUNCTION(shmop_write)
shmop = Z_SHMOP_P(shmid);

if ((shmop->shmatflg & SHM_RDONLY) == SHM_RDONLY) {
php_error_docref(NULL, E_WARNING, "Trying to write to a read only segment");
RETURN_FALSE;
zend_throw_error(NULL, "Read-only segment cannot be written");
RETURN_THROWS();
}

if (offset < 0 || offset > shmop->size) {
php_error_docref(NULL, E_WARNING, "Offset out of range");
RETURN_FALSE;
zend_argument_value_error(3, "is out of range");
RETURN_THROWS();
}

writesize = ((zend_long)ZSTR_LEN(data) < shmop->size - offset) ? (zend_long)ZSTR_LEN(data) : shmop->size - offset;
Expand Down
4 changes: 2 additions & 2 deletions ext/shmop/shmop.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ final class Shmop {}

function shmop_open(int $key, string $flags, int $mode, int $size): Shmop|false {}

function shmop_read(Shmop $shmid, int $start, int $count): string|false {}
function shmop_read(Shmop $shmid, int $start, int $count): string {}

/** @deprecated */
function shmop_close(Shmop $shmid): void {}

function shmop_size(Shmop $shmid): int {}

function shmop_write(Shmop $shmid, string $data, int $offset): int|false {}
function shmop_write(Shmop $shmid, string $data, int $offset): int {}

function shmop_delete(Shmop $shmid): bool {}
6 changes: 3 additions & 3 deletions ext/shmop/shmop_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: e451ccfbe66fc2b6fc0dae6e7e5710ededaf7b0c */
* Stub hash: 1fe8d001718e20ca915480d1ab6cb6996115b547 */

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_shmop_open, 0, 4, Shmop, MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, key, IS_LONG, 0)
Expand All @@ -8,7 +8,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_shmop_open, 0, 4, Shmop, MAY
ZEND_ARG_TYPE_INFO(0, size, IS_LONG, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_shmop_read, 0, 3, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_shmop_read, 0, 3, IS_STRING, 0)
ZEND_ARG_OBJ_INFO(0, shmid, Shmop, 0)
ZEND_ARG_TYPE_INFO(0, start, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, count, IS_LONG, 0)
Expand All @@ -22,7 +22,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_shmop_size, 0, 1, IS_LONG, 0)
ZEND_ARG_OBJ_INFO(0, shmid, Shmop, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_shmop_write, 0, 3, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_shmop_write, 0, 3, IS_LONG, 0)
ZEND_ARG_OBJ_INFO(0, shmid, Shmop, 0)
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, offset, IS_LONG, 0)
Expand Down
15 changes: 9 additions & 6 deletions ext/shmop/tests/001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ shmop extension test
echo "data in memory is: " . shmop_read($shm_id, 0, $written) . "\n";

/* try to append data to the shared memory segment, this should fail */
shmop_write($shm_id, $write_d1, $written);
try {
shmop_write($shm_id, $write_d1, $written);
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

echo "shm open for read only: ";
$shm_id = shmop_open($hex_shm_id, "w", 0644, 1024);
Expand All @@ -53,7 +57,7 @@ shmop extension test
echo "ok\n";
}

echo "shm write test #1: ";
echo "shm write test #2: ";
$written = shmop_write($shm_id, $write_d2, $written);
if ($written != strlen($write_d2)) {
die("failed\n");
Expand All @@ -70,16 +74,15 @@ shmop extension test
echo "ok\n";
}
?>
--EXPECTF--
--EXPECT--
shm open for create: ok
shm size is: 1024
shm write test #1: ok
data in memory is: test #1 of the shmop() extension
shm open for read only: ok
data in memory is: test #1 of the shmop() extension

Warning: shmop_write(): Trying to write to a read only segment in %s on line %d
Read-only segment cannot be written
shm open for read only: ok
shm write test #1: ok
shm write test #2: ok
data in memory is: test #1 of the shmop() extensiontest #2 append data to shared memory segment
deletion of shm segment: ok
86 changes: 55 additions & 31 deletions ext/shmop/tests/002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,81 @@ edgarsandi - <edgar.r.sandi@gmail.com>
--FILE--
<?php

echo PHP_EOL, '## shmop_open function tests ##';
// warning outputs: invalid flag when the flags length != 1
var_dump(shmop_open(1338, '', 0644, 1024));
echo PHP_EOL, '## shmop_open function tests ##', PHP_EOL;

// warning outputs: invalid access mode
var_dump(shmop_open(1338, 'b', 0644, 1024));
// Invalid flag when the flags length != 1
try {
shmop_open(1338, '', 0644, 1024);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

// warning outputs: unable to attach or create shared memory segment
try {
shmop_open(1338, 'b', 0644, 1024);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

// Warning outputs: Unable to attach or create shared memory segment
var_dump(shmop_open(null, 'a', 0644, 1024));

// warning outputs: Shared memory segment size must be greater than zero
var_dump(shmop_open(1338, "c", 0666, 0));
// Shared memory segment size must be greater than zero
try {
shmop_open(null, 'a', 0644, 1024);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

//Shared memory segment size must be greater than zero
try {
shmop_open(1338, "c", 0666, 0);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

echo PHP_EOL, '## shmop_read function tests ##';
// warning outputs: start is out of range
echo PHP_EOL, '## shmop_read function tests ##', PHP_EOL;
// Start is out of range
$shm_id = shmop_open(1338, 'n', 0600, 1024);
var_dump(shmop_read($shm_id, -10, 0));
try {
shmop_read($shm_id, -10, 0);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
shmop_delete($shm_id);

// warning outputs: count is out of range
// Count is out of range
$shm_id = shmop_open(1339, 'n', 0600, 1024);
var_dump(shmop_read($shm_id, 0, -10));
try {
shmop_read($shm_id, 0, -10);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
shmop_delete($shm_id);

echo PHP_EOL, '## shmop_write function tests ##';
// warning outputs: offset out of range
echo PHP_EOL, '## shmop_write function tests ##', PHP_EOL;
// Offset out of range
$shm_id = shmop_open(1340, 'n', 0600, 1024);
var_dump(shmop_write($shm_id, 'text to try write', -10));
try {
shmop_write($shm_id, 'text to try write', -10);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
shmop_delete($shm_id);
?>
--EXPECTF--
## shmop_open function tests ##
Warning: shmop_open(): is not a valid flag in %s on line %d
bool(false)

Warning: shmop_open(): Invalid access mode in %s on line %d
bool(false)
shmop_open(): Argument #2 ($flags) must be a valid access mode
shmop_open(): Argument #2 ($flags) must be a valid access mode

Warning: shmop_open(): Unable to attach or create shared memory segment '%s' in %s on line %d
Warning: shmop_open(): Unable to attach or create shared memory segment "%s" in %s on line %d
bool(false)

Warning: shmop_open(): Shared memory segment size must be greater than zero in %s on line %d
bool(false)
Warning: shmop_open(): Unable to attach or create shared memory segment "%s" in %s on line %d
shmop_open(): Argument #4 ($size) must be greater than 0 for the "c" and "n" access modes

## shmop_read function tests ##
Warning: shmop_read(): Start is out of range in %s on line %d
bool(false)

Warning: shmop_read(): Count is out of range in %s on line %d
bool(false)
shmop_read(): Argument #2 ($start) must be between 0 and the segment size
shmop_read(): Argument #3 ($count) is out of range

## shmop_write function tests ##
Warning: shmop_write(): Offset out of range in %s on line %d
bool(false)
shmop_write(): Argument #3 ($offset) is out of range