Let the function support pre-allocated buffers if the argument is not null,
or allocate its own buffer if it is null.
int len;
const char *p = source;
int i,j;
+ int alloc;
len = strlen(source);
if (len % 2) {
}
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) {
bad_input:
memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
- free(binstr);
+ if (alloc)
+ free(binstr);
return 0;
}