]> granicus.if.org Git - php/commitdiff
cleanup some code, improve error handling
authorStanislav Malyshev <stas@php.net>
Wed, 23 Dec 2009 21:41:05 +0000 (21:41 +0000)
committerStanislav Malyshev <stas@php.net>
Wed, 23 Dec 2009 21:41:05 +0000 (21:41 +0000)
ext/intl/dateformat/dateformat.c
ext/intl/formatter/formatter_main.c
ext/intl/grapheme/grapheme_string.c
ext/intl/grapheme/grapheme_util.c
ext/intl/idn/idn.c
ext/intl/intl_error.c
ext/intl/intl_error.h
ext/intl/locale/locale_methods.c
ext/intl/normalizer/normalizer_normalize.c
ext/intl/tests/badargs.phpt [new file with mode: 0755]

index 180a1de008f2ab07a901394d1f424c56f1aaedbd..a1b5fd694541118cf49fe33a3853a66d5ef31c57 100755 (executable)
@@ -155,8 +155,7 @@ PHP_METHOD( IntlDateFormatter, __construct )
  */
 PHP_FUNCTION( datefmt_get_error_code )
 {
-       zval*                    object  = NULL;
-       IntlDateFormatter_object*  dfo     = NULL;
+       DATE_FORMAT_METHOD_INIT_VARS;
 
        /* Parse parameters. */
        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
@@ -182,8 +181,7 @@ PHP_FUNCTION( datefmt_get_error_code )
 PHP_FUNCTION( datefmt_get_error_message )
 {
        char*                    message = NULL;
-       zval*                    object  = NULL;
-       IntlDateFormatter_object*  dfo     = NULL;
+       DATE_FORMAT_METHOD_INIT_VARS;
 
        /* Parse parameters. */
        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
@@ -198,7 +196,7 @@ PHP_FUNCTION( datefmt_get_error_message )
        dfo = (IntlDateFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
 
        /* Return last error message. */
-       message = intl_error_get_message( &dfo->datef_data.error TSRMLS_CC );
+       message = intl_error_get_message( INTL_DATA_ERROR_P(dfo) TSRMLS_CC );
        RETURN_STRING( message, 0);
 }
 /* }}} */
index 40fbfe63eeb194dd2d76919005c14dcdab67451e..18c5d9fe07df7dd49bb3e2608c15bc1c62d549d4 100755 (executable)
@@ -128,7 +128,7 @@ PHP_FUNCTION( numfmt_get_error_message )
        nfo = (NumberFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
 
        /* Return last error message. */
-       message = intl_error_get_message( &INTL_DATA_ERROR(nfo) TSRMLS_CC );
+       message = intl_error_get_message( INTL_DATA_ERROR_P(nfo) TSRMLS_CC );
        RETURN_STRING( message, 0);
 }
 /* }}} */
index 9a53afa72e7daf742a5e7bd0f0d5b13f437a666e..03b7f112794984a508201d08ece641be7ae39f22 100755 (executable)
@@ -83,7 +83,7 @@ PHP_FUNCTION(grapheme_strlen)
                intl_error_set_code( NULL, status TSRMLS_CC );
 
                /* Set error messages. */
-               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
+               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
                efree( ustring );
                RETURN_NULL();
        }
@@ -446,7 +446,7 @@ PHP_FUNCTION(grapheme_substr)
                intl_error_set_code( NULL, status TSRMLS_CC );
 
                /* Set error messages. */
-               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
+               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
                efree( ustr );
                RETURN_FALSE;
        }
@@ -507,7 +507,7 @@ PHP_FUNCTION(grapheme_substr)
                        intl_error_set_code( NULL, status TSRMLS_CC );
 
                        /* Set error messages. */
-                       intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 1 TSRMLS_CC );
+                       intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 0 TSRMLS_CC );
 
                        efree( sub_str );
 
@@ -563,7 +563,7 @@ PHP_FUNCTION(grapheme_substr)
                intl_error_set_code( NULL, status TSRMLS_CC );
 
                /* Set error messages. */
-               intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 1 TSRMLS_CC );
+               intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 0 TSRMLS_CC );
 
                if ( NULL != sub_str )
                        efree( sub_str );
@@ -869,7 +869,7 @@ PHP_FUNCTION(grapheme_extract)
                intl_error_set_code( NULL, status TSRMLS_CC );
 
                /* Set error messages. */
-               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
+               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
 
                if ( NULL != ustr )
                        efree( ustr );
