Skip to content

Commit 1d6e16b

Browse files
author
Bulat Shakirzyanov
committed
[PHP-47] validate null values in collection types
1 parent 0c258a8 commit 1d6e16b

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

ext/src/Cassandra/Collection.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ PHP_METHOD(Collection, add)
137137
collection = (cassandra_collection*) zend_object_store_get_object(getThis() TSRMLS_CC);
138138

139139
for (i = 0; i < argc; i++) {
140+
if (Z_TYPE_P(*args[i]) == IS_NULL) {
141+
efree(args);
142+
zend_throw_exception_ex(cassandra_invalid_argument_exception_ce, 0 TSRMLS_CC,
143+
"Invalid value: null is not supported inside collections");
144+
RETURN_FALSE;
145+
}
146+
140147
if (!php_cassandra_validate_object(*args[i], collection->type TSRMLS_CC)) {
141148
efree(args);
142149
RETURN_FALSE;

ext/src/Cassandra/Map.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ php_cassandra_map_set(cassandra_map* map, zval* zkey, zval* zvalue TSRMLS_DC)
1616
int key_len;
1717
int result = 0;
1818

19+
if (Z_TYPE_P(zkey) == IS_NULL) {
20+
zend_throw_exception_ex(cassandra_invalid_argument_exception_ce, 0 TSRMLS_CC,
21+
"Invalid key: null is not supported inside maps");
22+
return 0;
23+
}
24+
25+
if (Z_TYPE_P(zvalue) == IS_NULL) {
26+
zend_throw_exception_ex(cassandra_invalid_argument_exception_ce, 0 TSRMLS_CC,
27+
"Invalid value: null is not supported inside maps");
28+
return 0;
29+
}
30+
1931
if (!php_cassandra_hash_object(zkey, map->key_type, &key, &key_len TSRMLS_CC)) {
2032
zend_throw_exception_ex(cassandra_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid key");
2133
return 0;

ext/src/Cassandra/Set.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ php_cassandra_set_add(cassandra_set* set, zval* object TSRMLS_DC)
1111
int key_len;
1212
int result = 0;
1313

14+
if (Z_TYPE_P(object) == IS_NULL) {
15+
zend_throw_exception_ex(cassandra_invalid_argument_exception_ce, 0 TSRMLS_CC,
16+
"Invalid value: null is not supported inside sets");
17+
return 0;
18+
}
19+
1420
if (!php_cassandra_hash_object(object, set->type, &key, &key_len TSRMLS_CC))
1521
return 0;
1622

tests/Cassandra/MapTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,14 @@ public function testSupportsOnlyCassandraTypesForValues()
3737
{
3838
new Map(\Cassandra::TYPE_VARINT, 'another custom type');
3939
}
40+
41+
/**
42+
* @expectedException InvalidArgumentException
43+
* @expectedExceptionMessage Invalid value: null is not supported inside maps
44+
*/
45+
public function testSupportsNullValues()
46+
{
47+
$map = new Map(\Cassandra::TYPE_VARCHAR, \Cassandra::TYPE_VARCHAR);
48+
$map->set("test", null);
49+
}
4050
}

0 commit comments

Comments
 (0)