Skip to content

Commit 4605d53

Browse files
committed
Fixed bug #69364 - use smart_str to assemble strings
1 parent c27f012 commit 4605d53

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

main/rfc1867.c

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "php_variables.h"
3434
#include "rfc1867.h"
3535
#include "ext/standard/php_string.h"
36+
#include "ext/standard/php_smart_str.h"
3637

3738
#define DEBUG_FILE_UPLOAD ZEND_DEBUG
3839

@@ -398,8 +399,9 @@ static int find_boundary(multipart_buffer *self, char *boundary TSRMLS_DC)
398399
static int multipart_buffer_headers(multipart_buffer *self, zend_llist *header TSRMLS_DC)
399400
{
400401
char *line;
401-
mime_header_entry prev_entry = {0}, entry;
402-
int prev_len, cur_len;
402+
mime_header_entry entry = {0};
403+
smart_str buf_value = {0};
404+
char *key = NULL;
403405

404406
/* didn't find boundary, abort */
405407
if (!find_boundary(self, self->boundary TSRMLS_CC)) {
@@ -411,11 +413,10 @@ static int multipart_buffer_headers(multipart_buffer *self, zend_llist *header T
411413
while( (line = get_line(self TSRMLS_CC)) && strlen(line) > 0 )
412414
{
413415
/* add header to table */
414-
char *key = line;
415416
char *value = NULL;
416417

417418
if (php_rfc1867_encoding_translation(TSRMLS_C)) {
418-
self->input_encoding = zend_multibyte_encoding_detector(line, strlen(line), self->detect_order, self->detect_order_size TSRMLS_CC);
419+
self->input_encoding = zend_multibyte_encoding_detector((unsigned char *)line, strlen(line), self->detect_order, self->detect_order_size TSRMLS_CC);
419420
}
420421

421422
/* space in the beginning means same header */
@@ -424,31 +425,33 @@ static int multipart_buffer_headers(multipart_buffer *self, zend_llist *header T
424425
}
425426

426427
if (value) {
427-
*value = 0;
428-
do { value++; } while(isspace(*value));
429-
430-
entry.value = estrdup(value);
431-
entry.key = estrdup(key);
432-
433-
} else if (zend_llist_count(header)) { /* If no ':' on the line, add to previous line */
434-
435-
prev_len = strlen(prev_entry.value);
436-
cur_len = strlen(line);
437-
438-
entry.value = emalloc(prev_len + cur_len + 1);
439-
memcpy(entry.value, prev_entry.value, prev_len);
440-
memcpy(entry.value + prev_len, line, cur_len);
441-
entry.value[cur_len + prev_len] = '\0';
428+
if(buf_value.c && key) {
429+
/* new entry, add the old one to the list */
430+
smart_str_0(&buf_value);
431+
entry.key = key;
432+
entry.value = buf_value.c;
433+
zend_llist_add_element(header, &entry);
434+
buf_value.c = NULL;
435+
key = NULL;
436+
}
442437

443-
entry.key = estrdup(prev_entry.key);
438+
*value = '\0';
439+
do { value++; } while(isspace(*value));
444440

445-
zend_llist_remove_tail(header);
441+
key = estrdup(line);
442+
smart_str_appends(&buf_value, value);
443+
} else if (buf_value.c) { /* If no ':' on the line, add to previous line */
444+
smart_str_appends(&buf_value, line);
446445
} else {
447446
continue;
448447
}
449-
448+
}
449+
if(buf_value.c && key) {
450+
/* add the last one to the list */
451+
smart_str_0(&buf_value);
452+
entry.key = key;
453+
entry.value = buf_value.c;
450454
zend_llist_add_element(header, &entry);
451-
prev_entry = entry;
452455
}
453456

454457
return 1;
@@ -884,7 +887,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
884887
if (count == PG(max_input_vars) + 1) {
885888
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input variables exceeded %ld. To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
886889
}
887-
890+
888891
if (php_rfc1867_callback != NULL) {
889892
multipart_event_formdata event_formdata;
890893

0 commit comments

Comments
 (0)