index ab2367b13385a2de3996e3efb19692574b3311d5..8d2c80715c81fc034ae71c1a95eb5a131c2db66b 100755 (executable)
@@ -169,7 +169,7 @@ grapheme_strrpos_utf16(unsigned char *haystack, int32_t haystack_len, unsigned c
         intl_error_set_code( NULL, status TSRMLS_CC );
 
         /* Set error messages. */
-        intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
+        intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
         efree( uhaystack );
         return -1;
     }
@@ -202,7 +202,7 @@ grapheme_strrpos_utf16(unsigned char *haystack, int32_t haystack_len, unsigned c
         intl_error_set_code( NULL, status TSRMLS_CC );
 
         /* Set error messages. */
-        intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
+        intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
         efree( uhaystack );
         efree( uneedle );
         ubrk_close (bi);
@@ -294,7 +294,7 @@ grapheme_strpos_utf16(unsigned char *haystack, int32_t haystack_len, unsigned ch
                intl_error_set_code( NULL, status TSRMLS_CC );
 
                /* Set error messages. */
-               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
+               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
                efree( uhaystack );
                return -1;
        }
@@ -331,7 +331,7 @@ grapheme_strpos_utf16(unsigned char *haystack, int32_t haystack_len, unsigned ch
                intl_error_set_code( NULL, status TSRMLS_CC );
 
                /* Set error messages. */
-               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
+               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
                efree( uhaystack );
                efree( uneedle );
                ubrk_close (bi);
index d19254d6db24079153aa7be7a16484ea52ff0cba..54591e3bdcb1539c1d712548d38be738222bcc88 100644 (file)
@@ -65,7 +65,7 @@ static void php_intl_idn_to(INTERNAL_FUNCTION_PARAMETERS, int mode)
        int32_t   converted_ret_len;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "u|l", (char **)&domain, &domain_len, &option) == FAILURE) {
-               return;
+               RETURN_FALSE;
        }
 
        if (domain_len < 1) {
index 4e55d956df99c5708335f13ea01a9834395477b0..d139f283ab4942d8461cb6f2a32b440b9ccdd281 100755 (executable)
@@ -44,10 +44,9 @@ static void intl_free_custom_error_msg( intl_error* err TSRMLS_DC )
        if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
                return;
 
-       if( !err->free_custom_error_message )
-               return;
-
-       efree( err->custom_error_message );
+       if( err->free_custom_error_message ) {
+               efree( err->custom_error_message );
+       }
 
        err->custom_error_message      = NULL;
        err->free_custom_error_message = 0;
@@ -181,6 +180,16 @@ void intl_error_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TS
 }
 /* }}} */
 
+/* {{{ void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg )
+ * Set error code and message.
+ */
+void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC )
+{
+       intl_errors_set_code( err, code TSRMLS_CC );
+       intl_errors_set_custom_msg( err, msg, copyMsg TSRMLS_CC );
+}
+/* }}} */
+
 /* {{{ void intl_errors_reset( intl_error* err )
  */
 void intl_errors_reset( intl_error* err TSRMLS_DC )
index 2efaca37fba7913c876e2be7a5d0c4038aa56a6a..156f4d57b6982b7cd03f17a4a77505ddbba4c588 100755 (executable)
@@ -42,5 +42,6 @@ char*       intl_error_get_message( intl_error* err TSRMLS_DC );
 void        intl_errors_reset( intl_error* err TSRMLS_DC );
 void        intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg TSRMLS_DC );
 void        intl_errors_set_code( intl_error* err, UErrorCode err_code TSRMLS_DC );
+void        intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC );
 
 #endif // INTL_ERROR_H
index 1dfed65d74fcdb6a877a30dc5dc6e4c3f8d1deb5..1c9534d51ac86d6a7a36810517fa04f501673b56 100755 (executable)
@@ -389,7 +389,7 @@ static void get_icu_value_src_php( char* tag_name, INTERNAL_FUNCTION_PARAMETERS)
                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,  msg , 1 TSRMLS_CC );
                efree(msg);
 
-               RETURN_NULL();
+               RETURN_FALSE;
     }
 
        if(loc_name_len == 0) {
@@ -1129,10 +1129,10 @@ PHP_FUNCTION(locale_get_all_variants)
        if(zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s",
        &loc_name, &loc_name_len ) == FAILURE)
        {
-       intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
+               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
             "locale_parse: unable to parse input params", 0 TSRMLS_CC );
 
-       RETURN_FALSE;
+               RETURN_FALSE;
        }
 
        if(loc_name_len == 0) {
@@ -1524,7 +1524,7 @@ PHP_FUNCTION(locale_lookup)
        if(zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "as|bs", &arr, &loc_range, &loc_range_len,
                &boolCanonical, &fallback_loc, &fallback_loc_len) == FAILURE) {
                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "locale_lookup: unable to parse input params", 0 TSRMLS_CC );
