Skip to content

Commit 5ea5bcc

Browse files
committed
Another batch of fixes
1 parent 85bca21 commit 5ea5bcc

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

ext/uri/php_uri.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -846,12 +846,10 @@ static PHP_MINIT_FUNCTION(uri)
846846

847847
zend_hash_init(&uri_handlers, 4, NULL, NULL, true);
848848

849-
if (uri_handler_register(&uriparser_uri_handler) == FAILURE) {
849+
if (PHP_MINIT(uri_uriparser)(INIT_FUNC_ARGS_PASSTHRU) == FAILURE) {
850850
return FAILURE;
851851
}
852852

853-
uriparser_module_init();
854-
855853
if (uri_handler_register(&lexbor_uri_handler) == FAILURE) {
856854
return FAILURE;
857855
}

ext/uri/php_uriparser.c

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ void *uriparser_parse_uri(const zend_string *uri_str, const void *base_url, zval
2424
static void uriparser_free_uri(void *uri);
2525
static void throw_invalid_uri_exception(void);
2626

27+
static inline size_t get_text_range_length(const UriTextRangeA *range)
28+
{
29+
return range->afterLast - range->first;
30+
}
31+
2732
static void uriparser_copy_text_range(UriTextRangeA *text_range, UriTextRangeA *new_text_range, bool use_safe)
2833
{
2934
if (text_range->first == NULL || text_range->afterLast == NULL || (text_range->first > text_range->afterLast && !use_safe)) {
@@ -33,7 +38,7 @@ static void uriparser_copy_text_range(UriTextRangeA *text_range, UriTextRangeA *
3338
new_text_range->first = uriSafeToPointToA;
3439
new_text_range->afterLast = uriSafeToPointToA;
3540
} else {
36-
size_t length = (size_t) (text_range->afterLast - text_range->first);
41+
size_t length = get_text_range_length(text_range);
3742
char *dup = emalloc(length);
3843
memcpy(dup, text_range->first, length);
3944

@@ -135,8 +140,8 @@ static zend_result uriparser_read_scheme(const uri_internal_t *internal_uri, uri
135140
}
136141

137142
if (uriparser_uri->scheme.first != NULL && uriparser_uri->scheme.afterLast != NULL) {
138-
zend_string *str = zend_string_init(uriparser_uri->scheme.first, uriparser_uri->scheme.afterLast - uriparser_uri->scheme.first, false);
139-
ZVAL_STR(retval, str);
143+
zend_string *str = zend_string_init(uriparser_uri->scheme.first, get_text_range_length(&uriparser_uri->scheme), false);
144+
ZVAL_NEW_STR(retval, str);
140145
} else {
141146
ZVAL_NULL(retval);
142147
}
@@ -152,7 +157,7 @@ zend_result uriparser_read_userinfo(const uri_internal_t *internal_uri, uri_comp
152157
}
153158

154159
if (uriparser_uri->userInfo.first != NULL && uriparser_uri->userInfo.afterLast != NULL) {
155-
ZVAL_STRINGL(retval, uriparser_uri->userInfo.first, uriparser_uri->userInfo.afterLast - uriparser_uri->userInfo.first);
160+
ZVAL_STRINGL(retval, uriparser_uri->userInfo.first, get_text_range_length(&uriparser_uri->userInfo));
156161
} else {
157162
ZVAL_NULL(retval);
158163
}
@@ -168,7 +173,7 @@ static zend_result uriparser_read_username(const uri_internal_t *internal_uri, u
168173
}
169174

170175
if (uriparser_uri->userInfo.first != NULL && uriparser_uri->userInfo.afterLast != NULL) {
171-
size_t length = uriparser_uri->userInfo.afterLast - uriparser_uri->userInfo.first;
176+
size_t length = get_text_range_length(&uriparser_uri->userInfo);
172177
char *c = memchr(uriparser_uri->userInfo.first, ':', length);
173178

174179
if (c == NULL && length > 0) {
@@ -193,7 +198,7 @@ static zend_result uriparser_read_password(const uri_internal_t *internal_uri, u
193198
}
194199

195200
if (uriparser_uri->userInfo.first != NULL && uriparser_uri->userInfo.afterLast != NULL) {
196-
const char *c = memchr(uriparser_uri->userInfo.first, ':', uriparser_uri->userInfo.afterLast - uriparser_uri->userInfo.first);
201+
const char *c = memchr(uriparser_uri->userInfo.first, ':', get_text_range_length(&uriparser_uri->userInfo));
197202

198203
if (c != NULL && uriparser_uri->userInfo.afterLast - c - 1 > 0) {
199204
ZVAL_STRINGL(retval, c + 1, uriparser_uri->userInfo.afterLast - c - 1);
@@ -214,17 +219,17 @@ static zend_result uriparser_read_host(const uri_internal_t *internal_uri, uri_c
214219
return FAILURE;
215220
}
216221

217-
if (uriparser_uri->hostText.first != NULL && uriparser_uri->hostText.afterLast != NULL && uriparser_uri->hostText.afterLast - uriparser_uri->hostText.first > 0) {
222+
if (uriparser_uri->hostText.first != NULL && uriparser_uri->hostText.afterLast != NULL && get_text_range_length(&uriparser_uri->hostText) > 0) {
218223
if (uriparser_uri->hostData.ip6 != NULL) {
219224
smart_str host_str = {0};
220225

221226
smart_str_appendc(&host_str, '[');
222-
smart_str_appendl(&host_str, uriparser_uri->hostText.first, uriparser_uri->hostText.afterLast - uriparser_uri->hostText.first);
227+
smart_str_appendl(&host_str, uriparser_uri->hostText.first, get_text_range_length(&uriparser_uri->hostText));
223228
smart_str_appendc(&host_str, ']');
224229

225230
ZVAL_NEW_STR(retval, smart_str_extract(&host_str));
226231
} else {
227-
ZVAL_STRINGL(retval, uriparser_uri->hostText.first, uriparser_uri->hostText.afterLast - uriparser_uri->hostText.first);
232+
ZVAL_STRINGL(retval, uriparser_uri->hostText.first, get_text_range_length(&uriparser_uri->hostText));
228233
}
229234
} else {
230235
ZVAL_NULL(retval);
@@ -233,13 +238,26 @@ static zend_result uriparser_read_host(const uri_internal_t *internal_uri, uri_c
233238
return SUCCESS;
234239
}
235240

241+
static int str_to_int(const char *str, int len)
242+
{
243+
int result = 0;
244+
245+
for (int i = 0; i < len; ++i) {
246+
result = result * 10 + (str[i] - '0');
247+
}
248+
249+
return result;
250+
}
251+
236252
static zend_result uriparser_read_port(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
237253
{
238-
uriparser_uris_t *uriparser_uris = (uriparser_uris_t *) internal_uri->uri;
239-
UriUriA *uriparser_uri = uriparser_uris->uri;
254+
UriUriA *uriparser_uri = uriparser_read_uri(internal_uri->uri, read_mode);
255+
if (UNEXPECTED(uriparser_uri == NULL)) {
256+
return FAILURE;
257+
}
240258

241259
if (uriparser_uri->portText.first != NULL && uriparser_uri->portText.afterLast != NULL) {
242-
ZVAL_LONG(retval, strtol(uriparser_uri->portText.first, NULL, 10));
260+
ZVAL_LONG(retval, str_to_int(uriparser_uri->portText.first, get_text_range_length(&uriparser_uri->portText)));
243261
} else {
244262
ZVAL_NULL(retval);
245263
}
@@ -262,11 +280,11 @@ static zend_result uriparser_read_path(const uri_internal_t *internal_uri, uri_c
262280
smart_str_appendc(&str, '/');
263281
}
264282

265-
smart_str_appendl(&str, uriparser_uri->pathHead->text.first, (size_t) ((uriparser_uri->pathHead->text).afterLast - (uriparser_uri->pathHead->text).first));
283+
smart_str_appendl(&str, uriparser_uri->pathHead->text.first, get_text_range_length(&uriparser_uri->pathHead->text));
266284

267285
for (p = uriparser_uri->pathHead->next; p != NULL; p = p->next) {
268286
smart_str_appendc(&str, '/');
269-
smart_str_appendl(&str, p->text.first, (int) ((p->text).afterLast - (p->text).first));
287+
smart_str_appendl(&str, p->text.first, get_text_range_length(&p->text));
270288
}
271289

272290
ZVAL_NEW_STR(retval, smart_str_extract(&str));
@@ -287,7 +305,7 @@ static zend_result uriparser_read_query(const uri_internal_t *internal_uri, uri_
287305
}
288306

289307
if (uriparser_uri->query.first != NULL && uriparser_uri->query.afterLast != NULL) {
290-
ZVAL_STRINGL(retval, uriparser_uri->query.first, uriparser_uri->query.afterLast - uriparser_uri->query.first);
308+
ZVAL_STRINGL(retval, uriparser_uri->query.first, get_text_range_length(&uriparser_uri->query));
291309
} else {
292310
ZVAL_NULL(retval);
293311
}
@@ -303,7 +321,7 @@ static zend_result uriparser_read_fragment(const uri_internal_t *internal_uri, u
303321
}
304322

305323
if (uriparser_uri->fragment.first != NULL && uriparser_uri->fragment.afterLast != NULL) {
306-
ZVAL_STRINGL(retval, uriparser_uri->fragment.first, uriparser_uri->fragment.afterLast - uriparser_uri->fragment.first);
324+
ZVAL_STRINGL(retval, uriparser_uri->fragment.first, get_text_range_length(&uriparser_uri->fragment));
307325
} else {
308326
ZVAL_NULL(retval);
309327
}
@@ -336,13 +354,19 @@ static void uriparser_free(UriMemoryManager *memory_manager, void *ptr)
336354
efree(ptr);
337355
}
338356

339-
void uriparser_module_init(void)
357+
PHP_MINIT_FUNCTION(uri_uriparser)
340358
{
359+
if (uri_handler_register(&uriparser_uri_handler) == FAILURE) {
360+
return FAILURE;
361+
}
362+
341363
defaultMemoryManager.malloc = uriparser_malloc;
342364
defaultMemoryManager.calloc = uriparser_calloc;
343365
defaultMemoryManager.realloc = uriparser_realloc;
344366
defaultMemoryManager.reallocarray = uriparser_reallocarray;
345367
defaultMemoryManager.free = uriparser_free;
368+
369+
return SUCCESS;
346370
}
347371

348372
static uriparser_uris_t *uriparser_create_uris(

ext/uri/php_uriparser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ typedef struct uriparser_uris_t {
3030
zend_string *normalized_uri_str;
3131
} uriparser_uris_t;
3232

33-
void uriparser_module_init(void);
33+
PHP_MINIT_FUNCTION(uri_uriparser);
3434

3535
zend_result uriparser_read_userinfo(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval);
3636

0 commit comments

Comments
 (0)