From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Subject: [PATCH libnftnl 9/9] chain: dynamically allocate name
Date: Tue, 14 Jun 2016 15:18:45 +0200 [thread overview]
Message-ID: <1465910325-13286-9-git-send-email-pablo@netfilter.org> (raw)
In-Reply-To: <1465910325-13286-1-git-send-email-pablo@netfilter.org>
Just in case we ever support chain with larger names in the future,
this will ensure the library doesn't break. Although I don't expect
allocating more bytes for this anytime soon, but let's be conservative
here.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
src/chain.c | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/src/chain.c b/src/chain.c
index 86ccef6..a7f6742 100644
--- a/src/chain.c
+++ b/src/chain.c
@@ -32,7 +32,7 @@
struct nftnl_chain {
struct list_head head;
- char name[NFT_CHAIN_MAXNAMELEN];
+ const char *name;
const char *type;
const char *table;
const char *dev;
@@ -95,13 +95,14 @@ EXPORT_SYMBOL(nftnl_chain_alloc);
void nftnl_chain_free(const struct nftnl_chain *c)
{
+ if (c->flags & (1 << NFTNL_CHAIN_NAME))
+ xfree(c->name);
if (c->flags & (1 << NFTNL_CHAIN_TABLE))
xfree(c->table);
if (c->flags & (1 << NFTNL_CHAIN_TYPE))
xfree(c->type);
if (c->flags & (1 << NFTNL_CHAIN_DEV))
xfree(c->dev);
-
xfree(c);
}
EXPORT_SYMBOL(nftnl_chain_free);
@@ -118,6 +119,9 @@ void nftnl_chain_unset(struct nftnl_chain *c, uint16_t attr)
return;
switch (attr) {
+ case NFTNL_CHAIN_NAME:
+ xfree(c->name);
+ break;
case NFTNL_CHAIN_TABLE:
xfree(c->table);
break;
@@ -126,7 +130,6 @@ void nftnl_chain_unset(struct nftnl_chain *c, uint16_t attr)
case NFTNL_CHAIN_TYPE:
xfree(c->type);
break;
- case NFTNL_CHAIN_NAME:
case NFTNL_CHAIN_HOOKNUM:
case NFTNL_CHAIN_PRIO:
case NFTNL_CHAIN_POLICY:
@@ -164,7 +167,12 @@ int nftnl_chain_set_data(struct nftnl_chain *c, uint16_t attr,
switch(attr) {
case NFTNL_CHAIN_NAME:
- strncpy(c->name, data, NFT_CHAIN_MAXNAMELEN);
+ if (c->flags & (1 << NFTNL_CHAIN_NAME))
+ xfree(c->name);
+
+ c->name = strdup(data);
+ if (!c->name)
+ return -1;
break;
case NFTNL_CHAIN_TABLE:
if (c->flags & (1 << NFTNL_CHAIN_TABLE))
@@ -528,8 +536,11 @@ int nftnl_chain_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_chain *c)
return -1;
if (tb[NFTA_CHAIN_NAME]) {
- strncpy(c->name, mnl_attr_get_str(tb[NFTA_CHAIN_NAME]),
- NFT_CHAIN_MAXNAMELEN);
+ if (c->flags & (1 << NFTNL_CHAIN_NAME))
+ xfree(c->name);
+ c->name = strdup(mnl_attr_get_str(tb[NFTA_CHAIN_NAME]));
+ if (!c->name)
+ return -1;
c->flags |= (1 << NFTNL_CHAIN_NAME);
}
if (tb[NFTA_CHAIN_TABLE]) {
--
2.1.4
prev parent reply other threads:[~2016-06-14 13:19 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-06-14 13:18 [PATCH libnftnl 1/9] src: get rid of aliases Pablo Neira Ayuso
2016-06-14 13:18 ` [PATCH libnftnl 2/9] src: assert when setting unknown attributes Pablo Neira Ayuso
2016-06-14 13:18 ` [PATCH libnftnl 3/9] src: return value on setters that internally allocate memory Pablo Neira Ayuso
2016-06-14 13:18 ` [PATCH libnftnl 4/9] src: check for strdup() errors from setters and parsers Pablo Neira Ayuso
2016-06-14 13:18 ` [PATCH libnftnl 5/9] expr: data_reg: get rid of leftover perror() calls Pablo Neira Ayuso
2016-06-14 13:18 ` [PATCH libnftnl 6/9] src: simplify unsetters Pablo Neira Ayuso
2016-06-14 13:18 ` [PATCH libnftnl 7/9] src: check for flags before releasing attributes Pablo Neira Ayuso
2016-06-14 13:18 ` [PATCH libnftnl 8/9] tests: shuffle values that are injected Pablo Neira Ayuso
2016-06-14 13:18 ` Pablo Neira Ayuso [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1465910325-13286-9-git-send-email-pablo@netfilter.org \
--to=pablo@netfilter.org \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).