]> granicus.if.org Git - php/commitdiff
Work around -Walloc-size-larger-than bug
authorNikita Popov <nikita.ppv@gmail.com>
Thu, 11 Apr 2019 15:44:20 +0000 (17:44 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Thu, 11 Apr 2019 15:44:20 +0000 (17:44 +0200)
sapi/phpdbg/phpdbg_eol.c

index 50e0056fa831909ef79acd9e6273cf79f124bd78..dc4e07c76c8ffa041274d42cfdfa369efdbe8ee4 100644 (file)
@@ -81,27 +81,30 @@ char *phpdbg_eol_rep(int id)
        return NULL;
 }
 
+/* Marked as never_inline to work around a -Walloc-size-larger-than bug in GCC. */
+static zend_never_inline int count_lf_and_cr(const char *in, int in_len) {
+       int i, count = 0;
+       for (i = 0; i < in_len; i++) {
+               if (0x0a == in[i] || 0x0d == in[i]) {
+                       count++;
+               }
+       }
+       return count;
+}
 
 /* Inspired by https://ccrma.stanford.edu/~craig/utility/flip/flip.cpp */
 void phpdbg_eol_convert(char **str, int *len)
 {
-       char *in = *str, *out ;
-       int in_len = *len, out_len, cursor, i;
+       char *in = *str, *out;
+       int in_len = *len, cursor, i;
        char last, cur;
 
        if ((PHPDBG_G(flags) & PHPDBG_IS_REMOTE) != PHPDBG_IS_REMOTE) {
                return;
        }
 
-       out_len = *len;
        if (PHPDBG_EOL_CRLF == PHPDBG_G(eol)) { /* XXX add LFCR case if it's gonna be needed */
-               /* depending on the source EOL the out str will have all CR/LF duplicated */
-               for (i = 0; i < in_len; i++) {
-                       if (0x0a == in[i] || 0x0d == in[i]) {
-                               out_len++;
-                       }
-               }
-               out = (char *)emalloc(out_len);
+               out = (char *)emalloc(in_len + count_lf_and_cr(in, in_len));
 
                last = cur = in[0];
                i = cursor = 0;
@@ -142,7 +145,7 @@ void phpdbg_eol_convert(char **str, int *len)
                }
 
                /* We gonna have a smaller or equally long string, estimation is almost neglecting */
-               out = (char *)emalloc(out_len);
+               out = (char *)emalloc(in_len);
 
                last = cur = in[0];
                i = cursor = 0;