Skip to content

Commit c216669

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 917837c commit c216669

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
@@ -31,6 +31,7 @@
3131
#include "php_ini.h"
3232

3333
#include "php_win32_globals.h"
34+
#include "../Zend/zend_string.h"
3435

3536
#include "ext/pcre/php_pcre.h"
3637
#include "ext/standard/php_string.h"
@@ -112,7 +113,7 @@ static const char *ErrorMessages[] =
112113
#define PHP_WIN32_MAIL_DOT_REPLACE "\n.."
113114

114115
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);
116+
zend_string *headers, zend_string *headers_lc, char **error_message);
116117
static int MailConnect();
117118
static int PostHeader(char *RPath, const char *Subject, const char *mailTo, char *xheaders);
118119
static bool Post(LPCSTR msg);
@@ -387,14 +388,14 @@ static char *find_address(char *list, char **state)
387388
// History:
388389
//*********************************************************************
389390
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)
391+
zend_string *headers, zend_string *headers_lc, char **error_message)
391392
{
392393
int res;
393394
char *p;
394395
char *tempMailTo, *token, *token_state;
395396
const char *pos1, *pos2;
396397
char *server_response = NULL;
397-
char *stripped_header = NULL;
398+
zend_string *stripped_header = NULL;
398399
zend_string *data_cln;
399400

400401
/* check for NULL parameters */
@@ -565,35 +566,36 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, const
565566

566567
/* Now that we've identified that we've a Bcc list,
567568
remove it from the current header. */
568-
stripped_header = ecalloc(1, ZSTR_LEN(headers));
569569
/* headers = point to string start of header
570570
pos1 = pointer IN headers where the Bcc starts
571571
'4' = Length of the characters 'bcc:'
572572
Because we've added +4 above for parsing the Emails
573573
we've to subtract them here. */
574-
memcpy(stripped_header, ZSTR_VAL(headers), pos1 - ZSTR_VAL(headers) - 4);
574+
size_t header_length_prior_to_bcc = pos1 - ZSTR_VAL(headers) - 4;
575575
if (pos1 != pos2) {
576576
/* if pos1 != pos2 , pos2 points to the rest of the headers.
577577
Since pos1 != pos2 if "\r\n" was found, we know those characters
578578
are there and so we jump over them (else we would generate a new header
579579
which would look like "\r\n\r\n". */
580-
memcpy(stripped_header + (pos1 - ZSTR_VAL(headers) - 4), pos2 + 2, strlen(pos2) - 2);
580+
stripped_header = zend_string_concat2(ZSTR_VAL(headers), header_length_prior_to_bcc, pos2 + 2, strlen(pos2) - 2);
581+
} else {
582+
stripped_header = zend_string_truncate(headers, header_length_prior_to_bcc, false);
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)