Skip to content

Commit fc276ae

Browse files
committed
win32/sendmail.c/SendText(): use zend_string for stripped_headers
This prevents some copying and is in preparation of other refactoring preventing strlen() recomputations
1 parent 8aa64bb commit fc276ae

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

win32/sendmail.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static const char *ErrorMessages[] =
112112
#define PHP_WIN32_MAIL_DOT_REPLACE "\n.."
113113

114114
static int SendText(char *RPath, const char *Subject, const char *mailTo, const char *data,
115-
const zend_string *headers, zend_string *headers_lc, char **error_message);
115+
zend_string *headers, zend_string *headers_lc, char **error_message);
116116
static int MailConnect();
117117
static int PostHeader(char *RPath, const char *Subject, const char *mailTo, char *xheaders);
118118
static bool Post(LPCSTR msg);
@@ -387,14 +387,14 @@ static char *find_address(char *list, char **state)
387387
// History:
388388
//*********************************************************************
389389
static int SendText(char *RPath, const char *Subject, const char *mailTo, const char *data,
390-
const zend_string *headers, zend_string *headers_lc, char **error_message)
390+
zend_string *headers, zend_string *headers_lc, char **error_message)
391391
{
392392
int res;
393393
char *p;
394394
char *tempMailTo, *token, *token_state;
395395
const char *pos1, *pos2;
396396
char *server_response = NULL;
397-
char *stripped_header = NULL;
397+
zend_string *stripped_header = NULL;
398398
zend_string *data_cln;
399399

400400
/* check for NULL parameters */
@@ -565,35 +565,37 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, const
565565

566566
/* Now that we've identified that we've a Bcc list,
567567
remove it from the current header. */
568-
stripped_header = ecalloc(1, ZSTR_LEN(headers));
569568
/* headers = point to string start of header
570569
pos1 = pointer IN headers where the Bcc starts
571570
'4' = Length of the characters 'bcc:'
572571
Because we've added +4 above for parsing the Emails
573572
we've to subtract them here. */
574-
memcpy(stripped_header, ZSTR_VAL(headers), pos1 - ZSTR_VAL(headers) - 4);
573+
size_t header_length_prior_to_bcc = pos1 - ZSTR_VAL(headers) - 4;
575574
if (pos1 != pos2) {
576575
/* if pos1 != pos2 , pos2 points to the rest of the headers.
577576
Since pos1 != pos2 if "\r\n" was found, we know those characters
578577
are there and so we jump over them (else we would generate a new header
579578
which would look like "\r\n\r\n". */
580-
memcpy(stripped_header + (pos1 - ZSTR_VAL(headers) - 4), pos2 + 2, strlen(pos2) - 2);
579+
stripped_header = zend_string_concat2(ZSTR_VAL(headers), header_length_prior_to_bcc, pos2 + 2, strlen(pos2) - 2);
580+
} else {
581+
stripped_header = zend_string_truncate(headers, header_length_prior_to_bcc, false);
582+
ZSTR_VAL(stripped_header)[ZSTR_LEN(stripped_header)] = '\0';
581583
}
582584
} else {
583585
/* Simplify the code that we create a copy of stripped_header no matter if
584-
we actually strip something or not. So we've a single efree() later. */
585-
stripped_header = estrndup(ZSTR_VAL(headers), ZSTR_LEN(headers));
586+
we actually strip something or not. So we've a single zend_string_release() later. */
587+
stripped_header = zend_string_copy(headers);
586588
}
587589
}
588590

589591
/* send message header */
590592
if (Subject == NULL) {
591-
res = PostHeader(RPath, "No Subject", mailTo, stripped_header);
593+
res = PostHeader(RPath, "No Subject", mailTo, stripped_header ? ZSTR_VAL(stripped_header) : NULL);
592594
} else {
593-
res = PostHeader(RPath, Subject, mailTo, stripped_header);
595+
res = PostHeader(RPath, Subject, mailTo, stripped_header ? ZSTR_VAL(stripped_header) : NULL);
594596
}
595597
if (stripped_header) {
596-
efree(stripped_header);
598+
zend_string_release_ex(stripped_header, false);
597599
}
598600
if (res != SUCCESS) {
599601
return (res);

0 commit comments

Comments
 (0)