Skip to content

Commit aba672a

Browse files
committed
Promote warnings to exceptions in ext/shmop
1 parent 9cb5221 commit aba672a

File tree

5 files changed

+85
-58
lines changed

5 files changed

+85
-58
lines changed

ext/shmop/shmop.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ PHP_FUNCTION(shmop_open)
149149
}
150150

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

156156
object_init_ex(return_value, shmop_ce);
@@ -178,35 +178,35 @@ PHP_FUNCTION(shmop_open)
178178
*/
179179
break;
180180
default:
181-
php_error_docref(NULL, E_WARNING, "Invalid access mode");
181+
zend_argument_value_error(2, "must be a valid access mode");
182182
goto err;
183183
}
184184

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

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

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

202202
if (shm.shm_segsz > ZEND_LONG_MAX) {
203-
php_error_docref(NULL, E_WARNING, "shared memory segment too large to attach");
203+
php_error_docref(NULL, E_WARNING, "Shared memory segment too large to attach");
204204
goto err;
205205
}
206206

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

@@ -236,13 +236,13 @@ PHP_FUNCTION(shmop_read)
236236
shmop = Z_SHMOP_P(shmid);
237237

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

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

248248
startaddr = shmop->addr + start;
@@ -297,13 +297,13 @@ PHP_FUNCTION(shmop_write)
297297
shmop = Z_SHMOP_P(shmid);
298298

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

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

309309
writesize = ((zend_long)ZSTR_LEN(data) < shmop->size - offset) ? (zend_long)ZSTR_LEN(data) : shmop->size - offset;

ext/shmop/shmop.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ final class Shmop {}
66

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

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

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

1414
function shmop_size(Shmop $shmid): int {}
1515

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

1818
function shmop_delete(Shmop $shmid): bool {}

ext/shmop/shmop_arginfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: e451ccfbe66fc2b6fc0dae6e7e5710ededaf7b0c */
2+
* Stub hash: 1fe8d001718e20ca915480d1ab6cb6996115b547 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_shmop_open, 0, 4, Shmop, MAY_BE_FALSE)
55
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
88
ZEND_ARG_TYPE_INFO(0, size, IS_LONG, 0)
99
ZEND_END_ARG_INFO()
1010

11-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_shmop_read, 0, 3, MAY_BE_STRING|MAY_BE_FALSE)
11+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_shmop_read, 0, 3, IS_STRING, 0)
1212
ZEND_ARG_OBJ_INFO(0, shmid, Shmop, 0)
1313
ZEND_ARG_TYPE_INFO(0, start, IS_LONG, 0)
1414
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)
2222
ZEND_ARG_OBJ_INFO(0, shmid, Shmop, 0)
2323
ZEND_END_ARG_INFO()
2424

25-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_shmop_write, 0, 3, MAY_BE_LONG|MAY_BE_FALSE)
25+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_shmop_write, 0, 3, IS_LONG, 0)
2626
ZEND_ARG_OBJ_INFO(0, shmid, Shmop, 0)
2727
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
2828
ZEND_ARG_TYPE_INFO(0, offset, IS_LONG, 0)

ext/shmop/tests/001.phpt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ shmop extension test
4343
echo "data in memory is: " . shmop_read($shm_id, 0, $written) . "\n";
4444

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

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

56-
echo "shm write test #1: ";
60+
echo "shm write test #2:\n";
5761
$written = shmop_write($shm_id, $write_d2, $written);
5862
if ($written != strlen($write_d2)) {
5963
die("failed\n");
@@ -70,16 +74,15 @@ shmop extension test
7074
echo "ok\n";
7175
}
7276
?>
73-
--EXPECTF--
77+
--EXPECT--
7478
shm open for create: ok
7579
shm size is: 1024
7680
shm write test #1: ok
7781
data in memory is: test #1 of the shmop() extension
7882
shm open for read only: ok
7983
data in memory is: test #1 of the shmop() extension
80-
81-
Warning: shmop_write(): Trying to write to a read only segment in %s on line %d
84+
Read-only segment cannot be written
8285
shm open for read only: ok
83-
shm write test #1: ok
86+
shm write test #2: ok
8487
data in memory is: test #1 of the shmop() extensiontest #2 append data to shared memory segment
8588
deletion of shm segment: ok