-               RETURN_NULL();
+               RETURN_FALSE;
        }
 
        if(loc_range_len == 0) {
@@ -1571,7 +1571,7 @@ PHP_FUNCTION(locale_accept_from_http)
        {
                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
                "locale_accept_from_http: unable to parse input parameters", 0 TSRMLS_CC );
-               RETURN_NULL();
+               RETURN_FALSE;
        }
        
        available = ures_openAvailableLocales(NULL, &status);
index 97ae9e7f694b878079aad77188c2f211f6d04344..8691e3bcf8e3eecb103380ef2532aaf6f6df8fd2 100755 (executable)
@@ -52,9 +52,9 @@ PHP_FUNCTION( normalizer_normalize )
                                &uinput, &uinput_len, &form ) == FAILURE )
        {
                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
-                                                "normalizer_normalize: unable to parse input params", 1 TSRMLS_CC );
+                                                "normalizer_normalize: unable to parse input params", 0 TSRMLS_CC );
 
-               RETURN_NULL();
+               RETURN_FALSE;
        }
 
        expansion_factor = 1;
@@ -73,8 +73,8 @@ PHP_FUNCTION( normalizer_normalize )
                        break;
                default:
                        intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
-                                               "normalizer_normalize: illegal normalization form", 1 TSRMLS_CC );
-                       RETURN_NULL();
+                                               "normalizer_normalize: illegal normalization form", 0 TSRMLS_CC );
+                       RETURN_FALSE;
        }
 
        /*
@@ -94,7 +94,7 @@ PHP_FUNCTION( normalizer_normalize )
         */
        if( U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR && status != U_STRING_NOT_TERMINATED_WARNING ) {
                efree( uret_buf );
-               RETURN_NULL();
+               RETURN_FALSE;
        }
 
        if ( size_needed > uret_len ) {
@@ -113,9 +113,9 @@ PHP_FUNCTION( normalizer_normalize )
                /* Bail out if an unexpected error occured. */
                if( U_FAILURE(status)  ) {
                        /* Set error messages. */
-                       intl_error_set_custom_msg( NULL,"Error normalizing string", 1 TSRMLS_CC );
+                       intl_error_set_custom_msg( NULL,"Error normalizing string", 0 TSRMLS_CC );
                        efree( uret_buf );
-                       RETURN_NULL();
+                       RETURN_FALSE;
                }
        }
 
@@ -149,7 +149,7 @@ PHP_FUNCTION( normalizer_is_normalized )
                                &uinput, &uinput_len, &form) == FAILURE )
        {
                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
-                               "normalizer_is_normalized: unable to parse input params", 1 TSRMLS_CC );
+                               "normalizer_is_normalized: unable to parse input params", 0 TSRMLS_CC );
 
                RETURN_FALSE;
        }
@@ -164,8 +164,8 @@ PHP_FUNCTION( normalizer_is_normalized )
                        break;
                default:
                        intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
-                                               "normalizer_normalize: illegal normalization form", 1 TSRMLS_CC );
-                       RETURN_NULL();
+                                               "normalizer_normalize: illegal normalization form", 0 TSRMLS_CC );
+                       RETURN_FALSE;
        }
 
 
@@ -179,7 +179,7 @@ PHP_FUNCTION( normalizer_is_normalized )
        /* Bail out if an unexpected error occured. */
        if( U_FAILURE(status)  ) {
                /* Set error messages. */
-               intl_error_set_custom_msg( NULL,"Error testing if string is the given normalization form.", 1 TSRMLS_CC );
+               intl_error_set_custom_msg( NULL,"Error testing if string is the given normalization form.", 0 TSRMLS_CC );
                RETURN_FALSE;
        }
 
diff --git a/ext/intl/tests/badargs.phpt b/ext/intl/tests/badargs.phpt
new file mode 100755 (executable)
index 0000000..9232bbf
--- /dev/null
@@ -0,0 +1,25 @@
+--TEST--
+Check that bad argumens return the same
+--SKIPIF--
+<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>
+--FILE--
+<?php
+$funcs = get_extension_funcs("intl");
+function ignore_err() {}
+set_error_handler("ignore_err");
+$arg = new stdClass();
+foreach($funcs as $func) {
+        $rfunc = new ReflectionFunction($func);
+        if($rfunc->getNumberOfRequiredParameters() == 0) {
+                continue;
+        }
+        $res = $func($arg);
+        if($res != false) {
+                echo "$func: ";
+                var_dump($res);
+        }
+}
+echo "OK!\n";
+?>
+--EXPECT--
+OK!