Skip to content

Commit 7ef1380

Browse files
Fix leak and simplify error handling
1 parent a096a14 commit 7ef1380

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

ext/intl/listformatter/listformatter_class.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,21 @@ PHP_METHOD(IntlListFormatter, format)
153153
UErrorCode status = U_ZERO_ERROR;
154154

155155
intl_convert_utf8_to_utf16(&ustr, &ustr_len, ZSTR_VAL(str_val), ZSTR_LEN(str_val), &status);
156+
zend_string_release(str_val);
157+
156158
if (U_FAILURE(status)) {
159+
// We can't use goto cleanup because items and itemLengths are incompletely allocated
157160
for (uint32_t j = 0; j < i; j++) {
158161
efree((void *)items[j]);
159162
}
160163
efree(items);
161164
efree(itemLengths);
162-
zend_string_release(str_val);
163165
intl_error_set(NULL, status, "Failed to convert string to UTF-16", 0);
164166
RETURN_FALSE;
165167
}
166168

167169
items[i] = ustr;
168170
itemLengths[i] = ustr_len;
169-
zend_string_release(str_val);
170171
i++;
171172
} ZEND_HASH_FOREACH_END();
172173

@@ -178,27 +179,22 @@ PHP_METHOD(IntlListFormatter, format)
178179

179180
if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) {
180181
intl_error_set(NULL, status, "Failed to format list", 0);
181-
RETURN_FALSE;
182+
RETVAL_FALSE;
183+
goto cleanup;
182184
}
183185

184186
// Allocate buffer and try again
185187
status = U_ZERO_ERROR;
186188
result = (UChar *)emalloc((resultLength + 1) * sizeof(UChar));
187189
ulistfmt_format(LISTFORMATTER_OBJECT(obj), items, itemLengths, count, result, resultLength, &status);
188190

189-
// Clean up input strings
190-
for (i = 0; i < count; i++) {
191-
efree((void *)items[i]);
192-
}
193-
efree(items);
194-
efree(itemLengths);
195-
196191
if (U_FAILURE(status)) {
197192
if (result) {
198193
efree(result);
199194
}
200195
intl_error_set(NULL, status, "Failed to format list", 0);
201-
RETURN_FALSE;
196+
RETVAL_FALSE;
197+
goto cleanup;
202198
}
203199

204200
// Convert result back to UTF-8
@@ -207,10 +203,17 @@ PHP_METHOD(IntlListFormatter, format)
207203

208204
if (!ret) {
209205
intl_error_set(NULL, status, "Failed to convert result to UTF-8", 0);
210-
RETURN_FALSE;
206+
RETVAL_FALSE;
207+
} else {
208+
RETVAL_NEW_STR(ret);
211209
}
212210

213-
RETURN_NEW_STR(ret);
211+
cleanup:
212+
for (i = 0; i < count; i++) {
213+
efree((void *)items[i]);
214+
}
215+
efree(items);
216+
efree(itemLengths);
214217
}
215218
/* }}} */
216219

0 commit comments

Comments
 (0)