execute_external_filter(): Don't rely on undefined malloc() behaviour
authorFabian Keil <fk@fabiankeil.de>
Wed, 12 Aug 2015 10:34:21 +0000 (10:34 +0000)
committerFabian Keil <fk@fabiankeil.de>
Wed, 12 Aug 2015 10:34:21 +0000 (10:34 +0000)
... and fix the read buffer scaling for initial sizes below READ_LENGTH.

Could fix the crash reported by Jonathan McKenzie on ijbswa-users@

filters.c

index 1fc1c47..d3dcaeb 100644 (file)
--- a/filters.c
+++ b/filters.c
@@ -1,4 +1,4 @@
-const char filters_rcs[] = "$Id: filters.c,v 1.191 2014/10/18 11:28:36 fabiankeil Exp $";
+const char filters_rcs[] = "$Id: filters.c,v 1.192 2014/10/18 11:30:24 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/filters.c,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/filters.c,v $
@@ -1871,7 +1871,8 @@ static char *execute_external_filter(const struct client_state *csp,
       return NULL;
    }
 
       return NULL;
    }
 
-   filter_output = malloc_or_die(*size);
+   /* Allocate at least one byte */
+   filter_output = malloc_or_die(*size + 1);
 
    new_size = 0;
    while (!feof(fp) && !ferror(fp))
 
    new_size = 0;
    while (!feof(fp) && !ferror(fp))
@@ -1885,7 +1886,7 @@ static char *execute_external_filter(const struct client_state *csp,
          char *p;
 
          /* Could be considered wasteful if the content is 'large'. */
          char *p;
 
          /* Could be considered wasteful if the content is 'large'. */
-         *size = (*size != 0) ? *size * 2 : READ_LENGTH;
+         *size = (*size > READ_LENGTH) ? *size * 2 : READ_LENGTH;
 
          p = realloc(filter_output, *size);
          if (p == NULL)
 
          p = realloc(filter_output, *size);
          if (p == NULL)