ext/shmop/tests/002.phpt

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,81 @@ edgarsandi - <edgar.r.sandi@gmail.com>
1111
--FILE--
1212
<?php
1313

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

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

21-
// warning outputs: unable to attach or create shared memory segment
23+
try {
24+
shmop_open(1338, 'b', 0644, 1024);
25+
} catch (ValueError $exception) {
26+
echo $exception->getMessage() . "\n";
27+
}
28+
29+
// Warning outputs: Unable to attach or create shared memory segment
2230
var_dump(shmop_open(null, 'a', 0644, 1024));
2331

24-
// warning outputs: Shared memory segment size must be greater than zero
25-
var_dump(shmop_open(1338, "c", 0666, 0));
32+
// Shared memory segment size must be greater than zero
33+
try {
34+
shmop_open(null, 'a', 0644, 1024);
35+
} catch (ValueError $exception) {
36+
echo $exception->getMessage() . "\n";
37+
}
38+
39+
//Shared memory segment size must be greater than zero
40+
try {
41+
shmop_open(1338, "c", 0666, 0);
42+
} catch (ValueError $exception) {
43+
echo $exception->getMessage() . "\n";
44+
}
2645

27-
echo PHP_EOL, '## shmop_read function tests ##';
28-
// warning outputs: start is out of range
46+
echo PHP_EOL, '## shmop_read function tests ##', PHP_EOL;
47+
// Start is out of range
2948
$shm_id = shmop_open(1338, 'n', 0600, 1024);
30-
var_dump(shmop_read($shm_id, -10, 0));
49+
try {
50+
shmop_read($shm_id, -10, 0);
51+
} catch (ValueError $exception) {
52+
echo $exception->getMessage() . "\n";
53+
}
3154
shmop_delete($shm_id);
3255

33-
// warning outputs: count is out of range
56+
// Count is out of range
3457
$shm_id = shmop_open(1339, 'n', 0600, 1024);
35-
var_dump(shmop_read($shm_id, 0, -10));
58+
try {
59+
shmop_read($shm_id, 0, -10);
60+
} catch (ValueError $exception) {
61+
echo $exception->getMessage() . "\n";
62+
}
3663
shmop_delete($shm_id);
3764

38-
echo PHP_EOL, '## shmop_write function tests ##';
39-
// warning outputs: offset out of range
65+
echo PHP_EOL, '## shmop_write function tests ##', PHP_EOL;
66+
// Offset out of range
4067
$shm_id = shmop_open(1340, 'n', 0600, 1024);
41-
var_dump(shmop_write($shm_id, 'text to try write', -10));
68+
try {
69+
shmop_write($shm_id, 'text to try write', -10);
70+
} catch (ValueError $exception) {
71+
echo $exception->getMessage() . "\n";
72+
}
4273
shmop_delete($shm_id);
4374
?>
4475
--EXPECTF--
4576
## shmop_open function tests ##
46-
Warning: shmop_open(): is not a valid flag in %s on line %d
47-
bool(false)
48-
49-
Warning: shmop_open(): Invalid access mode in %s on line %d
50-
bool(false)
77+
shmop_open(): Argument #2 ($flags) must be a valid access mode
78+
shmop_open(): Argument #2 ($flags) must be a valid access mode
5179

52-
Warning: shmop_open(): Unable to attach or create shared memory segment '%s' in %s on line %d
80+
Warning: shmop_open(): Unable to attach or create shared memory segment "Invalid argument" in %s on line %d
5381
bool(false)
5482

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

5886
## shmop_read function tests ##
59-
Warning: shmop_read(): Start is out of range in %s on line %d
60-
bool(false)
61-
62-
Warning: shmop_read(): Count is out of range in %s on line %d
63-
bool(false)
87+
shmop_read(): Argument #2 ($start) must be between 0 and the segment size
88+
shmop_read(): Argument #3 ($count) is out of range
6489

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

0 commit comments

Comments
 (0)