Skip to content

Commit c04ebfa

Browse files
committed
Improve shm_attach()
Fix Unknown default value & promote a warning to exception
1 parent 1cf81c0 commit c04ebfa

File tree

4 files changed

+50
-30
lines changed

4 files changed

+50
-30
lines changed

ext/sysvshm/sysvshm.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,20 @@ PHP_FUNCTION(shm_attach)
137137
sysvshm_shm *shm_list_ptr;
138138
char *shm_ptr;
139139
sysvshm_chunk_head *chunk_ptr;
140-
zend_long shm_key, shm_id, shm_size = php_sysvshm.init_mem, shm_flag = 0666;
140+
zend_long shm_key, shm_id, shm_size, shm_flag = 0666;
141+
zend_bool shm_size_is_null = 1;
141142

142-
if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "l|ll", &shm_key, &shm_size, &shm_flag)) {
143+
if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "l|l!l", &shm_key, &shm_size, &shm_size_is_null, &shm_flag)) {
143144
RETURN_THROWS();
144145
}
145146

147+
if (shm_size_is_null) {
148+
shm_size = php_sysvshm.init_mem;
149+
}
150+
146151
if (shm_size < 1) {
147-
php_error_docref(NULL, E_WARNING, "Segment size must be greater than zero");
148-
RETURN_FALSE;
152+
zend_argument_value_error(2, "must be greater than 0");
153+
RETURN_THROWS();
149154
}
150155

151156
/* get the id from a specified key or create new shared memory */

ext/sysvshm/sysvshm.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class SysvSharedMemory
66
{
77
}
88

9-
function shm_attach(int $key, int $memsize = UNKNOWN, int $perm = 0666): SysvSharedMemory|false {}
9+
function shm_attach(int $key, ?int $memsize = null, int $perm = 0666): SysvSharedMemory|false {}
1010

1111
function shm_detach(SysvSharedMemory $shm): bool {}
1212

ext/sysvshm/sysvshm_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_shm_attach, 0, 1, SysvSharedMemory, MAY_BE_FALSE)
44
ZEND_ARG_TYPE_INFO(0, key, IS_LONG, 0)
5-
ZEND_ARG_TYPE_INFO(0, memsize, IS_LONG, 0)
5+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, memsize, IS_LONG, 1, "null")
66
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, perm, IS_LONG, 0, "0666")
77
ZEND_END_ARG_INFO()
88

ext/sysvshm/tests/002.phpt

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,35 @@ if (!function_exists('ftok')){ print 'skip'; }
1010

1111
$key = ftok(__FILE__, 't');
1212

13-
var_dump(shm_attach(-1, 0));
14-
var_dump(shm_attach(0, -1));
15-
var_dump(shm_attach(123, -1));
16-
var_dump(shm_attach($key, -1));
17-
var_dump(shm_attach($key, 0));
13+
try {
14+
shm_attach(-1, 0);
15+
} catch (ValueError $exception) {
16+
echo $exception->getMessage() . "\n";
17+
}
18+
19+
try {
20+
shm_attach(0, -1);
21+
} catch (ValueError $exception) {
22+
echo $exception->getMessage() . "\n";
23+
}
24+
25+
try {
26+
shm_attach(123, -1);
27+
} catch (ValueError $exception) {
28+
echo $exception->getMessage() . "\n";
29+
}
30+
31+
try {
32+
shm_attach($key, -1);
33+
} catch (ValueError $exception) {
34+
echo $exception->getMessage() . "\n";
35+
}
36+
37+
try {
38+
shm_attach($key, 0);
39+
} catch (ValueError $exception) {
40+
echo $exception->getMessage() . "\n";
41+
}
1842

1943
var_dump($s = shm_attach($key, 1024));
2044
shm_remove($s);
@@ -31,28 +55,19 @@ shm_remove($s);
3155
echo "Done\n";
3256
?>
3357
--EXPECTF--
34-
Warning: shm_attach(): Segment size must be greater than zero in %s on line %d
35-
bool(false)
36-
37-
Warning: shm_attach(): Segment size must be greater than zero in %s on line %d
38-
bool(false)
39-
40-
Warning: shm_attach(): Segment size must be greater than zero in %s on line %d
41-
bool(false)
42-
43-
Warning: shm_attach(): Segment size must be greater than zero in %s on line %d
44-
bool(false)
45-
46-
Warning: shm_attach(): Segment size must be greater than zero in %s on line %d
47-
bool(false)
48-
object(SysvSharedMemory)#1 (0) {
58+
shm_attach(): Argument #2 ($memsize) must be greater than 0
59+
shm_attach(): Argument #2 ($memsize) must be greater than 0
60+
shm_attach(): Argument #2 ($memsize) must be greater than 0
61+
shm_attach(): Argument #2 ($memsize) must be greater than 0
62+
shm_attach(): Argument #2 ($memsize) must be greater than 0
63+
object(SysvSharedMemory)#%d (0) {
4964
}
50-
object(SysvSharedMemory)#2 (0) {
65+
object(SysvSharedMemory)#%d (0) {
5166
}
52-
object(SysvSharedMemory)#1 (0) {
67+
object(SysvSharedMemory)#%d (0) {
5368
}
54-
object(SysvSharedMemory)#2 (0) {
69+
object(SysvSharedMemory)#%d (0) {
5570
}
56-
object(SysvSharedMemory)#1 (0) {
71+
object(SysvSharedMemory)#%d (0) {
5772
}
5873
Done

0 commit comments

Comments
 (0)