Skip to content

Commit

Permalink
Switch all xlat 'out' argument to be char ** instead of char * Addres…
Browse files Browse the repository at this point in the history
…ses #1185

Pre-allocate buffer of 2048 bytes and pass it as char *
  • Loading branch information
arr2036 committed Aug 9, 2015
1 parent f9f8035 commit d7388f5
Show file tree
Hide file tree
Showing 24 changed files with 345 additions and 450 deletions.
4 changes: 2 additions & 2 deletions src/include/xlat.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ extern "C" {

typedef struct xlat_exp xlat_exp_t;

typedef size_t (*xlat_escape_t)(REQUEST *, char *out, size_t outlen, char const *in, void *arg);
typedef ssize_t (*xlat_func_t)(void *instance, REQUEST *, char const *, char *, size_t);
typedef size_t (*xlat_escape_t)(REQUEST *request, char *out, size_t outlen, char const *in, void *arg);
typedef ssize_t (*xlat_func_t)(void *instance, REQUEST *request, char const *fmt, char **out, size_t outlen);

ssize_t radius_xlat(char *out, size_t outlen, REQUEST *request, char const *fmt, xlat_escape_t escape,
void *escape_ctx)
Expand Down
12 changes: 3 additions & 9 deletions src/main/listen.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,25 @@ static fr_protocol_t master_listen[MAX_LISTENER];
/*
* Xlat for %{listen:foo}
*/
static ssize_t xlat_listen(UNUSED void *instance, REQUEST *request,
char const *fmt, char *out,
size_t outlen)
static ssize_t xlat_listen(UNUSED void *instance, REQUEST *request, char const *fmt, char **out, size_t outlen)
{
char const *value = NULL;
CONF_PAIR *cp;

if (!fmt || !out || (outlen < 1)) return 0;

if (!request->listener) {
RWDEBUG("No listener associated with this request");
*out = '\0';
return 0;
}

cp = cf_pair_find(request->listener->cs, fmt);
if (!cp || !(value = cf_pair_value(cp))) {
RDEBUG("Listener does not contain config item \"%s\"", fmt);
*out = '\0';
return 0;
}

strlcpy(out, value, outlen);
strlcpy(*out, value, outlen);

return strlen(out);
return strlen(*out);
}

/*
Expand Down
49 changes: 16 additions & 33 deletions src/main/mainconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ static size_t config_escape_func(UNUSED REQUEST *request, char *out, size_t outl
/*
* Xlat for %{config:section.subsection.attribute}
*/
static ssize_t xlat_config(UNUSED void *instance, REQUEST *request, char const *fmt, char *out, size_t outlen)
static ssize_t xlat_config(UNUSED void *instance, REQUEST *request, char const *fmt, char **out, size_t outlen)
{
char const *value;
CONF_PAIR *cp;
Expand All @@ -338,15 +338,12 @@ static ssize_t xlat_config(UNUSED void *instance, REQUEST *request, char const *
/*
* Expand it safely.
*/
if (radius_xlat(buffer, sizeof(buffer), request, fmt, config_escape_func, NULL) < 0) {
return 0;
}
if (radius_xlat(buffer, sizeof(buffer), request, fmt, config_escape_func, NULL) < 0) return 0;

ci = cf_reference_item(request->root->config,
request->root->config, buffer);
if (!ci || !cf_item_is_pair(ci)) {
REDEBUG("Config item \"%s\" does not exist", fmt);
*out = '\0';
return -1;
}

Expand All @@ -358,56 +355,47 @@ static ssize_t xlat_config(UNUSED void *instance, REQUEST *request, char const *
* If 'outlen' is too small, then the output is chopped to fit.
*/
value = cf_pair_value(cp);
if (!value) {
out[0] = '\0';
return 0;
}
if (!value) return 0;

if (outlen > strlen(value)) {
outlen = strlen(value) + 1;
}
if (outlen > strlen(value)) outlen = strlen(value) + 1;

strlcpy(out, value, outlen);
strlcpy(*out, value, outlen);

return strlen(out);
return strlen(*out);
}


/*
* Xlat for %{client:foo}
*/
static ssize_t xlat_client(UNUSED void *instance, REQUEST *request, char const *fmt, char *out, size_t outlen)
static ssize_t xlat_client(UNUSED void *instance, REQUEST *request, char const *fmt, char **out, size_t outlen)
{
char const *value = NULL;
CONF_PAIR *cp;

if (!fmt || !out || (outlen < 1)) return 0;

if (!request->client) {
RWDEBUG("No client associated with this request");
*out = '\0';
return 0;
}

cp = cf_pair_find(request->client->cs, fmt);
if (!cp || !(value = cf_pair_value(cp))) {
if (strcmp(fmt, "shortname") == 0) {
strlcpy(out, request->client->shortname, outlen);
return strlen(out);
strlcpy(*out, request->client->shortname, outlen);
return strlen(*out);
}
*out = '\0';
return 0;
}

strlcpy(out, value, outlen);
strlcpy(*out, value, outlen);

return strlen(out);
return strlen(*out);
}

/*
* Xlat for %{getclient:<ipaddr>.foo}
*/
static ssize_t xlat_getclient(UNUSED void *instance, REQUEST *request, char const *fmt, char *out, size_t outlen)
static ssize_t xlat_getclient(UNUSED void *instance, REQUEST *request, char const *fmt, char **out, size_t outlen)
{
char const *value = NULL;
char buffer[INET6_ADDRSTRLEN], *q;
Expand All @@ -416,8 +404,6 @@ static ssize_t xlat_getclient(UNUSED void *instance, REQUEST *request, char cons
CONF_PAIR *cp;
RADCLIENT *client = NULL;

if (!fmt || !out || (outlen < 1)) return 0;

q = strrchr(p, '.');
if (!q || (q == p) || (((size_t)(q - p)) > sizeof(buffer))) {
REDEBUG("Invalid client string");
Expand All @@ -435,25 +421,22 @@ static ssize_t xlat_getclient(UNUSED void *instance, REQUEST *request, char cons
client = client_find(NULL, &ip, IPPROTO_IP);
if (!client) {
RDEBUG("No client found with IP \"%s\"", buffer);
*out = '\0';
return 0;
}

cp = cf_pair_find(client->cs, fmt);
if (!cp || !(value = cf_pair_value(cp))) {
if (strcmp(fmt, "shortname") == 0) {
strlcpy(out, request->client->shortname, outlen);
return strlen(out);
strlcpy(*out, request->client->shortname, outlen);
return strlen(*out);
}
*out = '\0';
return 0;
}

strlcpy(out, value, outlen);
return strlen(out);
strlcpy(*out, value, outlen);
return strlen(*out);

error:
*out = '\0';
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/radattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pid_t rad_waitpid(pid_t pid, int *status)
}

static ssize_t xlat_test(UNUSED void *instance, UNUSED REQUEST *request,
UNUSED char const *fmt, UNUSED char *out, UNUSED size_t outlen)
UNUSED char const *fmt, UNUSED char **out, UNUSED size_t outlen)
{
return 0;
}
Expand Down
12 changes: 4 additions & 8 deletions src/main/realms.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,33 +226,29 @@ static size_t CC_HINT(nonnull) xlat_cs(CONF_SECTION *cs, char const *fmt, char *
* Xlat for %{home_server:foo}
*/
static ssize_t CC_HINT(nonnull) xlat_home_server(UNUSED void *instance, REQUEST *request,
char const *fmt, char *out, size_t outlen)
char const *fmt, char **out, size_t outlen)
{
if (!request->home_server) {
RWDEBUG("No home_server associated with this request");

*out = '\0';
return 0;
}

return xlat_cs(request->home_server->cs, fmt, out, outlen);
return xlat_cs(request->home_server->cs, fmt, *out, outlen);
}


/*
* Xlat for %{home_server_pool:foo}
*/
static ssize_t CC_HINT(nonnull) xlat_server_pool(UNUSED void *instance, REQUEST *request,
char const *fmt, char *out, size_t outlen)
char const *fmt, char **out, size_t outlen)
{
if (!request->home_pool) {
RWDEBUG("No home_pool associated with this request");

*out = '\0';
return 0;
}

return xlat_cs(request->home_pool->cs, fmt, out, outlen);
return xlat_cs(request->home_pool->cs, fmt, *out, outlen);
}
#endif

Expand Down
7 changes: 3 additions & 4 deletions src/main/unittest.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ static void print_packet(FILE *fp, RADIUS_PACKET *packet)
* %{poke:sql.foo=bar}
*/
static ssize_t xlat_poke(UNUSED void *instance, REQUEST *request,
char const *fmt, char *out, size_t outlen)
char const *fmt, char **out, size_t outlen)
{
int i;
void *data, *base;
Expand All @@ -432,8 +432,7 @@ static ssize_t xlat_poke(UNUSED void *instance, REQUEST *request,
rad_assert(request != NULL);
rad_assert(fmt != NULL);
rad_assert(out != NULL);

*out = '\0';
rad_assert(*out);

modules = cf_section_sub_find(request->root->config, "modules");
if (!modules) return 0;
Expand Down Expand Up @@ -477,7 +476,7 @@ static ssize_t xlat_poke(UNUSED void *instance, REQUEST *request,
* Copy the old value to the output buffer, that way
* tests can restore it later, if they need to.
*/
len = strlcpy(out, cf_pair_value(cp), outlen);
len = strlcpy(*out, cf_pair_value(cp), outlen);

if (cf_pair_replace(mi->cs, cp, q) < 0) {
RDEBUG("Failed replacing pair");
Expand Down
Loading

0 comments on commit d7388f5

Please sign in to comment.