]> granicus.if.org Git - php/commitdiff
Fixed a buffer overflow that occurs when wordwrap is unable to calculate
authorIlia Alshanetsky <iliaa@php.net>
Thu, 5 Sep 2002 14:00:28 +0000 (14:00 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Thu, 5 Sep 2002 14:00:28 +0000 (14:00 +0000)
the correct number of times the multi-byte break needs to be inserted into
the string.

ext/standard/string.c

index 544d6237690b27c9eb8a48756427253e9860af7b..ed97392740df558b336741c25871a6200de677a7 100644 (file)
@@ -640,13 +640,14 @@ PHP_FUNCTION(wordwrap)
        else {
                /* Multiple character line break or forced cut */
                if (linelength > 0) {
-                       newtextlen = textlen + (textlen/linelength + 1) * breakcharlen + 1;
+                       /* Add extra 10% to accomodate strings with unpredicatable number of breaks */
+                       newtextlen = textlen + (textlen/linelength + 1) * breakcharlen * 1.1 + 1;
                }
                else {
                        newtextlen = textlen * (breakcharlen + 1) + 1;
                }
                newtext = emalloc(newtextlen);
-
+       
                /* now keep track of the actual new text length */
                newtextlen = 0;
 
@@ -705,6 +706,8 @@ PHP_FUNCTION(wordwrap)
                }
 
                newtext[newtextlen] = '\0';
+               /* free unused memory */
+               newtext = erealloc(newtext, newtextlen+1);
 
                RETURN_STRINGL(newtext, newtextlen, 0);
        }