Skip to content

Commit 882a89f

Browse files
committed
cleanup some code, improve error handling
1 parent f0c1cd5 commit 882a89f

File tree

10 files changed

+74
-41
lines changed

10 files changed

+74
-41
lines changed

Diff for: ext/intl/dateformat/dateformat.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,7 @@ PHP_METHOD( IntlDateFormatter, __construct )
181181
*/
182182
PHP_FUNCTION( datefmt_get_error_code )
183183
{
184-
zval* object = NULL;
185-
IntlDateFormatter_object* dfo = NULL;
184+
DATE_FORMAT_METHOD_INIT_VARS;
186185

187186
/* Parse parameters. */
188187
if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
@@ -208,8 +207,7 @@ PHP_FUNCTION( datefmt_get_error_code )
208207
PHP_FUNCTION( datefmt_get_error_message )
209208
{
210209
char* message = NULL;
211-
zval* object = NULL;
212-
IntlDateFormatter_object* dfo = NULL;
210+
DATE_FORMAT_METHOD_INIT_VARS;
213211

214212
/* Parse parameters. */
215213
if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
@@ -224,7 +222,7 @@ PHP_FUNCTION( datefmt_get_error_message )
224222
dfo = (IntlDateFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
225223

226224
/* Return last error message. */
227-
message = intl_error_get_message( &dfo->datef_data.error TSRMLS_CC );
225+
message = intl_error_get_message( INTL_DATA_ERROR_P(dfo) TSRMLS_CC );
228226
RETURN_STRING( message, 0);
229227
}
230228
/* }}} */

Diff for: ext/intl/formatter/formatter_main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ PHP_FUNCTION( numfmt_get_error_message )
141141
nfo = (NumberFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
142142

143143
/* Return last error message. */
144-
message = intl_error_get_message( &INTL_DATA_ERROR(nfo) TSRMLS_CC );
144+
message = intl_error_get_message( INTL_DATA_ERROR_P(nfo) TSRMLS_CC );
145145
RETURN_STRING( message, 0);
146146
}
147147
/* }}} */

Diff for: ext/intl/grapheme/grapheme_string.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ PHP_FUNCTION(grapheme_strlen)
8383
intl_error_set_code( NULL, status TSRMLS_CC );
8484

8585
/* Set error messages. */
86-
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
86+
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
8787
efree( ustring );
8888
RETURN_NULL();
8989
}
@@ -446,7 +446,7 @@ PHP_FUNCTION(grapheme_substr)
446446
intl_error_set_code( NULL, status TSRMLS_CC );
447447

448448
/* Set error messages. */
449-
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
449+
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
450450
efree( ustr );
451451
RETURN_FALSE;
452452
}
@@ -507,7 +507,7 @@ PHP_FUNCTION(grapheme_substr)
507507
intl_error_set_code( NULL, status TSRMLS_CC );
508508

509509
/* Set error messages. */
510-
intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 1 TSRMLS_CC );
510+
intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 0 TSRMLS_CC );
511511

512512
efree( sub_str );
513513

@@ -563,7 +563,7 @@ PHP_FUNCTION(grapheme_substr)
563563
intl_error_set_code( NULL, status TSRMLS_CC );
564564

565565
/* Set error messages. */
566-
intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 1 TSRMLS_CC );
566+
intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 0 TSRMLS_CC );
567567

568568
if ( NULL != sub_str )
569569
efree( sub_str );
@@ -870,7 +870,7 @@ PHP_FUNCTION(grapheme_extract)
870870
intl_error_set_code( NULL, status TSRMLS_CC );
871871

872872
/* Set error messages. */
873-
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
873+
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
874874

875875
if ( NULL != ustr )
876876
efree( ustr );

