]> granicus.if.org Git - php/commitdiff
Promote warnings to exceptions in ext/shmop
authorMáté Kocsis <kocsismate@woohoolabs.com>
Thu, 13 Aug 2020 22:32:36 +0000 (00:32 +0200)
committerMáté Kocsis <kocsismate@woohoolabs.com>
Tue, 25 Aug 2020 11:09:23 +0000 (13:09 +0200)
Closes GH-5986

ext/shmop/shmop.c
ext/shmop/shmop.stub.php
ext/shmop/shmop_arginfo.h
ext/shmop/tests/001.phpt
ext/shmop/tests/002.phpt

index 7cd66f830016fd0f0a5f16dba04169aaab63ee44..3d8975e76fae5129a99804a2554bc287775f1989 100644 (file)
@@ -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;
index c19cbc26f0e07bb36dde53c5b7ef57e93cc61963..cddb9c64f489203b3c5a2690697ec6b022824883 100644 (file)
@@ -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 {}
index 494559e4f5b72f301362dd1bcb2ad44d246d16ff..211204720ad0a6ed7cac71b1453afcdc025d1fce 100644 (file)
@@ -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)
index 55e8476b844a2b6a2cd6105a777bbd6b693f38a1..4cac82d2f0c8bc23f1f53f4be516f11f3884af82 100644 (file)
@@ -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
index f1f084fdbf8b7342d813b50d37fda56d46e7d093..6b82fc27c05825cf097e3c3183f34582d17842cb 100644 (file)
@@ -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