1

While writing and looking at some PHP extension's source code I noticed that some use a LONG type flag to parse a boolean parameter:

bool new_map_embed;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &new_map_embed) == FAILURE) {
    RETURN_FALSE;
}

Why is that? Why not using the b flag instead?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shlomi Hassid
  • 6,500
  • 3
  • 27
  • 48

2 Answers2

1

Most likely the programmer made a mistake (perhaps they didn't know b existed?), or the parameter is not actually a boolean (accepts other values, perhaps). This isn't common practice.

In PHP 5, IS_BOOL stored its value in the same place as IS_LONG anyway, so you might have been forgiven for thinking you should use l here.

Andrea
  • 19,134
  • 4
  • 43
  • 65
-1

You can use something like:

zend_bool new_map_embed;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &new_map_embed) == FAILURE) {
    RETURN_FALSE;
}

Please look into real example inside the file zend_builtin_functions.c, and then search for zend_bool, you will see how PHP uses it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ritesh
  • 1,809
  • 1
  • 14
  • 16
  • 1
    I know I can use `b` my question is why parsing as long `l` is a common practice – Shlomi Hassid Oct 09 '15 at 19:36
  • @ShlomiHassid Why do you think it is common practice? It isn't. – NikiC Oct 09 '15 at 21:42
  • @Nikic I'm seeing it more and more just wondering why and is this a good practice – Shlomi Hassid Oct 09 '15 at 21:44
  • zend_bool is unsigned char (zend_types.h: typedef unsigned char zend_bool;) and when you use long, you are inadvertently passing the address of 4 consecutive chars. and inside the api this is being treated as consecutive char without any loss of generality. You can verify by changing the value of new_map_embed=0xffffff0 (assuming long=4 bytes) and you will find that only value of lsb changes and rest of value remain uneffected after the call to zend_parse_parameters. And also if php core is not using it, so I assume its not right practice. – Ritesh Oct 09 '15 at 22:41