]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: standard: The function parse_binary() can use preallocated buffer
authorThierry FOURNIER <tfournier@exceliance.fr>
Fri, 6 Dec 2013 18:59:28 +0000 (19:59 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 12 Dec 2013 14:42:11 +0000 (15:42 +0100)
Let the function support pre-allocated buffers if the argument is not null,
or allocate its own buffer if it is null.

src/standard.c

index 72fd0d6d427f8e1c9bc13387e27e7babfd13c22e..3ed1e2aed26edee3a68e43461a9692d4bfef937d 100644 (file)
@@ -1367,6 +1367,7 @@ int parse_binary(const char *source, char **binstr, int *binstrlen, char **err)
        int len;
        const char *p = source;
        int i,j;
+       int alloc;
 
        len = strlen(source);
        if (len % 2) {
@@ -1375,12 +1376,24 @@ int parse_binary(const char *source, char **binstr, int *binstrlen, char **err)
        }
 
        len = len >> 1;
-       *binstrlen = len;
-       *binstr = calloc(len, sizeof(char));
+
        if (!*binstr) {
-               memprintf(err, "out of memory while loading string pattern");
-               return 0;
+               *binstr = calloc(len, sizeof(char));
+               if (!*binstr) {
+                       memprintf(err, "out of memory while loading string pattern");
+                       return 0;
+               }
+               alloc = 1;
        }
+       else {
+               if (*binstrlen < len) {
+                       memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
+                                 len, *binstrlen);
+                       return 0;
+               }
+               alloc = 0;
+       }
+       *binstrlen = len;
 
        i = j = 0;
        while (j < len) {
@@ -1394,7 +1407,8 @@ int parse_binary(const char *source, char **binstr, int *binstrlen, char **err)
 
 bad_input:
        memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
-       free(binstr);
+       if (alloc)
+               free(binstr);
        return 0;
 }