@- Fixed bug #22386 (Using browscap causes segfault). This fix

@  also adds an optional bool argument to get_browser() that
@  makes the return value an array instead of an object. (jay)
This commit is contained in:
Jay Smith 2003-03-14 17:54:38 +00:00
parent 4a244a3889
commit 830b14297f

View File

@ -34,11 +34,11 @@ static zval *current_section;
static void browscap_entry_dtor(zval *pvalue)
{
if (Z_TYPE_P(pvalue) == IS_OBJECT) {
if (Z_TYPE_P(pvalue) == IS_ARRAY) {
TSRMLS_FETCH();
zend_hash_destroy(Z_OBJPROP_P(pvalue));
free(Z_OBJPROP_P(pvalue));
zend_hash_destroy(Z_ARRVAL_P(pvalue));
free(Z_ARRVAL_P(pvalue));
}
}
@ -61,8 +61,9 @@ static void convert_browscap_pattern(zval *pattern)
}
t = (char *) malloc(Z_STRLEN_P(pattern)*2 + 1);
for (i=0, j=0; i<Z_STRLEN_P(pattern); i++, j++) {
t[0] = '^';
for (i=0, j=1; i<Z_STRLEN_P(pattern); i++, j++) {
switch (Z_STRVAL_P(pattern)[i]) {
case '?':
t[j] = '.';
@ -114,32 +115,41 @@ static void php_browscap_parser_cb(zval *arg1, zval *arg2, int callback_type, vo
new_key = zend_strndup(Z_STRVAL_P(arg1), Z_STRLEN_P(arg1));
zend_str_tolower(new_key, Z_STRLEN_P(arg1));
zend_hash_update(Z_OBJPROP_P(current_section), new_key, Z_STRLEN_P(arg1)+1, &new_property, sizeof(zval *), NULL);
zend_hash_update(Z_ARRVAL_P(current_section), new_key, Z_STRLEN_P(arg1)+1, &new_property, sizeof(zval *), NULL);
free(new_key);
}
break;
case ZEND_INI_PARSER_SECTION: {
zval *processed;
zval *unprocessed;
HashTable *section_properties;
TSRMLS_FETCH();
/*printf("'%s' (%d)\n",$1.value.str.val,$1.value.str.len+1);*/
current_section = (zval *) malloc(sizeof(zval));
INIT_PZVAL(current_section);
array_init(current_section);
processed = (zval *) malloc(sizeof(zval));
INIT_PZVAL(processed);
unprocessed = (zval *) malloc(sizeof(zval));
INIT_PZVAL(unprocessed);
section_properties = (HashTable *) malloc(sizeof(HashTable));
_object_and_properties_init(current_section, ZEND_STANDARD_CLASS_DEF_PTR, section_properties ZEND_FILE_LINE_CC TSRMLS_CC);
zend_hash_init(section_properties, 0, NULL, (dtor_func_t) browscap_entry_dtor, 1);
zend_hash_update(&browser_hash, Z_STRVAL_P(arg1), Z_STRLEN_P(arg1)+1, (void *) &current_section, sizeof(zval *), NULL);
current_section->value.ht = section_properties;
zend_hash_update(&browser_hash, Z_STRVAL_P(arg1), Z_STRLEN_P(arg1)+1, (void *) &current_section, sizeof(zval *), NULL);
Z_STRVAL_P(processed) = Z_STRVAL_P(arg1);
Z_STRLEN_P(processed) = Z_STRLEN_P(arg1);
Z_TYPE_P(processed) = IS_STRING;
Z_STRVAL_P(unprocessed) = Z_STRVAL_P(arg1);
Z_STRLEN_P(unprocessed) = Z_STRLEN_P(arg1);
Z_TYPE_P(unprocessed) = IS_STRING;
Z_STRVAL_P(unprocessed) = zend_strndup(Z_STRVAL_P(unprocessed), Z_STRLEN_P(unprocessed));
convert_browscap_pattern(processed);
zend_hash_update(section_properties, "browser_name_pattern", sizeof("browser_name_pattern"), (void *) &processed, sizeof(zval *), NULL);
zend_hash_update(section_properties, "browser_name_regex", sizeof("browser_name_regex"), (void *) &processed, sizeof(zval *), NULL);
zend_hash_update(section_properties, "browser_name_pattern", sizeof("browser_name_pattern"), (void *) &unprocessed, sizeof(zval *), NULL);
}
break;
}
@ -154,6 +164,7 @@ PHP_MINIT_FUNCTION(browscap)
if (browscap) {
zend_file_handle fh;
memset(&fh, 0, sizeof(fh));
if (zend_hash_init(&browser_hash, 0, NULL, (dtor_func_t) browscap_entry_dtor, 1)==FAILURE) {
return FAILURE;
@ -197,7 +208,10 @@ static int browser_reg_compare(zval **browser, int num_args, va_list args, zend_
if (*found_browser_entry) { /* already found */
return 0;
}
if(zend_hash_find(Z_OBJPROP_PP(browser), "browser_name_pattern", sizeof("browser_name_pattern"), (void **) &browser_name) == FAILURE) {
if (zend_hash_find(Z_ARRVAL_PP(browser), "browser_name_regex", sizeof("browser_name_regex"), (void **) &browser_name) == FAILURE) {
return 0;
}
if (Z_STRVAL_PP(browser_name)[0] != '^') {
return 0;
}
if (regcomp(&r, Z_STRVAL_PP(browser_name), REG_NOSUB)!=0) {
@ -215,35 +229,35 @@ static int browser_reg_compare(zval **browser, int num_args, va_list args, zend_
Get information about the capabilities of a browser */
PHP_FUNCTION(get_browser)
{
zval **agent_name, **agent;
zval **agent_name = NULL, **agent, **retarr;
zval *found_browser_entry, *tmp_copy;
char *lookup_browser_name;
zend_bool return_array = 0;
if (!INI_STR("browscap")) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "browscap ini directive not set.");
RETURN_FALSE;
}
switch(ZEND_NUM_ARGS()) {
case 0:
if (!PG(http_globals)[TRACK_VARS_SERVER]
|| zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, "HTTP_USER_AGENT", sizeof("HTTP_USER_AGENT"), (void **) &agent_name)==FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "HTTP_USER_AGENT variable is not set, cannot determine user agent name");
RETURN_FALSE;
}
break;
case 1:
if (zend_get_parameters_ex(1, &agent_name)==FAILURE) {
RETURN_FALSE;
}
break;
default:
WRONG_PARAM_COUNT;
break;
if (ZEND_NUM_ARGS() > 2 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &agent_name, &retarr) == FAILURE) {
ZEND_WRONG_PARAM_COUNT();
}
if (agent_name == NULL || Z_TYPE_PP(agent_name) == IS_NULL) {
if (!PG(http_globals)[TRACK_VARS_SERVER]
|| zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, "HTTP_USER_AGENT", sizeof("HTTP_USER_AGENT"), (void **) &agent_name)==FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "HTTP_USER_AGENT variable is not set, cannot determine user agent name");
RETURN_FALSE;
}
}
convert_to_string_ex(agent_name);
if (ZEND_NUM_ARGS() == 2) {
convert_to_boolean_ex(retarr);
return_array = Z_LVAL_PP(retarr);
}
if (zend_hash_find(&browser_hash, Z_STRVAL_PP(agent_name), Z_STRLEN_PP(agent_name)+1, (void **) &agent)==FAILURE) {
lookup_browser_name = Z_STRVAL_PP(agent_name);
found_browser_entry = NULL;
@ -255,17 +269,27 @@ PHP_FUNCTION(get_browser)
RETURN_FALSE;
}
}
object_init(return_value);
zend_hash_copy(Z_OBJPROP_P(return_value), Z_OBJPROP_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *));
while (zend_hash_find(Z_OBJPROP_PP(agent), "parent", sizeof("parent"), (void **) &agent_name)==SUCCESS) {
if (return_array) {
array_init(return_value);
zend_hash_copy(Z_ARRVAL_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *));
}
else {
object_init(return_value);
zend_hash_copy(Z_OBJPROP_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *));
}
while (zend_hash_find(Z_ARRVAL_PP(agent), "parent", sizeof("parent"), (void **) &agent_name)==SUCCESS) {
if (zend_hash_find(&browser_hash, Z_STRVAL_PP(agent_name), Z_STRLEN_PP(agent_name)+1, (void **)&agent)==FAILURE) {
break;
}
zend_hash_merge(Z_OBJPROP_P(return_value), Z_OBJPROP_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *), 0);
if (return_array) {
zend_hash_merge(Z_ARRVAL_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *), 0);
}
else {
zend_hash_merge(Z_OBJPROP_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *), 0);
}
}
}
/* }}} */