common/sctp_crc32.c\
common/assert.cc \
common/debug.cc \
+ common/dyn_snprintf.c \
common/WorkQueue.cc \
common/ConfUtils.cc \
mon/MonMap.cc \
common/Clock.h\
common/common_init.h\
common/Cond.h\
+ common/dyn_snprintf.h\
common/ConfUtils.h\
common/DecayCounter.h\
common/Finisher.h\
#include <string>
#include "ConfUtils.h"
+#include "dyn_snprintf.h"
using namespace std;
return newname;
}
-#define MAX_LINE 2560
+#define MAX_LINE 256
static char *get_next_delim(char *str, const char *delim, int alloc, char **p)
{
char *name = NULL;
char *p;
int ret = 0;
- char line[MAX_LINE];
+ char *line;
+ size_t max_line = MAX_LINE;
char *start, *end;
p = start;
+ line = (char *)malloc(max_line);
line[0] ='\0';
do {
if (*name) {
if (*line)
- snprintf(line, MAX_LINE, "%s %s", line, name);
+ dyn_snprintf(&line, &max_line, 2, "%s %s", line, name);
else
- snprintf(line, MAX_LINE, "%s", name);
+ dyn_snprintf(&line, &max_line, 1, "%s", name);
}
} while (*name);
if (*line)
parsed->set_section(line);
+ free(line);
+
return ret;
}
{
SectionList::iterator sec_iter, sec_end;
ConfLine *cl;
- char line[MAX_LINE];
- int len = 0;
+ char *line;
+ size_t max_line = MAX_LINE;
+ size_t len;
char *p;
-
+
+ line = (char *)malloc(max_line);
sec_end=sections_list.end();
if (cl) {
line[0] = '\0';
- cl->output(line, MAX_LINE);
+ do {
+ if (len >= max_line) {
+ max_line *= 2;
+ free(line);
+ line = (char *)malloc(max_line);
+ }
+
+ len = cl->output(line, max_line);
+ } while (len == max_line);
::write(fd, line, strlen(line));
::write(fd, "\n", 1);
}
}
}
+
+ free(line);
}
void ConfFile::dump()
{
char *buf;
int len, i, l;
- char line[MAX_LINE];
+ char *line;
ConfLine *cl;
ConfSection *section = *psection;
int fd;
+ int max_line = MAX_LINE;
+
+ line = (char *)malloc(max_line);
+
+
fd = open(filename, O_RDWR);
if (fd < 0)
break;
default:
line[l++] = buf[i];
+
+ if (l == max_line-1) {
+ max_line *= 2;
+ line = (char *)realloc(line, max_line);
+ }
}
}
} while (len);
*psection = section;
+ free(line);
+
return 1;
}
--- /dev/null
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define MAXARGS 32
+
+
+#define CALL_SNPRINTF(buf, size, format, args) snprintf(buf, size, format, args[0], args[1], args[2], args[3], \
+ args[4], args[5], args[6], args[7], \
+ args[8], args[9], args[10], args[11], \
+ args[12], args[13], args[14], args[15], \
+ args[16], args[17], args[18], args[19], \
+ args[20], args[21], args[22], args[23], \
+ args[24], args[25], args[26], args[27], \
+ args[28], args[29], args[30], args[31])
+
+int dyn_snprintf(char **pbuf, size_t *pmax_size, int nargs, const char *format, ...)
+{
+ int ret;
+ va_list vl;
+ char *old_buf = *pbuf;
+ char *args[MAXARGS];
+ char *arg;
+ char *tmp_src = NULL;
+ int i;
+
+ if (nargs > MAXARGS)
+ return -1;
+
+ va_start(vl, format);
+ arg = va_arg(vl, char *);
+ for (i = 0; i<nargs; i++) {
+ if (arg == old_buf) {
+ if (!tmp_src) {
+ tmp_src = strdup(old_buf);
+ }
+ arg = tmp_src;
+ }
+ args[i] = arg;
+ arg = va_arg(vl, char *);
+ }
+ va_end(vl);
+ ret = CALL_SNPRINTF(*pbuf, *pmax_size, format, args);
+
+ if (ret >= *pmax_size) {
+ *pmax_size = ret * 2;
+ *pbuf = (char *)realloc(*pbuf, *pmax_size);
+ ret = CALL_SNPRINTF(*pbuf, *pmax_size, format, args);
+ }
+
+ return ret;
+}
+
--- /dev/null
+#ifndef __DYN_SNPRINTF_H
+#define __DYN_SNPRINTF_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int dyn_snprintf(char **pbuf, size_t *pmax_size, int nargs, const char *format, ...);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
#include "osd/osd_types.h"
#include "common/ConfUtils.h"
+#include "common/dyn_snprintf.h"
static bool show_config = false;
return "";
}
-#define MAX_LINE 2560
+#define MAX_LINE 256
#define MAX_VAR_LEN 32
char *conf_post_process_val(const char *val)
{
char var_name[MAX_VAR_LEN];
- char buf[MAX_LINE];
+ char *buf;
int i=0;
- int out_pos = 0;
+ size_t out_pos = 0;
+ size_t max_line = MAX_LINE;
+
+ buf = (char *)malloc(max_line);
- while (val[i] && (out_pos < MAX_LINE - 1)) {
+ while (val[i]) {
if (val[i] == '$') {
if (get_var(val, i+1, var_name, MAX_VAR_LEN, &i)) {
- out_pos += snprintf(buf+out_pos, MAX_LINE-out_pos, "%s", var_val(var_name));
+ out_pos = dyn_snprintf(&buf, &max_line, 2, "%s%s", buf, var_val(var_name));
} else {
++i;
}
} else {
+ if (out_pos == max_line - 1) {
+ max_line *= 2;
+ buf = (char *)realloc(buf, max_line);
+ }
buf[out_pos] = val[i];
+ buf[out_pos + 1] = '\0';
++out_pos;
++i;
}
buf[out_pos] = '\0';
- return strdup(buf);
+ return buf;
}
#define OPT_READ_TYPE(ret, section, var, type, out, def) \