]> granicus.if.org Git - php/commitdiff
Fixed issues inside str_pad() identified by bug #52550
authorIlia Alshanetsky <iliaa@php.net>
Fri, 6 Aug 2010 19:55:10 +0000 (19:55 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Fri, 6 Aug 2010 19:55:10 +0000 (19:55 +0000)
ext/standard/string.c

index 5474c406b0b6f988e6f97f08dbcc0d44a897ae29..51bb611bd7776dd058356bb15ff370ca1644dd01 100644 (file)
@@ -4861,7 +4861,7 @@ PHP_FUNCTION(str_pad)
        long pad_length;                        /* Length to pad to */
        
        /* Helper variables */
-       int        num_pad_chars;               /* Number of padding characters (total - input size) */
+       size_t     num_pad_chars;               /* Number of padding characters (total - input size) */
        char  *result = NULL;           /* Resulting string */
        int        result_len = 0;              /* Length of the resulting string */
        char  *pad_str_val = " ";       /* Pointer to padding string */
@@ -4874,11 +4874,9 @@ PHP_FUNCTION(str_pad)
                return;
        }
 
-       num_pad_chars = pad_length - input_len;
-
        /* If resulting string turns out to be shorter than input string,
           we simply copy the input and return. */
-       if (pad_length <= 0 || num_pad_chars <= 0) {
+       if (pad_length <= 0 || (pad_length - input_len) <= 0) {
                RETURN_STRINGL(input, input_len, 1);
        }
 
@@ -4892,6 +4890,11 @@ PHP_FUNCTION(str_pad)
                return;
        }
 
+       num_pad_chars = pad_length - input_len;
+       if (num_pad_chars >= INT_MAX) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Padding length is too long");
+               return; 
+       }
        result = (char *)emalloc(input_len + num_pad_chars + 1);
 
        /* We need to figure out the left/right padding lengths. */