int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
int chunk_strcmp(const struct chunk *chk, const char *str);
int chunk_strcasecmp(const struct chunk *chk, const char *str);
-int alloc_trash_buffers(int bufsize);
-void free_trash_buffers(void);
struct chunk *get_trash_chunk(void);
struct chunk *alloc_trash_chunk(void);
+int init_trash_buffers(void);
+void deinit_trash_buffers(void);
/*
* free a trash chunk allocated by alloc_trash_chunk(). NOP on NULL.
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
- chunk_init(&trash, realloc(trash.str, global.tune.bufsize), global.tune.bufsize);
- alloc_trash_buffers(global.tune.bufsize);
+ if (!init_trash_buffers()) {
+ Alert("parsing [%s:%d] : failed to initialize trash buffers.\n", file, linenum);
+ err_code |= ERR_ALERT | ERR_FATAL;
+ goto out;
+ }
}
else if (!strcmp(args[0], "tune.maxrewrite")) {
if (alertif_too_many_args(1, file, linenum, args, &err_code))
#include <common/chunk.h>
#include <common/standard.h>
+#include <types/global.h>
+
/* trash chunks used for various conversions */
static struct chunk *trash_chunk;
static struct chunk trash_chunk1;
/* the trash pool for reentrant allocations */
struct pool_head *pool2_trash = NULL;
+/* this is used to drain data, and as a temporary buffer for sprintf()... */
+struct chunk trash = { .str = NULL };
+
/*
* Returns a pre-allocated and initialized trash chunk that can be used for any
* type of conversion. Two chunks and their respective buffers are alternatively
/* (re)allocates the trash buffers. Returns 0 in case of failure. It is
* possible to call this function multiple times if the trash size changes.
*/
-int alloc_trash_buffers(int bufsize)
+static int alloc_trash_buffers(int bufsize)
{
trash_size = bufsize;
trash_buf1 = (char *)my_realloc2(trash_buf1, bufsize);
return trash_buf1 && trash_buf2 && pool2_trash;
}
+/* Initialize the trash buffers. It returns 0 if an error occurred. */
+int init_trash_buffers()
+{
+ chunk_init(&trash, my_realloc2(trash.str, global.tune.bufsize), global.tune.bufsize);
+ if (!trash.str || !alloc_trash_buffers(global.tune.bufsize))
+ return 0;
+ return 1;
+}
+
/*
* free the trash buffers
*/
-void free_trash_buffers(void)
+void deinit_trash_buffers(void)
{
+ chunk_destroy(&trash);
free(trash_buf2);
free(trash_buf1);
trash_buf2 = NULL;
static char *cur_unixsocket = NULL;
-/* this is used to drain data, and as a temporary buffer for sprintf()... */
-struct chunk trash = { };
-
/* this buffer is always the same size as standard buffers and is used for
* swapping data inside a buffer.
*/
next_argv = copy_argv(argc, argv);
- chunk_init(&trash, malloc(global.tune.bufsize), global.tune.bufsize);
- alloc_trash_buffers(global.tune.bufsize);
+ if (!init_trash_buffers()) {
+ Alert("failed to initialize trash buffers.\n");
+ exit(1);
+ }
/* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
* the string in case of truncation, and at least FreeBSD appears not to do
cfg_unregister_sections();
- free_trash_buffers();
- chunk_destroy(&trash);
+ deinit_trash_buffers();
protocol_unbind_all();