From 79781ad25ded9bef9dc4c64444916c95f37082eb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 26 Jun 2018 15:33:05 +0200 Subject: [PATCH] Fix flag default value Fix cloning to fetch correctly bbcode object Replace member flag value with old one --- bbcode.c | 112 +++++++++++++++---------------------------------------- 1 file changed, 30 insertions(+), 82 deletions(-) diff --git a/bbcode.c b/bbcode.c index 863d84f..22780ae 100644 --- a/bbcode.c +++ b/bbcode.c @@ -17,7 +17,7 @@ typedef struct _bbcode_object { /*zval *tag; zval *smiley; zval *flag;*/ - zend_long flag; + zval flag; zend_object std; } bbcode_object; @@ -76,6 +76,16 @@ enum { * TODO: Set it in an ini ? */ #define BBCODE_DEFAULT_FLAG BBCODE_QUOTE_DOUBLE|BBCODE_QUOTE_SIMPLE|BBCODE_QUOTE_ESCAPE|BBCODE_CORRECT_AUTO|BBCODE_CORRECT_REOPEN|BBCODE_SMILEY_ON|BBCODE_CLOSE_AUTO|BBCODE_REMOVE_EMPTY +/*static inline bbcode_object *bbcode_object_from_zend_object(zend_object *obj) { + return (bbcode_object *)((char*)(obj) - XtOffsetOf(bbcode_object, std)); +}*/ + +static inline bbcode_object* bbcode_fetch(zend_object *obj) { + return (bbcode_object *)((char *)obj - XtOffsetOf(bbcode_object, std)); +} + +#define Z_BBCODE_P(zv) bbcode_fetch(Z_OBJ_P((zv))) + static void bbcode_destroy(bbcode_object *obj TSRMLS_DC); static void bbcode_free(bbcode_object *obj TSRMLS_DC); static zend_object *bbcode_clone(zval *obj TSRMLS_DC); @@ -106,106 +116,42 @@ static void bbcode_free(bbcode_object *obj TSRMLS_DC) { zend_object_std_dtor((zend_object *)obj); } -static zend_object *bbcode_clone(zval *obj TSRMLS_DC) { - - bbcode_object *oldobj = (bbcode_object *)((char *)obj - XtOffsetOf(bbcode_object, std)); - - printf("bbcode_clone=%d\n", sizeof(bbcode_object) + zend_object_properties_size(Z_OBJCE_P(obj))); - - bbcode_object *newobj = ecalloc(1, sizeof(bbcode_object) + zend_object_properties_size(Z_OBJCE_P(obj))); +static zend_object *bbcode_clone(zval *obj) { - zend_object_std_init(&newobj->std, Z_OBJCE_P(obj) TSRMLS_CC); - object_properties_init(&newobj->std, Z_OBJCE_P(obj) TSRMLS_CC); + zend_object *oldobj = Z_OBJ_P(obj); + zend_object *newobj = bbcode_create(oldobj->ce); - //newobj->std.handlers = &bbcode_handlers; + zend_objects_clone_members(newobj, oldobj); - zend_objects_clone_members(&newobj->std, &oldobj->std); + /*Retrieve from zend object old bbcode*/ + bbcode_object *bboldobj = Z_BBCODE_P(obj); + /*Retrieve from zend object new bbcode*/ + bbcode_object *bbnewobj = bbcode_fetch(newobj); -#if 0 - bbcode_object *oldobj = (bbcode_object *)((char *)obj - XtOffsetOf(bbcode_object, std)); - zend_object *newobj_val = bbcode_create(Z_OBJCE_P(obj) TSRMLS_CC); - bbcode_object *newobj = (bbcode_object *)((char *)newobj_val - XtOffsetOf(bbcode_object, std)); + /*Restore object member values*/ + /*TODO: deal with the other one*/ + Z_LVAL(bbnewobj->flag) = Z_LVAL(bboldobj->flag); - zend_objects_clone_members(&newobj->std, &oldobj->std); -#endif - - /*XXX: clone flag member*/ - newobj->flag = oldobj->flag; - - /* TODO: clone the members of bbcode_object - newobj->buffer = oldobj->buffer; - newobj->length = oldobj->length; - - if (oldobj->flag) { - newobj->buffer = emalloc(oldobj->length); - memcpy(newobj->buffer, oldobj->buffer, oldobj->length); - }*/ - - return &newobj->std; + return newobj; } -#if 0 -static zend_object_value bbcode_clone(zval *obj TSRMLS_DC) -{ - bbcode_object *oldobj = zend_object_store_get_object(obj TSRMLS_CC); - zend_object_value newobj_val = array_buffer_create_object(Z_OBJCE_P(obj) TSRMLS_CC); - bbcode_object *newobj = zend_object_store_get_object_by_handle(newobj_val.handle TSRMLS_CC); - - zend_objects_clone_members(&newobj->std, &oldobj->std); - - /* TODO: clone the members of bbcode_object - newobj->buffer = oldobj->buffer; - newobj->length = oldobj->length; - - if (oldobj->flag) { - newobj->buffer = emalloc(oldobj->length); - memcpy(newobj->buffer, oldobj->buffer, oldobj->length); - }*/ - - return newobj_val; -} -#endif - static zend_object *bbcode_create(zend_class_entry *ce TSRMLS_DC) { - printf("bbcode_clone=%d\n", sizeof(bbcode_object) + zend_object_properties_size(ce)); - - bbcode_object *obj = ecalloc(1, sizeof(bbcode_object) + zend_object_properties_size(ce));//emalloc(sizeof(bbcode_object)); - obj->flag = 0; - //memset(obj, 0, sizeof(bbcode_object)); + bbcode_object *obj = (bbcode_object *)ecalloc(1, sizeof(bbcode_object) + zend_object_properties_size(ce));//emalloc(sizeof(bbcode_object)); zend_object_std_init(&obj->std, ce TSRMLS_CC); object_properties_init(&obj->std, ce TSRMLS_CC); - /**/ - memcpy(&bbcode_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); bbcode_handlers.offset = XtOffsetOf(bbcode_object, std); bbcode_handlers.dtor_obj = (zend_object_dtor_obj_t) bbcode_destroy; bbcode_handlers.free_obj = (zend_object_free_obj_t) bbcode_free; bbcode_handlers.clone_obj = (zend_object_clone_obj_t) bbcode_clone; - //bbcode_handlers.offset = XtOffsetOf(bbcode_object, std); - //bbcode_handlers.dtor_obj = (zend_object_dtor_obj_t) bbcode_destroy; - //bbcode_handlers.free_obj = (zend_object_free_obj_t) bbcode_free; - //bbcode_handlers.clone_obj = (zend_object_clone_obj_t) bbcode_clone; - - - /*retval.handle = zend_object_store_put( - obj, - (zend_objects_store_dtor_t) zend_objects_destroy_object, - (zend_objects_free_object_storage_t) bbcode_free, - NULL TSRMLS_CC - ); - retval.handlers = &bbcode_handlers; - - return retval;*/ obj->std.handlers = &bbcode_handlers; - return &obj->std; -} + Z_LVAL(obj->flag) = 0; -static inline bbcode_object* bbcode_fetch(zend_object *obj) { - return (bbcode_object *)((char *)obj - XtOffsetOf(bbcode_object, std)); + return &obj->std; } /* {{{ PHP_METHOD(BBCode::__construct) { @@ -220,7 +166,7 @@ ZEND_END_ARG_INFO() PHP_METHOD(BBCode, __construct) { zend_array *tag; zend_array *smiley; - zend_long flag = BBCODE_DEFAULT_FLAG; + long flag = BBCODE_DEFAULT_FLAG; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|al", &tag, &smiley, &flag) == FAILURE) { return; @@ -229,8 +175,10 @@ PHP_METHOD(BBCode, __construct) { bbcode_object *obj = bbcode_fetch(Z_OBJ_P(getThis())); assert(obj != NULL); + //TODO: set tag and smiley + if (flag) { - obj->flag = (zend_long) flag; + Z_LVAL(obj->flag) = flag; } //obj = zend_object_store_get_object(getThis() TSRMLS_CC); -- 2.41.1