Diff for: ext/intl/grapheme/grapheme_util.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ grapheme_strrpos_utf16(unsigned char *haystack, int32_t haystack_len, unsigned c
169169
intl_error_set_code( NULL, status TSRMLS_CC );
170170

171171
/* Set error messages. */
172-
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
172+
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
173173
efree( uhaystack );
174174
return -1;
175175
}
@@ -202,7 +202,7 @@ grapheme_strrpos_utf16(unsigned char *haystack, int32_t haystack_len, unsigned c
202202
intl_error_set_code( NULL, status TSRMLS_CC );
203203

204204
/* Set error messages. */
205-
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
205+
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
206206
efree( uhaystack );
207207
efree( uneedle );
208208
ubrk_close (bi);
@@ -294,7 +294,7 @@ grapheme_strpos_utf16(unsigned char *haystack, int32_t haystack_len, unsigned ch
294294
intl_error_set_code( NULL, status TSRMLS_CC );
295295

296296
/* Set error messages. */
297-
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
297+
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
298298
efree( uhaystack );
299299
return -1;
300300
}
@@ -331,7 +331,7 @@ grapheme_strpos_utf16(unsigned char *haystack, int32_t haystack_len, unsigned ch
331331
intl_error_set_code( NULL, status TSRMLS_CC );
332332

333333
/* Set error messages. */
334-
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
334+
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
335335
efree( uhaystack );
336336
efree( uneedle );
337337
ubrk_close (bi);

Diff for: ext/intl/idn/idn.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static void php_intl_idn_to(INTERNAL_FUNCTION_PARAMETERS, int mode)
8484
intl_error_set_code(NULL, status TSRMLS_CC);
8585

8686
/* Set error messages. */
87-
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
87+
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
8888
efree(ustring);
8989
RETURN_FALSE;
9090
} else {
@@ -111,7 +111,7 @@ static void php_intl_idn_to(INTERNAL_FUNCTION_PARAMETERS, int mode)
111111
intl_error_set_code(NULL, status TSRMLS_CC);
112112

113113
/* Set error messages. */
114-
intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 1 TSRMLS_CC );
114+
intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 0 TSRMLS_CC );
115115
efree(converted_utf8);
116116
RETURN_FALSE;
117117
}

Diff for: ext/intl/intl_error.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ static void intl_free_custom_error_msg( intl_error* err TSRMLS_DC )
4444
if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
4545
return;
4646

47-
if( !err->free_custom_error_message )
48-
return;
49-
50-
efree( err->custom_error_message );
47+
if(err->free_custom_error_message ) {
48+
efree( err->custom_error_message );
49+
}
5150

5251
err->custom_error_message = NULL;
5352
err->free_custom_error_message = 0;
@@ -181,6 +180,16 @@ void intl_error_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TS
181180
}
182181
/* }}} */
183182

183+
/* {{{ void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg )
184+
* Set error code and message.
185+
*/
186+
void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC )
187+
{
188+
intl_errors_set_code( err, code TSRMLS_CC );
189+
intl_errors_set_custom_msg( err, msg, copyMsg TSRMLS_CC );
190+
}
191+
/* }}} */
192+
184193
/* {{{ void intl_errors_reset( intl_error* err )
185194
*/
186195
void intl_errors_reset( intl_error* err TSRMLS_DC )

Diff for: ext/intl/intl_error.h

+1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ char* intl_error_get_message( intl_error* err TSRMLS_DC );
4242
void intl_errors_reset( intl_error* err TSRMLS_DC );
4343
void intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg TSRMLS_DC );
4444
void intl_errors_set_code( intl_error* err, UErrorCode err_code TSRMLS_DC );
45+
void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC );
4546

4647
#endif // INTL_ERROR_H

Diff for: ext/intl/locale/locale_methods.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ static void get_icu_value_src_php( char* tag_name, INTERNAL_FUNCTION_PARAMETERS)
389389
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, msg , 1 TSRMLS_CC );
390390
efree(msg);
391391

392-
RETURN_NULL();
392+
RETURN_FALSE;
393393
}
394394

395395
if(loc_name_len == 0) {
@@ -1129,10 +1129,10 @@ PHP_FUNCTION(locale_get_all_variants)
11291129
if(zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s",
11301130
&loc_name, &loc_name_len ) == FAILURE)
11311131
{
1132-
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
1132+
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
11331133
"locale_parse: unable to parse input params", 0 TSRMLS_CC );
11341134

1135-
RETURN_FALSE;
1135+
RETURN_FALSE;
11361136
}
11371137

11381138
if(loc_name_len == 0) {
@@ -1524,7 +1524,7 @@ PHP_FUNCTION(locale_lookup)
15241524
if(zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "as|bs", &arr, &loc_range, &loc_range_len,
15251525
&boolCanonical, &fallback_loc, &fallback_loc_len) == FAILURE) {
15261526
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "locale_lookup: unable to parse input params", 0 TSRMLS_CC );
1527-
RETURN_NULL();
1527+
RETURN_FALSE;
15281528
}
15291529

15301530
if(loc_range_len == 0) {
@@ -1571,7 +1571,7 @@ PHP_FUNCTION(locale_accept_from_http)
15711571
{
15721572
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
15731573
"locale_accept_from_http: unable to parse input parameters", 0 TSRMLS_CC );
1574-
RETURN_NULL();
1574+
RETURN_FALSE;
15751575
}
15761576

15771577
available = ures_openAvailableLocales(NULL, &status);

Diff for: ext/intl/normalizer/normalizer_normalize.c

+15-15
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ PHP_FUNCTION( normalizer_normalize )
5757
&input, &input_len, &form ) == FAILURE )
5858
{
5959
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
60-
"normalizer_normalize: unable to parse input params", 1 TSRMLS_CC );
60+
"normalizer_normalize: unable to parse input params", 0 TSRMLS_CC );
6161

