Use reverse-unwinding goto-style error-handling in grapheme_levenshtein (#18451)

This reduces repetition and makes it harder to accidentally miss
cleaning up something.
This commit is contained in:
Niels Dossche 2025-04-29 22:48:08 +02:00 committed by GitHub
parent 272abc2fb7
commit 82e09db0e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -980,8 +980,8 @@ PHP_FUNCTION(grapheme_levenshtein)
intl_error_set_code(NULL, ustatus); intl_error_set_code(NULL, ustatus);
intl_error_set_custom_msg(NULL, "Error converting input string to UTF-16", 0); intl_error_set_custom_msg(NULL, "Error converting input string to UTF-16", 0);
efree(ustring1); RETVAL_FALSE;
RETURN_FALSE; goto out_ustring1;
} }
intl_convert_utf8_to_utf16(&ustring2, &ustring2_len, pstr2, ZSTR_LEN(string2), &ustatus); intl_convert_utf8_to_utf16(&ustring2, &ustring2_len, pstr2, ZSTR_LEN(string2), &ustatus);
@ -990,9 +990,8 @@ PHP_FUNCTION(grapheme_levenshtein)
intl_error_set_code(NULL, ustatus); intl_error_set_code(NULL, ustatus);
intl_error_set_custom_msg(NULL, "Error converting input string to UTF-16", 0); intl_error_set_custom_msg(NULL, "Error converting input string to UTF-16", 0);
efree(ustring2); RETVAL_FALSE;
efree(ustring1); goto out_ustring2;
RETURN_FALSE;
} }
UBreakIterator *bi1, *bi2; UBreakIterator *bi1, *bi2;
@ -1002,14 +1001,12 @@ PHP_FUNCTION(grapheme_levenshtein)
strlen_2 = grapheme_split_string(ustring2, ustring2_len, NULL, 0); strlen_2 = grapheme_split_string(ustring2, ustring2_len, NULL, 0);
if (strlen_1 == 0) { if (strlen_1 == 0) {
efree(ustring1); RETVAL_LONG(strlen_2 * cost_ins);
efree(ustring2); goto out_ustring2;
RETURN_LONG(strlen_2 * cost_ins);
} }
if (strlen_2 == 0) { if (strlen_2 == 0) {
efree(ustring1); RETVAL_LONG(strlen_1 * cost_del);
efree(ustring2); goto out_ustring2;
RETURN_LONG(strlen_1 * cost_del);
} }
unsigned char u_break_iterator_buffer1[U_BRK_SAFECLONE_BUFFERSIZE]; unsigned char u_break_iterator_buffer1[U_BRK_SAFECLONE_BUFFERSIZE];
@ -1018,33 +1015,25 @@ PHP_FUNCTION(grapheme_levenshtein)
if (U_FAILURE(ustatus)) { if (U_FAILURE(ustatus)) {
intl_error_set_code(NULL, ustatus); intl_error_set_code(NULL, ustatus);
intl_error_set_custom_msg(NULL, "Error on grapheme_get_break_iterator for argument #1 ($string1)", 0); intl_error_set_custom_msg(NULL, "Error on grapheme_get_break_iterator for argument #1 ($string1)", 0);
efree(ustring2); RETVAL_FALSE;
efree(ustring1); goto out_bi1;
ubrk_close(bi1);
RETURN_FALSE;
} }
bi2 = grapheme_get_break_iterator(u_break_iterator_buffer2, &ustatus); bi2 = grapheme_get_break_iterator(u_break_iterator_buffer2, &ustatus);
if (U_FAILURE(ustatus)) { if (U_FAILURE(ustatus)) {
intl_error_set_code(NULL, ustatus); intl_error_set_code(NULL, ustatus);
intl_error_set_custom_msg(NULL, "Error on grapheme_get_break_iterator for argument #2 ($string2)", 0); intl_error_set_custom_msg(NULL, "Error on grapheme_get_break_iterator for argument #2 ($string2)", 0);
efree(ustring2); RETVAL_FALSE;
efree(ustring1); goto out_bi2;
ubrk_close(bi2);
ubrk_close(bi1);
RETURN_FALSE;
} }
ubrk_setText(bi1, ustring1, ustring1_len, &ustatus);
ubrk_setText(bi1, ustring1, ustring1_len, &ustatus);
if (U_FAILURE(ustatus)) { if (U_FAILURE(ustatus)) {
intl_error_set_code(NULL, ustatus); intl_error_set_code(NULL, ustatus);
intl_error_set_custom_msg(NULL, "Error on ubrk_setText for argument #1 ($string1)", 0); intl_error_set_custom_msg(NULL, "Error on ubrk_setText for argument #1 ($string1)", 0);
efree(ustring2); RETVAL_FALSE;
efree(ustring1); goto out_bi2;
ubrk_close(bi2);
ubrk_close(bi1);
RETURN_FALSE;
} }
ubrk_setText(bi2, ustring2, ustring2_len, &ustatus); ubrk_setText(bi2, ustring2, ustring2_len, &ustatus);
@ -1052,23 +1041,16 @@ PHP_FUNCTION(grapheme_levenshtein)
intl_error_set_code(NULL, ustatus); intl_error_set_code(NULL, ustatus);
intl_error_set_custom_msg(NULL, "Error on ubrk_setText for argument #2 ($string2)", 0); intl_error_set_custom_msg(NULL, "Error on ubrk_setText for argument #2 ($string2)", 0);
efree(ustring2); RETVAL_FALSE;
efree(ustring1); goto out_bi2;
ubrk_close(bi2);
ubrk_close(bi1);
RETURN_FALSE;
} }
UCollator *collator = ucol_open("", &ustatus); UCollator *collator = ucol_open("", &ustatus);
if (U_FAILURE(ustatus)) { if (U_FAILURE(ustatus)) {
intl_error_set_code(NULL, ustatus); intl_error_set_code(NULL, ustatus);
intl_error_set_custom_msg(NULL, "Error on ucol_open", 0); intl_error_set_custom_msg(NULL, "Error on ucol_open", 0);
efree(ustring2); RETVAL_FALSE;
efree(ustring1); goto out_collator;
ubrk_close(bi2);
ubrk_close(bi1);
ucol_close(collator);
RETURN_FALSE;
} }
zend_long *p1, *p2, *tmp; zend_long *p1, *p2, *tmp;
@ -1118,19 +1100,22 @@ PHP_FUNCTION(grapheme_levenshtein)
p2 = tmp; p2 = tmp;
} }
ucol_close(collator);
ubrk_close(bi1);
ubrk_close(bi2);
efree(ustring1);
efree(ustring2);
retval = p1[strlen_2]; retval = p1[strlen_2];
RETVAL_LONG(retval);
efree(p1);
efree(p2); efree(p2);
RETURN_LONG(retval); efree(p1);
out_collator:
ucol_close(collator);
out_bi2:
ubrk_close(bi2);
out_bi1:
ubrk_close(bi1);
out_ustring2:
efree(ustring2);
out_ustring1:
efree(ustring1);
} }
/* }}} */ /* }}} */