* This means that <err> must be initialized to NULL before first invocation.
* The return value also holds the allocated string, which eases error checking
* and immediate consumption. If the output pointer is not used, NULL must be
- * passed instead and it will be ignored.
+ * passed instead and it will be ignored. The returned message will then also
+ * be NULL so that the caller does not have to bother with freeing anything.
*
* It is also convenient to use it without any free except the last one :
* err = NULL;
node = calloc(1, sizeof(*node) + len + 1);
if (!node) {
- if (err)
- memprintf(err, "out of memory while loading string pattern");
+ memprintf(err, "out of memory while loading string pattern");
return 0;
}
memcpy(node->key, *text, len + 1);
pattern->ptr.str = strdup(*text);
if (!pattern->ptr.str) {
- if (err)
- memprintf(err, "out of memory while loading string pattern");
+ memprintf(err, "out of memory while loading string pattern");
return 0;
}
pattern->len = len;
pattern->type = SMP_T_CSTR;
pattern->ptr.str = s = calloc(1, len);
if (!pattern->ptr.str) {
- if (err)
- memprintf(err, "out of memory while loading pattern");
+ memprintf(err, "out of memory while loading pattern");
return 0;
}
preg = calloc(1, sizeof(regex_t));
if (!preg) {
- if (err)
- memprintf(err, "out of memory while loading pattern");
+ memprintf(err, "out of memory while loading pattern");
return 0;
}
icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
free(preg);
- if (err)
- memprintf(err, "regex '%s' is invalid", *text);
+ memprintf(err, "regex '%s' is invalid", *text);
return 0;
}
case STD_OP_LT: *opaque = 3; break;
case STD_OP_LE: *opaque = 4; break;
default:
- if (err)
- memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
+ memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
return 0;
}
if (last && *opaque >= 1 && *opaque <= 4) {
/* having a range with a min or a max is absurd */
- if (err)
- memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
+ memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
return 0;
}
case STD_OP_LT: *opaque = 3; break;
case STD_OP_LE: *opaque = 4; break;
default:
- if (err)
- memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
+ memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
return 0;
}
if (last && *opaque >= 1 && *opaque <= 4) {
/* having a range with a min or a max is absurd */
- if (err)
- memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
+ memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
return 0;
}
/* FIXME: insert <addr>/<mask> into the tree here */
node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
if (!node) {
- if (err)
- memprintf(err, "out of memory while loading IPv4 pattern");
+ memprintf(err, "out of memory while loading IPv4 pattern");
return 0;
}
memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
return 1;
}
else {
- if (err)
- memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
+ memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
return 0;
}
}
aclkw = find_acl_kw(args[0]);
if (!aclkw || !aclkw->parse) {
- if (err)
- memprintf(err, "unknown ACL keyword '%s'", *args);
+ memprintf(err, "unknown ACL keyword '%s'", *args);
goto out_return;
}
expr = (struct acl_expr *)calloc(1, sizeof(*expr));
if (!expr) {
- if (err)
- memprintf(err, "out of memory when parsing ACL expression");
+ memprintf(err, "out of memory when parsing ACL expression");
goto out_return;
}
arg++;
end = strchr(arg, ')');
if (!end) {
- if (err)
- memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", aclkw->kw);
+ memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", aclkw->kw);
goto out_free_expr;
}
err, NULL, NULL);
if (nbargs < 0) {
/* note that make_arg_list will have set <err> here */
- if (err)
- memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
+ memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
goto out_free_expr;
}
/* invalid keyword argument, error must have been
* set by val_args().
*/
- if (err)
- memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
+ memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
goto out_free_expr;
}
}
* the current one later.
*/
if (type != ARGT_FE && type != ARGT_BE && type != ARGT_TAB) {
- if (err)
- memprintf(err, "ACL keyword '%s' expects %d arguments", aclkw->kw, ARGM(aclkw->arg_mask));
+ memprintf(err, "ACL keyword '%s' expects %d arguments", aclkw->kw, ARGM(aclkw->arg_mask));
goto out_free_expr;
}
}
else if (ARGM(aclkw->arg_mask)) {
/* there were some mandatory arguments */
- if (err)
- memprintf(err, "ACL keyword '%s' expects %d arguments", aclkw->kw, ARGM(aclkw->arg_mask));
+ memprintf(err, "ACL keyword '%s' expects %d arguments", aclkw->kw, ARGM(aclkw->arg_mask));
goto out_free_expr;
}
}
else {
if (arg) {
/* no argument expected */
- if (err)
- memprintf(err, "ACL keyword '%s' takes no argument", aclkw->kw);
+ memprintf(err, "ACL keyword '%s' takes no argument", aclkw->kw);
goto out_free_expr;
}
}
int ret;
pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
if (!pattern) {
- if (err)
- memprintf(err, "out of memory when parsing ACL pattern");
+ memprintf(err, "out of memory when parsing ACL pattern");
goto out_free_expr;
}
pattern->flags = patflags;
const char *pos;
if (**args && (pos = invalid_char(*args))) {
- if (err)
- memprintf(err, "invalid character in ACL name : '%c'", *pos);
+ memprintf(err, "invalid character in ACL name : '%c'", *pos);
goto out_return;
}
if (!cur_acl) {
name = strdup(args[0]);
if (!name) {
- if (err)
- memprintf(err, "out of memory when parsing ACL");
+ memprintf(err, "out of memory when parsing ACL");
goto out_free_acl_expr;
}
cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
if (cur_acl == NULL) {
- if (err)
- memprintf(err, "out of memory when parsing ACL");
+ memprintf(err, "out of memory when parsing ACL");
goto out_free_name;
}
}
if (default_acl_list[index].name == NULL) {
- if (err)
- memprintf(err, "no such ACL : '%s'", acl_name);
+ memprintf(err, "no such ACL : '%s'", acl_name);
return NULL;
}
name = strdup(acl_name);
if (!name) {
- if (err)
- memprintf(err, "out of memory when building default ACL '%s'", acl_name);
+ memprintf(err, "out of memory when building default ACL '%s'", acl_name);
goto out_free_acl_expr;
}
cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
if (cur_acl == NULL) {
- if (err)
- memprintf(err, "out of memory when building default ACL '%s'", acl_name);
+ memprintf(err, "out of memory when building default ACL '%s'", acl_name);
goto out_free_name;
}
cond = (struct acl_cond *)calloc(1, sizeof(*cond));
if (cond == NULL) {
- if (err)
- memprintf(err, "out of memory when parsing condition");
+ memprintf(err, "out of memory when parsing condition");
goto out_return;
}
arg_end++;
if (!*args[arg_end]) {
- if (err)
- memprintf(err, "missing closing '}' in condition");
+ memprintf(err, "missing closing '}' in condition");
goto out_free_suite;
}
args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
if (!args_new) {
- if (err)
- memprintf(err, "out of memory when parsing condition");
+ memprintf(err, "out of memory when parsing condition");
goto out_free_suite;
}
cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
if (cur_term == NULL) {
- if (err)
- memprintf(err, "out of memory when parsing condition");
+ memprintf(err, "out of memory when parsing condition");
goto out_free_suite;
}
if (!cur_suite) {
cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
if (cur_term == NULL) {
- if (err)
- memprintf(err, "out of memory when parsing condition");
+ memprintf(err, "out of memory when parsing condition");
goto out_free_term;
}
LIST_INIT(&cur_suite->terms);
args++;
}
else {
- if (err)
- memprintf(err, "conditions must start with either 'if' or 'unless'");
+ memprintf(err, "conditions must start with either 'if' or 'unless'");
return NULL;
}
if (pos < min_arg) {
/* not enough arguments */
- if (err_msg)
- memprintf(err_msg,
- "Missing arguments (got %d/%d), type '%s' expected",
- pos, min_arg, arg_type_names[(mask >> (pos * 4)) & 15]);
+ memprintf(err_msg,
+ "Missing arguments (got %d/%d), type '%s' expected",
+ pos, min_arg, arg_type_names[(mask >> (pos * 4)) & 15]);
goto err;
}
if (len) {
/* too many arguments, starting at <in> */
- if (err_msg) {
- /* the caller is responsible for freeing this message */
- word = my_strndup(in, len);
- memprintf(err_msg, "end of arguments expected at position %d, but got '%s'",
- pos + 1, word);
- free(word); word = NULL;
- }
+ /* the caller is responsible for freeing this message */
+ word = my_strndup(in, len);
+ memprintf(err_msg, "end of arguments expected at position %d, but got '%s'",
+ pos + 1, word);
+ free(word); word = NULL;
goto err;
}
return -1;
empty_err:
- if (err_msg) {
- memprintf(err_msg, "expected type '%s' at position %d, but got nothing",
- arg_type_names[(mask >> (pos * 4)) & 15], pos + 1);
- }
+ memprintf(err_msg, "expected type '%s' at position %d, but got nothing",
+ arg_type_names[(mask >> (pos * 4)) & 15], pos + 1);
goto err;
parse_err:
- if (err_msg) {
- memprintf(err_msg, "failed to parse '%s' as type '%s' at position %d",
- word, arg_type_names[(mask >> (pos * 4)) & 15], pos + 1);
- }
+ memprintf(err_msg, "failed to parse '%s' as type '%s' at position %d",
+ word, arg_type_names[(mask >> (pos * 4)) & 15], pos + 1);
goto err;
not_impl:
- if (err_msg) {
- memprintf(err_msg, "parsing for type '%s' was not implemented, please report this bug",
- arg_type_names[(mask >> (pos * 4)) & 15]);
- }
+ memprintf(err_msg, "parsing for type '%s' was not implemented, please report this bug",
+ arg_type_names[(mask >> (pos * 4)) & 15]);
goto err;
}
int val;
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : missing value", args[cur_arg]);
+ memprintf(err, "'%s' : missing value", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
val = atol(args[cur_arg + 1]);
if (val <= 0) {
- if (err)
- memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
+ memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
return ERR_ALERT | ERR_FATAL;
}
struct listener *l, *new;
if (conf->listeners.n != conf->listeners.p) {
- if (err)
- memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
+ memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
+ memprintf(err, "'%s' : expects an integer argument", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
new->conf.id.key = new->luid;
if (new->luid <= 0) {
- if (err)
- memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
+ memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
node = eb32_lookup(&px->conf.used_listener_id, new->luid);
if (node) {
l = container_of(node, struct listener, conf.id);
- if (err)
- memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
- args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
- l->bind_conf->arg);
+ memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')",
+ args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line,
+ l->bind_conf->arg);
return ERR_ALERT | ERR_FATAL;
}
int val;
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : missing value", args[cur_arg]);
+ memprintf(err, "'%s' : missing value", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
val = atol(args[cur_arg + 1]);
if (val <= 0) {
- if (err)
- memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
+ memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val);
return ERR_ALERT | ERR_FATAL;
}
struct listener *l;
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : missing name", args[cur_arg]);
+ memprintf(err, "'%s' : missing name", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
int val;
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : missing value", args[cur_arg]);
+ memprintf(err, "'%s' : missing value", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
val = atol(args[cur_arg + 1]);
if (val < -1024 || val > 1024) {
- if (err)
- memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
+ memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val);
return ERR_ALERT | ERR_FATAL;
}
if (meth == HTTP_METH_OTHER) {
pattern->ptr.str = strdup(*text);
if (!pattern->ptr.str) {
- if (err)
- memprintf(err, "out of memory while loading pattern");
+ memprintf(err, "out of memory while loading pattern");
return 0;
}
pattern->len = len;
{
pattern->ptr.str = strdup(*text);
if (!pattern->ptr.str) {
- if (err)
- memprintf(err, "out of memory while loading pattern");
+ memprintf(err, "out of memory while loading pattern");
return 0;
}
pattern->len = strlen(*text);
static int val_hdr(struct arg *arg, char **err_msg)
{
if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
- if (err_msg)
- memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
+ memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
return 0;
}
return 1;
static int val_payload(struct arg *arg, char **err_msg)
{
if (!arg[1].data.uint) {
- if (err_msg)
- memprintf(err_msg, "payload length must be > 0");
+ memprintf(err_msg, "payload length must be > 0");
return 0;
}
return 1;
static int val_payload_lv(struct arg *arg, char **err_msg)
{
if (!arg[1].data.uint) {
- if (err_msg)
- memprintf(err_msg, "payload length must be > 0");
+ memprintf(err_msg, "payload length must be > 0");
return 0;
}
if (arg[2].type == ARGT_SINT &&
(int)(arg[0].data.uint + arg[1].data.uint + arg[2].data.sint) < 0) {
- if (err_msg)
- memprintf(err_msg, "payload offset too negative");
+ memprintf(err_msg, "payload offset too negative");
return 0;
}
return 1;
int mss;
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
+ memprintf(err, "'%s' : missing MSS value", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
mss = atoi(args[cur_arg + 1]);
if (!mss || abs(mss) > 65535) {
- if (err)
- memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
+ memprintf(err, "'%s' : expects an MSS with and absolute value between 1 and 65535", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
struct listener *l;
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : missing interface name", args[cur_arg]);
+ memprintf(err, "'%s' : missing interface name", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
static int bind_parse_mode(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
{
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : missing mode (octal integer expected)", args[cur_arg]);
+ memprintf(err, "'%s' : missing mode (octal integer expected)", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
static int bind_parse_gid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
{
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : missing value", args[cur_arg]);
+ memprintf(err, "'%s' : missing value", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
struct group *group;
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : missing group name", args[cur_arg]);
+ memprintf(err, "'%s' : missing group name", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
group = getgrnam(args[cur_arg + 1]);
if (!group) {
- if (err)
- memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
+ memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
return ERR_ALERT | ERR_FATAL;
}
static int bind_parse_uid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
{
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : missing value", args[cur_arg]);
+ memprintf(err, "'%s' : missing value", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
struct passwd *user;
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : missing user name", args[cur_arg]);
+ memprintf(err, "'%s' : missing user name", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
user = getpwnam(args[cur_arg + 1]);
if (!user) {
- if (err)
- memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
+ memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
return ERR_ALERT | ERR_FATAL;
}
ctx = SSL_CTX_new(SSLv23_server_method());
if (!ctx) {
- if (err)
- memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
- *err ? *err : "", path);
+ memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
+ err && *err ? *err : "", path);
return 1;
}
if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
- if (err)
- memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n",
- *err ? *err : "", path);
+ memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n",
+ err && *err ? *err : "", path);
SSL_CTX_free(ctx);
return 1;
}
ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf);
if (ret <= 0) {
- if (err)
- memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n",
- *err ? *err : "", path);
+ memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n",
+ err && *err ? *err : "", path);
if (ret < 0) /* serious error, must do that ourselves */
SSL_CTX_free(ctx);
return 1;
*/
#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
if (bind_conf->default_ctx) {
- if (err)
- memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
- *err ? *err : "");
+ memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
+ err && *err ? *err : "");
return 1;
}
#endif
while ((de = readdir(dir))) {
snprintf(fp, pathlen + 1 + NAME_MAX + 1, "%s/%s", path, de->d_name);
if (stat(fp, &buf) != 0) {
- if (err)
- memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
- *err ? *err : "", fp, strerror(errno));
+ memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
+ err && *err ? *err : "", fp, strerror(errno));
cfgerr++;
continue;
}
static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
{
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
+ memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
static int bind_parse_crt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
{
if (!*args[cur_arg + 1]) {
- if (err)
- memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
+ memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
return ERR_ALERT | ERR_FATAL;
}
* This means that <err> must be initialized to NULL before first invocation.
* The return value also holds the allocated string, which eases error checking
* and immediate consumption. If the output pointer is not used, NULL must be
- * passed instead and it will be ignored.
+ * passed instead and it will be ignored. The returned message will then also
+ * be NULL so that the caller does not have to bother with freeing anything.
*
* It is also convenient to use it without any free except the last one :
* err = NULL;
int allocated = 0;
int needed = 0;
+ if (!out)
+ return NULL;
+
do {
/* vsnprintf() will return the required length even when the
* target buffer is NULL. We do this in a loop just in case