62-
RETURN_NULL();
62+
RETURN_FALSE;
6363
}
6464

6565
expansion_factor = 1;
@@ -78,8 +78,8 @@ PHP_FUNCTION( normalizer_normalize )
7878
break;
7979
default:
8080
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
81-
"normalizer_normalize: illegal normalization form", 1 TSRMLS_CC );
82-
RETURN_NULL();
81+
"normalizer_normalize: illegal normalization form", 0 TSRMLS_CC );
82+
RETURN_FALSE;
8383
}
8484

8585
/*
@@ -95,9 +95,9 @@ PHP_FUNCTION( normalizer_normalize )
9595
intl_error_set_code( NULL, status TSRMLS_CC );
9696

9797
/* Set error messages. */
98-
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC );
98+
intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
9999
efree( uinput );
100-
RETURN_NULL();
100+
RETURN_FALSE;
101101
}
102102

103103

@@ -134,10 +134,10 @@ PHP_FUNCTION( normalizer_normalize )
134134
/* Bail out if an unexpected error occured. */
135135
if( U_FAILURE(status) ) {
136136
/* Set error messages. */
137-
intl_error_set_custom_msg( NULL,"Error normalizing string", 1 TSRMLS_CC );
137+
intl_error_set_custom_msg( NULL,"Error normalizing string", 0 TSRMLS_CC );
138138
efree( uret_buf );
139139
efree( uinput );
140-
RETURN_NULL();
140+
RETURN_FALSE;
141141
}
142142
}
143143

@@ -152,8 +152,8 @@ PHP_FUNCTION( normalizer_normalize )
152152
if( U_FAILURE( status ) )
153153
{
154154
intl_error_set( NULL, status,
155-
"normalizer_normalize: error converting normalized text UTF-8", 1 TSRMLS_CC );
156-
RETURN_NULL();
155+
"normalizer_normalize: error converting normalized text UTF-8", 0 TSRMLS_CC );
156+
RETURN_FALSE;
157157
}
158158

159159
/* Return it. */
@@ -186,7 +186,7 @@ PHP_FUNCTION( normalizer_is_normalized )
186186
&input, &input_len, &form) == FAILURE )
187187
{
188188
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
189-
"normalizer_is_normalized: unable to parse input params", 1 TSRMLS_CC );
189+
"normalizer_is_normalized: unable to parse input params", 0 TSRMLS_CC );
190190

191191
RETURN_FALSE;
192192
}
@@ -201,8 +201,8 @@ PHP_FUNCTION( normalizer_is_normalized )
201201
break;
202202
default:
203203
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
204-
"normalizer_normalize: illegal normalization form", 1 TSRMLS_CC );
205-
RETURN_NULL();
204+
"normalizer_normalize: illegal normalization form", 0 TSRMLS_CC );
205+
RETURN_FALSE;
206206
}
207207

208208

@@ -219,7 +219,7 @@ PHP_FUNCTION( normalizer_is_normalized )
219219
intl_error_set_code( NULL, status TSRMLS_CC );
220220

221221
/* Set error messages. */
222-
intl_error_set_custom_msg( NULL, "Error converting string to UTF-16.", 1 TSRMLS_CC );
222+
intl_error_set_custom_msg( NULL, "Error converting string to UTF-16.", 0 TSRMLS_CC );
223223
efree( uinput );
224224
RETURN_FALSE;
225225
}
@@ -233,7 +233,7 @@ PHP_FUNCTION( normalizer_is_normalized )
233233
/* Bail out if an unexpected error occured. */
234234
if( U_FAILURE(status) ) {
235235
/* Set error messages. */
236-
intl_error_set_custom_msg( NULL,"Error testing if string is the given normalization form.", 1 TSRMLS_CC );
236+
intl_error_set_custom_msg( NULL,"Error testing if string is the given normalization form.", 0 TSRMLS_CC );
237237
RETURN_FALSE;
238238
}
239239

Diff for: ext/intl/tests/badargs.phpt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Check that bad argumens return the same
3+
--SKIPIF--
4+
<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>
5+
--FILE--
6+
<?php
7+
$funcs = get_extension_funcs("intl");
8+
function ignore_err() {}
9+
set_error_handler("ignore_err");
10+
$arg = new stdClass();
11+
foreach($funcs as $func) {
12+
$rfunc = new ReflectionFunction($func);
13+
if($rfunc->getNumberOfRequiredParameters() == 0) {
14+
continue;
15+
}
16+
$res = $func($arg);
17+
if($res != false) {
18+
echo "$func: ";
19+
var_dump($res);
20+
}
21+
}
22+
echo "OK!\n";
23+
?>
24+
--EXPECT--
25+
OK!

0 commit comments

Comments
 (0)