Skip to content

Commit bcfb102

Browse files
committed
More string length checks & fixes
1 parent ccb91cd commit bcfb102

File tree

14 files changed

+42
-33
lines changed

14 files changed

+42
-33
lines changed

ext/bz2/bz2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ static PHP_FUNCTION(bzcompress)
513513
dest_len = (unsigned int) (source_len + (0.01 * source_len) + 600);
514514

515515
/* Allocate the destination buffer */
516-
dest = emalloc(dest_len + 1);
516+
dest = safe_emalloc(dest_len, 1, 1);
517517

518518
/* Handle the optional arguments */
519519
if (argc > 1) {

ext/iconv/iconv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2491,7 +2491,7 @@ PHP_NAMED_FUNCTION(php_if_iconv)
24912491
&out_buffer, &out_len, out_charset, in_charset);
24922492
_php_iconv_show_error(err, out_charset, in_charset TSRMLS_CC);
24932493
if (err == PHP_ICONV_ERR_SUCCESS && out_buffer != NULL) {
2494-
RETVAL_STRINGL(out_buffer, out_len, 0);
2494+
RETVAL_STRINGL_CHECK(out_buffer, out_len, 0);
24952495
} else {
24962496
if (out_buffer != NULL) {
24972497
efree(out_buffer);

ext/imap/php_imap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3916,7 +3916,7 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char *
39163916
#define PHP_IMAP_CLEAN if (bufferTo) efree(bufferTo); if (bufferCc) efree(bufferCc); if (bufferBcc) efree(bufferBcc); if (bufferHeader) efree(bufferHeader);
39173917
#define PHP_IMAP_BAD_DEST PHP_IMAP_CLEAN; efree(tempMailTo); return (BAD_MSG_DESTINATION);
39183918

3919-
bufferHeader = (char *)emalloc(bufferLen + 1);
3919+
bufferHeader = (char *)safe_emalloc(bufferLen, 1, 1);
39203920
memset(bufferHeader, 0, bufferLen);
39213921
if (to && *to) {
39223922
strlcat(bufferHeader, "To: ", bufferLen + 1);

ext/intl/breakiterator/breakiterator_iterators.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static void _breakiterator_parts_move_forward(zend_object_iterator *iter TSRMLS_
182182
}
183183
assert(next <= slen && next >= cur);
184184
len = next - cur;
185-
res = static_cast<char*>(emalloc(len + 1));
185+
res = static_cast<char*>(safe_emalloc(len, 1, 1));
186186

187187
memcpy(res, &s[cur], len);
188188
res[len] = '\0';

ext/intl/intl_convert.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void intl_convert_utf8_to_utf16(
4949
UErrorCode* status )
5050
{
5151
UChar* dst_buf = NULL;
52-
int32_t dst_len = 0;
52+
uint32_t dst_len = 0;
5353

5454
/* If *target is NULL determine required destination buffer size (pre-flighting).
5555
* Otherwise, attempt to convert source string; if *target buffer is not large enough

ext/intl/locale/locale_methods.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ static char* get_icu_value_internal( const char* loc_name , char* tag_name, int*
263263
int32_t buflen = 512;
264264
UErrorCode status = U_ZERO_ERROR;
265265

266+
if (strlen(loc_name) > INTL_MAX_LOCALE_LEN) {
267+
return NULL;
268+
}
266269

267270
if( strcmp(tag_name, LOC_CANONICALIZE_TAG) != 0 ){
268271
/* Handle grandfathered languages */
@@ -395,7 +398,7 @@ static void get_icu_value_src_php( char* tag_name, INTERNAL_FUNCTION_PARAMETERS)
395398
if(loc_name_len == 0) {
396399
loc_name = intl_locale_get_default(TSRMLS_C);
397400
}
398-
401+
399402
INTL_CHECK_LOCALE_LEN(strlen(loc_name));
400403

401404
/* Call ICU get */
@@ -702,6 +705,8 @@ PHP_FUNCTION( locale_get_keywords )
702705
RETURN_FALSE;
703706
}
704707

708+
INTL_CHECK_LOCALE_LEN(strlen(loc_name));
709+
705710
if(loc_name_len == 0) {
706711
loc_name = intl_locale_get_default(TSRMLS_C);
707712
}
@@ -1109,6 +1114,8 @@ PHP_FUNCTION(locale_parse)
11091114
RETURN_FALSE;
11101115
}
11111116

1117+
INTL_CHECK_LOCALE_LEN(strlen(loc_name));
1118+
11121119
if(loc_name_len == 0) {
11131120
loc_name = intl_locale_get_default(TSRMLS_C);
11141121
}

ext/intl/msgformat/msgformat_data.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ msgformat_data* msgformat_data_create( TSRMLS_D )
8080
/* }}} */
8181

8282
#ifdef MSG_FORMAT_QUOTE_APOS
83-
int msgformat_fix_quotes(UChar **spattern, uint32_t *spattern_len, UErrorCode *ec)
83+
int msgformat_fix_quotes(UChar **spattern, uint32_t *spattern_len, UErrorCode *ec)
8484
{
8585
if(*spattern && *spattern_len && u_strchr(*spattern, (UChar)'\'')) {
86-
UChar *npattern = emalloc(sizeof(UChar)*(2*(*spattern_len)+1));
86+
UChar *npattern = safe_emalloc(sizeof(UChar)*2, *spattern_len, sizeof(UChar));
8787
uint32_t npattern_len;
8888
npattern_len = umsg_autoQuoteApostrophe(*spattern, *spattern_len, npattern, 2*(*spattern_len)+1, ec);
8989
efree(*spattern);

ext/standard/exec.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ PHPAPI int php_exec(int type, char *cmd, zval *array, zval *return_value TSRMLS_
133133

134134
if (type != 3) {
135135
b = buf;
136-
136+
137137
while (php_stream_get_line(stream, b, EXEC_INPUT_BUF, &bufl)) {
138138
/* no new line found, let's read some more */
139139
if (b[bufl - 1] != '\n' && !php_stream_eof(stream)) {
@@ -330,7 +330,7 @@ PHPAPI char *php_escape_shell_cmd(char *str)
330330
cmd[y++] = str[x];
331331
break;
332332
#else
333-
/* % is Windows specific for enviromental variables, ^%PATH% will
333+
/* % is Windows specific for enviromental variables, ^%PATH% will
334334
output PATH while ^%PATH^% will not. escapeshellcmd will escape all % and !.
335335
*/
336336
case '%':
@@ -492,7 +492,7 @@ PHP_FUNCTION(escapeshellcmd)
492492
return;
493493
}
494494
cmd = php_escape_shell_cmd(command);
495-
RETVAL_STRING(cmd, 0);
495+
RETVAL_STRINGL_CHECK(cmd, strlen(cmd), 0);
496496
} else {
497497
RETVAL_EMPTY_STRING();
498498
}
@@ -517,7 +517,7 @@ PHP_FUNCTION(escapeshellarg)
517517
return;
518518
}
519519
cmd = php_escape_shell_arg(argument);
520-
RETVAL_STRING(cmd, 0);
520+
RETVAL_STRINGL_CHECK(cmd, strlen(cmd), 0);
521521
}
522522
}
523523
/* }}} */
@@ -551,7 +551,7 @@ PHP_FUNCTION(shell_exec)
551551
php_stream_close(stream);
552552

553553
if (total_readbytes > 0) {
554-
RETVAL_STRINGL(ret, total_readbytes, 0);
554+
RETVAL_STRINGL_CHECK(ret, total_readbytes, 0);
555555
}
556556
}
557557
/* }}} */

ext/standard/php_smart_str.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
newlen = (n); \
5858
(d)->a = newlen < SMART_STR_START_SIZE \
5959
? SMART_STR_START_SIZE \
60-
: newlen + SMART_STR_PREALLOC; \
60+
: (newlen >= (INT_MAX - SMART_STR_PREALLOC)? newlen \
61+
: (newlen + SMART_STR_PREALLOC)); \
6162
SMART_STR_DO_REALLOC(d, what); \
6263
} else { \
6364
newlen = (d)->len + (n); \

ext/standard/string.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -908,11 +908,7 @@ PHP_FUNCTION(wordwrap)
908908
RETURN_FALSE;
909909
}
910910

911-
if (linelength < 0) {
912-
/* For BC */
913-
linelength = 0;
914-
}
915-
if (linelength > INT_MAX) {
911+
if (linelength < 0 || linelength > INT_MAX) {
916912
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length should be between 0 and %d", INT_MAX);
917913
RETURN_FALSE;
918914
}

0 commit comments

Comments
 (0)