# INIT_CLASS_ENTRY and char*

- Thread: https://externals.io/message/23207
- Original archive: https://news-web.php.net/php.internals/23207
- Started by Brandon Fosdick on 2006-05-09T04:58:43+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #23207 — Brandon Fosdick — 2006-05-09T04:58:43+00:00

URL: https://externals.io/message/23207#23207

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?

---

## #23212 — Marcus Boerger — 2006-05-09T06:55:42+00:00

URL: https://externals.io/message/23207#23212 · In reply to: #23207

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

---

## #23219 — Brandon Fosdick — 2006-05-09T19:02:16+00:00

URL: https://externals.io/message/23207#23219 · In reply to: #23212

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.
