INIT_CLASS_ENTRY and char*

php.internals

Brandon Fosdick

20 years ago
I think I found a bug in the INIT_OVERLOADED_CLASS_ENTRY_EX macro. At least, I think its a bug, somebody else might think its a feature. :) If you do something like INIT_CLASS_ENTRY(ce, "MyClass", ...) everything works fine. However, if you have something like void register_class(char* name, ...) { ... INIT_CLASS_ENTRY(ce, name, ...); ... } things don't work so well. When I run this through gdb and break right after the macro, ce.name = "MyClass" as expected. But ce.name_length = 3, which is not quite right. It looks to me like the problem is in the 3rd line of the INIT_OVERLOADED_CLASS_ENTRY_EX macro... --- #define INIT_OVERLOADED_CLASS_ENTRY_EX(class_container, class_name, functions, handle_fcall, handle_propget, handle_propset, handle_propunset, handle_propisset) \ { \ class_container.name = strdup(class_name); \ class_container.name_length = sizeof(class_name) - 1; \ --- ...where sizeof() is used instead of strlen(). When class_name is a variable, sizeof() dutifully returns the size of the variable instead of the string length. Obviously this works, and provides a bit of a speed boost, when using literal strings, but it doesn't work so well for char*'s. Is this desired behavior or a bug?

Marcus Börger

20 years ago
Hello Brandon, that's by design. Look into: ext/spl/spl_functions.c:spl_register_sub_class() regards marcus Tuesday, May 9, 2006, 6:58:43 AM, you wrote:
> I think I found a bug in the INIT_OVERLOADED_CLASS_ENTRY_EX macro. At > least, I think its a bug, somebody else might think its a feature. :)
> If you do something like INIT_CLASS_ENTRY(ce, "MyClass", ...) everything > works fine. However, if you have something like
> void register_class(char* name, ...) > { > ... > INIT_CLASS_ENTRY(ce, name, ...); > ... > }
> things don't work so well. When I run this through gdb and break right > after the macro, ce.name = "MyClass" as expected. But ce.name_length = 3, which is not quite right.
> It looks to me like the problem is in the 3rd line of the > INIT_OVERLOADED_CLASS_ENTRY_EX macro...
> --- > #define INIT_OVERLOADED_CLASS_ENTRY_EX(class_container, class_name, > functions, handle_fcall, handle_propget, handle_propset, handle_propunset, handle_propisset) \ > { > class_container.name = strdup(class_name); > class_container.name_length = sizeof(class_name) - 1; \ > ---
> ...where sizeof() is used instead of strlen(). When class_name is a > variable, sizeof() dutifully returns the size of the variable instead of > the string length. Obviously this works, and provides a bit of a speed > boost, when using literal strings, but it doesn't work so well for char*'s.
> Is this desired behavior or a bug?
Best regards, Marcus

Brandon Fosdick

20 years ago
Marcus Boerger wrote:
> that's by design. Look into: ext/spl/spl_functions.c:spl_register_sub_class()
Thanks, I had already come up with the same workaround, but it took forever to figure out what the problem was. The only errors I got related to not being able to find the class, and ce.name always showed the correct value during debugging. Somehow, I just assumed that name_length was set properly too. Would it be worthwhile to make this "feature" more obvious to the unwitting newbie? Thanks for the help...again.