Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct handler range checks #235

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions atom/src/behaviors.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,28 @@ enum Mode: uint8_t
Property,
ObjectMethod_Name,
MemberMethod_Object,
Last // sentinel
};

} // namespace GetState

constexpr uint8_t _required_handler_size(uint8_t x) {
return (
(x > 128) ? 255 :
(x > 64) ? 128 :
(x > 32) ? 64 :
(x > 16) ? 32 :
(x > 8) ? 16 :
(x > 4) ? 8 :
(x > 2) ? 4 :
2
);
}

#define _handlers_size( a ) ( sizeof(a) / sizeof(a[0]) )
#define validate_handlers( handlers, sentinel ) \
_handlers_size(handlers) - 1; \
static_assert(sentinel <= _handlers_size(handlers), "Not enough handlers for all enum values"); \
static_assert(_required_handler_size(sentinel) == _handlers_size(handlers), "Handlers size does not match enum width") \

} // namespace atom
9 changes: 5 additions & 4 deletions atom/src/defaultvaluebehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,20 @@ handlers[] = {
call_object_object_name_handler,
object_method_handler,
object_method_name_handler,
member_method_object_handler
member_method_object_handler,
no_op_handler,
no_op_handler
};

const auto mask = validate_handlers(handlers, DefaultValue::Mode::Last);

} // namespace


PyObject*
Member::default_value( CAtom* atom )
{
if( get_default_value_mode() >= sizeof( handlers ) )
return no_op_handler( this, atom ); // LCOV_EXCL_LINE
return handlers[ get_default_value_mode() ]( this, atom );
return handlers[ get_default_value_mode() & mask ]( this, atom );
}


Expand Down
5 changes: 2 additions & 3 deletions atom/src/delattrbehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,15 @@ handlers[] = {
property_handler
};

const auto mask = validate_handlers(handlers, DelAttr::Mode::Last);

} // namespace


int
Member::delattr( CAtom* atom )
{
if( get_delattr_mode() >= sizeof( handlers ) )
return no_op_handler( this, atom ); // LCOV_EXCL_LINE
return handlers[ get_delattr_mode() ]( this, atom );
return handlers[ get_delattr_mode() & mask ]( this, atom );
}


Expand Down
12 changes: 8 additions & 4 deletions atom/src/getattrbehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,18 +256,22 @@ handlers[] = {
call_object_object_name_handler,
object_method_handler,
object_method_name_handler,
member_method_object_handler
member_method_object_handler,
no_op_handler,
no_op_handler,
no_op_handler,
no_op_handler
};

const auto mask = validate_handlers(handlers, GetAttr::Mode::Last);

} // namespace


PyObject*
Member::getattr( CAtom* atom )
{
if( get_getattr_mode() >= sizeof( handlers ) )
return no_op_handler( this, atom ); // LCOV_EXCL_LINE
return handlers[ get_getattr_mode() ]( this, atom );
return handlers[ get_getattr_mode() & mask ]( this, atom );
}

} // namespace atom
10 changes: 6 additions & 4 deletions atom/src/getstatebehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,20 @@ handlers[] = {
include_non_default_handler,
property_handler,
object_method_name_handler,
member_method_object_handler
member_method_object_handler,
include_handler,
include_handler
};

const auto mask = validate_handlers(handlers, GetState::Mode::Last);

} // namespace


PyObject*
Member::should_getstate( CAtom* atom )
{
if( get_getstate_mode() >= sizeof( handlers ) )
return include_handler( this, atom ); // LCOV_EXCL_LINE
return handlers[ get_getstate_mode() ]( this, atom );
return handlers[ get_getstate_mode() & mask ]( this, atom );
}

} // namespace atom
10 changes: 6 additions & 4 deletions atom/src/postgetattrbehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,21 @@ handlers[] = {
delegate_handler,
object_method_value_handler,
object_method_name_value_handler,
member_method_object_value_handler
member_method_object_value_handler,
no_op_handler,
no_op_handler,
no_op_handler,
};

