Skip to content

Commit

Permalink
Add protection code
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Jun 24, 2024
1 parent 8f3e02c commit ba1fae2
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions ext-src/swoole_thread_barrier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ using swoole::Barrier;
static zend_class_entry *swoole_thread_barrier_ce;
static zend_object_handlers swoole_thread_barrier_handlers;


struct BarrierResource : public ThreadResource {
Barrier barrier_;
BarrierResource(int count) : ThreadResource() {
Expand Down Expand Up @@ -104,8 +103,11 @@ void php_swoole_thread_barrier_minit(int module_number) {
zend_declare_property_long(swoole_thread_barrier_ce, ZEND_STRL("id"), 0, ZEND_ACC_PUBLIC);
SW_SET_CLASS_CLONEABLE(swoole_thread_barrier, sw_zend_class_clone_deny);
SW_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_thread_barrier, sw_zend_class_unset_property_deny);
SW_SET_CLASS_CUSTOM_OBJECT(
swoole_thread_barrier, php_swoole_thread_barrier_create_object, php_swoole_thread_barrier_free_object, BarrierObject, std);
SW_SET_CLASS_CUSTOM_OBJECT(swoole_thread_barrier,
php_swoole_thread_barrier_create_object,
php_swoole_thread_barrier_free_object,
BarrierObject,
std);
}

static PHP_METHOD(swoole_thread_barrier, __construct) {
Expand All @@ -120,6 +122,12 @@ static PHP_METHOD(swoole_thread_barrier, __construct) {
Z_PARAM_LONG(count)
ZEND_PARSE_PARAMETERS_END();

if (count < 2) {
zend_throw_exception(
swoole_exception_ce, "The parameter $count must be greater than 1", SW_ERROR_INVALID_PARAMS);
RETURN_FALSE;
}

bo->barrier = new BarrierResource(count);
auto resource_id = php_swoole_thread_resource_insert(bo->barrier);
zend_update_property_long(swoole_thread_barrier_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("id"), resource_id);
Expand Down

0 comments on commit ba1fae2

Please sign in to comment.