Skip to content

Commit 82e09db

Browse files
authored
Use reverse-unwinding goto-style error-handling in grapheme_levenshtein (#18451)
This reduces repetition and makes it harder to accidentally miss cleaning up something.
1 parent 272abc2 commit 82e09db

File tree

1 file changed

+32
-47
lines changed

1 file changed

+32
-47
lines changed

ext/intl/grapheme/grapheme_string.c

+32-47
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,8 @@ PHP_FUNCTION(grapheme_levenshtein)
980980
intl_error_set_code(NULL, ustatus);
981981

982982
intl_error_set_custom_msg(NULL, "Error converting input string to UTF-16", 0);
983-
efree(ustring1);
984-
RETURN_FALSE;
983+
RETVAL_FALSE;
984+
goto out_ustring1;
985985
}
986986

987987
intl_convert_utf8_to_utf16(&ustring2, &ustring2_len, pstr2, ZSTR_LEN(string2), &ustatus);
@@ -990,9 +990,8 @@ PHP_FUNCTION(grapheme_levenshtein)
990990
intl_error_set_code(NULL, ustatus);
991991

992992
intl_error_set_custom_msg(NULL, "Error converting input string to UTF-16", 0);
993-
efree(ustring2);
994-
efree(ustring1);
995-
RETURN_FALSE;
993+
RETVAL_FALSE;
994+
goto out_ustring2;
996995
}
997996

998997
UBreakIterator *bi1, *bi2;
@@ -1002,14 +1001,12 @@ PHP_FUNCTION(grapheme_levenshtein)
10021001
strlen_2 = grapheme_split_string(ustring2, ustring2_len, NULL, 0);
10031002

10041003
if (strlen_1 == 0) {
1005-
efree(ustring1);
1006-
efree(ustring2);
1007-
RETURN_LONG(strlen_2 * cost_ins);
1004+
RETVAL_LONG(strlen_2 * cost_ins);
1005+
goto out_ustring2;
10081006
}
10091007
if (strlen_2 == 0) {
1010-
efree(ustring1);
1011-
efree(ustring2);
1012-
RETURN_LONG(strlen_1 * cost_del);
1008+
RETVAL_LONG(strlen_1 * cost_del);
1009+
goto out_ustring2;
10131010
}
10141011

10151012
unsigned char u_break_iterator_buffer1[U_BRK_SAFECLONE_BUFFERSIZE];
@@ -1018,57 +1015,42 @@ PHP_FUNCTION(grapheme_levenshtein)
10181015
if (U_FAILURE(ustatus)) {
10191016
intl_error_set_code(NULL, ustatus);
10201017
intl_error_set_custom_msg(NULL, "Error on grapheme_get_break_iterator for argument #1 ($string1)", 0);
1021-
efree(ustring2);
1022-
efree(ustring1);
1023-
ubrk_close(bi1);
1024-
RETURN_FALSE;
1018+
RETVAL_FALSE;
1019+
goto out_bi1;
10251020
}
10261021

10271022
bi2 = grapheme_get_break_iterator(u_break_iterator_buffer2, &ustatus);
10281023
if (U_FAILURE(ustatus)) {
10291024
intl_error_set_code(NULL, ustatus);
10301025
intl_error_set_custom_msg(NULL, "Error on grapheme_get_break_iterator for argument #2 ($string2)", 0);
1031-
efree(ustring2);
1032-
efree(ustring1);
1033-
ubrk_close(bi2);
1034-
ubrk_close(bi1);
1035-
RETURN_FALSE;
1026+
RETVAL_FALSE;
1027+
goto out_bi2;
10361028
}
1037-
ubrk_setText(bi1, ustring1, ustring1_len, &ustatus);
10381029

1030+
ubrk_setText(bi1, ustring1, ustring1_len, &ustatus);
10391031
if (U_FAILURE(ustatus)) {
10401032
intl_error_set_code(NULL, ustatus);
10411033

10421034
intl_error_set_custom_msg(NULL, "Error on ubrk_setText for argument #1 ($string1)", 0);
1043-
efree(ustring2);
1044-
efree(ustring1);
1045-
ubrk_close(bi2);
1046-
ubrk_close(bi1);
1047-
RETURN_FALSE;
1035+
RETVAL_FALSE;
1036+
goto out_bi2;
10481037
}
10491038

10501039
ubrk_setText(bi2, ustring2, ustring2_len, &ustatus);
10511040
if (U_FAILURE(ustatus)) {
10521041
intl_error_set_code(NULL, ustatus);
10531042

10541043
intl_error_set_custom_msg(NULL, "Error on ubrk_setText for argument #2 ($string2)", 0);
1055-
efree(ustring2);
1056-
efree(ustring1);
1057-
ubrk_close(bi2);
1058-
ubrk_close(bi1);
1059-
RETURN_FALSE;
1044+
RETVAL_FALSE;
1045+
goto out_bi2;
10601046
}
10611047
UCollator *collator = ucol_open("", &ustatus);
10621048
if (U_FAILURE(ustatus)) {
10631049
intl_error_set_code(NULL, ustatus);
10641050

10651051
intl_error_set_custom_msg(NULL, "Error on ucol_open", 0);
1066-
efree(ustring2);
1067-
efree(ustring1);
1068-
ubrk_close(bi2);
1069-
ubrk_close(bi1);
1070-
ucol_close(collator);
1071-
RETURN_FALSE;
1052+
RETVAL_FALSE;
1053+
goto out_collator;
10721054
}
10731055

10741056
zend_long *p1, *p2, *tmp;
@@ -1118,19 +1100,22 @@ PHP_FUNCTION(grapheme_levenshtein)
11181100
p2 = tmp;
11191101
}
11201102

1121-
ucol_close(collator);
1122-
1123-
ubrk_close(bi1);
1124-
ubrk_close(bi2);
1125-
1126-
efree(ustring1);
1127-
efree(ustring2);
1128-
11291103
retval = p1[strlen_2];
1104+
RETVAL_LONG(retval);
11301105

1131-
efree(p1);
11321106
efree(p2);
1133-
RETURN_LONG(retval);
1107+
efree(p1);
1108+
1109+
out_collator:
1110+
ucol_close(collator);
1111+
out_bi2:
1112+
ubrk_close(bi2);
1113+
out_bi1:
1114+
ubrk_close(bi1);
1115+
out_ustring2:
1116+
efree(ustring2);
1117+
out_ustring1:
1118+
efree(ustring1);
11341119
}
11351120

11361121
/* }}} */

0 commit comments

Comments
 (0)