const auto mask = validate_handlers(handlers, PostGetAttr::Mode::Last);

} // namespace


PyObject*
Member::post_getattr( CAtom* atom, PyObject* value )
{
if( get_post_getattr_mode() >= sizeof( handlers ) )
return no_op_handler( this, atom, value ); // LCOV_EXCL_LINE
return handlers[ get_post_getattr_mode() ]( this, atom, value );
return handlers[ get_post_getattr_mode() & mask ]( this, atom, value );
}


Expand Down
10 changes: 6 additions & 4 deletions atom/src/postsetattrbehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,21 @@ handlers[] = {
delegate_handler,
object_method_old_new_handler,
object_method_name_old_new_handler,
member_method_object_old_new_handler
member_method_object_old_new_handler,
no_op_handler,
no_op_handler,
no_op_handler
};

const auto mask = validate_handlers(handlers, PostSetAttr::Mode::Last);

} // namespace


int
Member::post_setattr( CAtom* atom, PyObject* oldvalue, PyObject* newvalue )
{
if( get_post_setattr_mode() >= sizeof( handlers ) )
return no_op_handler( this, atom, oldvalue, newvalue ); // LCOV_EXCL_LINE
return handlers[ get_post_setattr_mode() ]( this, atom, oldvalue, newvalue );
return handlers[ get_post_setattr_mode() & mask ]( this, atom, oldvalue, newvalue );
}


Expand Down
10 changes: 6 additions & 4 deletions atom/src/postvalidatebehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,21 @@ handlers[] = {
delegate_handler,
object_method_old_new_handler,
object_method_name_old_new_handler,
member_method_object_old_new_handler
member_method_object_old_new_handler,
no_op_handler,
no_op_handler,
no_op_handler
};

const auto mask = validate_handlers(handlers, PostValidate::Mode::Last);

} // namespace


PyObject*
Member::post_validate( CAtom* atom, PyObject* oldvalue, PyObject* newvalue )
{
if( get_post_validate_mode() >= sizeof( handlers ) )
return no_op_handler( this, atom, oldvalue, newvalue ); // LCOV_EXCL_LINE
return handlers[ get_post_validate_mode() ]( this, atom, oldvalue, newvalue );
return handlers[ get_post_validate_mode() & mask ]( this, atom, oldvalue, newvalue );
}

} // namespace atom
10 changes: 6 additions & 4 deletions atom/src/setattrbehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,19 +377,21 @@ handlers[] = {
call_object_object_name_value_handler,
object_method_value_handler,
object_method_name_value_handler,
member_method_object_value_handler
member_method_object_value_handler,
no_op_handler,
no_op_handler,
no_op_handler
};

const auto mask = validate_handlers(handlers, SetAttr::Mode::Last);

} // namespace


int
Member::setattr( CAtom* atom, PyObject* value )
{
if( get_setattr_mode() >= sizeof( handlers ) )
return no_op_handler( this, atom, value ); // LCOV_EXCL_LINE
return handlers[ get_setattr_mode() ]( this, atom, value );
return handlers[ get_setattr_mode() & mask ]( this, atom, value );
}


Expand Down
5 changes: 2 additions & 3 deletions atom/src/validatebehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1000,16 +1000,15 @@ handlers[] = {
member_method_object_old_new_handler
};

const auto mask = validate_handlers(handlers, Validate::Mode::Last);

} // namespace


PyObject*
Member::validate( CAtom* atom, PyObject* oldvalue, PyObject* newvalue )
{
if( get_validate_mode() >= sizeof( handlers ) )
return no_op_handler( this, atom, oldvalue, newvalue ); // LCOV_EXCL_LINE
return handlers[ get_validate_mode() ]( this, atom, oldvalue, newvalue );
return handlers[ get_validate_mode() & mask ]( this, atom, oldvalue, newvalue );
}


Expand Down
Loading