summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMáté Kocsis <kocsismate@woohoolabs.com>2020-08-14 00:32:36 +0200
committerMáté Kocsis <kocsismate@woohoolabs.com>2020-08-25 13:09:23 +0200
commitd54bc295409663abc6ef940e24c35896e56e3372 (patch)
tree74863499e147f85c673024e6333fa5982a112f8b
parent3df306de94dd94d6520c6f95e96c692c3aa8173a (diff)
downloadphp-git-d54bc295409663abc6ef940e24c35896e56e3372.tar.gz
Promote warnings to exceptions in ext/shmop
Closes GH-5986
-rw-r--r--ext/shmop/shmop.c32
-rw-r--r--ext/shmop/shmop.stub.php4
-rw-r--r--ext/shmop/shmop_arginfo.h6
-rw-r--r--ext/shmop/tests/001.phpt15
-rw-r--r--ext/shmop/tests/002.phpt100
5 files changed, 92 insertions, 65 deletions
diff --git a/ext/shmop/shmop.c b/ext/shmop/shmop.c
index 7cd66f8300..3d8975e76f 100644
--- a/ext/shmop/shmop.c
+++ b/ext/shmop/shmop.c
@@ -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);
@@ -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;
}
@@ -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;
@@ -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;
diff --git a/ext/shmop/shmop.stub.php b/ext/shmop/shmop.stub.php
index c19cbc26f0..cddb9c64f4 100644
--- a/ext/shmop/shmop.stub.php
+++ b/ext/shmop/shmop.stub.php
@@ -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 {}
diff --git a/ext/shmop/shmop_arginfo.h b/ext/shmop/shmop_arginfo.h
index 494559e4f5..211204720a 100644
--- a/ext/shmop/shmop_arginfo.h
+++ b/ext/shmop/shmop_arginfo.h
@@ -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)
@@ -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)
@@ -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)
diff --git a/ext/shmop/tests/001.phpt b/ext/shmop/tests/001.phpt
index 55e8476b84..4cac82d2f0 100644
--- a/ext/shmop/tests/001.phpt
+++ b/ext/shmop/tests/001.phpt
@@ -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);
@@ -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");
@@ -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
diff --git a/ext/shmop/tests/002.phpt b/ext/shmop/tests/002.phpt
index f1f084fdbf..6b82fc27c0 100644
--- a/ext/shmop/tests/002.phpt
+++ b/ext/shmop/tests/002.phpt
@@ -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
- var_dump(shmop_open(null, 'a', 0644, 1024));
+try {
+ shmop_open(1338, 'b', 0644, 1024);
+} catch (ValueError $exception) {
+ echo $exception->getMessage() . "\n";
+}
- // warning outputs: Shared memory segment size must be greater than zero
- var_dump(shmop_open(1338, "c", 0666, 0));
+// Warning outputs: Unable to attach or create shared memory segment
+var_dump(shmop_open(null, 'a', 0644, 1024));
-echo PHP_EOL, '## shmop_read function tests ##';
- // warning outputs: start is out of range
- $shm_id = shmop_open(1338, 'n', 0600, 1024);
- var_dump(shmop_read($shm_id, -10, 0));
- shmop_delete($shm_id);
+// Shared memory segment size must be greater than zero
+try {
+ shmop_open(null, 'a', 0644, 1024);
+} catch (ValueError $exception) {
+ echo $exception->getMessage() . "\n";
+}
- // warning outputs: count is out of range
- $shm_id = shmop_open(1339, 'n', 0600, 1024);
- var_dump(shmop_read($shm_id, 0, -10));
- shmop_delete($shm_id);
+//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_write function tests ##';
- // warning outputs: offset out of range
- $shm_id = shmop_open(1340, 'n', 0600, 1024);
- var_dump(shmop_write($shm_id, 'text to try write', -10));
- shmop_delete($shm_id);
+echo PHP_EOL, '## shmop_read function tests ##', PHP_EOL;
+// Start is out of range
+$shm_id = shmop_open(1338, 'n', 0600, 1024);
+try {
+ shmop_read($shm_id, -10, 0);
+} catch (ValueError $exception) {
+ echo $exception->getMessage() . "\n";
+}
+shmop_delete($shm_id);
+
+// Count is out of range
+$shm_id = shmop_open(1339, 'n', 0600, 1024);
+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 ##', PHP_EOL;
+// Offset out of range
+$shm_id = shmop_open(1340, 'n', 0600, 1024);
+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