]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
libxtables: Introduce xtables_strdup() and use it everywhere
authorPhil Sutter <phil@nwl.cc>
Wed, 2 Jun 2021 10:56:06 +0000 (12:56 +0200)
committerPhil Sutter <phil@nwl.cc>
Mon, 7 Jun 2021 12:50:27 +0000 (14:50 +0200)
This wraps strdup(), checking for errors.

Signed-off-by: Phil Sutter <phil@nwl.cc>
26 files changed:
extensions/libebt_ip.c
extensions/libebt_ip6.c
extensions/libebt_stp.c
extensions/libip6t_DNAT.c
extensions/libip6t_SNAT.c
extensions/libip6t_dst.c
extensions/libip6t_hbh.c
extensions/libip6t_ipv6header.c
extensions/libip6t_mh.c
extensions/libip6t_rt.c
extensions/libipt_DNAT.c
extensions/libipt_SNAT.c
extensions/libxt_dccp.c
extensions/libxt_hashlimit.c
extensions/libxt_iprange.c
extensions/libxt_multiport.c
extensions/libxt_sctp.c
extensions/libxt_set.h
extensions/libxt_tcp.c
include/xtables.h
iptables/iptables-xml.c
iptables/nft-cache.c
iptables/nft-cmd.c
iptables/xshared.c
libxtables/xtables.c
libxtables/xtoptions.c

index acb9bfcdbbd9fb56e5dd1e527bcb217fd4cf3ffc..51649ffb3c3057e684c96d60176ebb1c83e1390e 100644 (file)
@@ -175,7 +175,8 @@ parse_port_range(const char *protocol, const char *portstring, uint16_t *ports)
        char *buffer;
        char *cp;
 
-       buffer = strdup(portstring);
+       buffer = xtables_strdup(portstring);
+
        if ((cp = strchr(buffer, ':')) == NULL)
                ports[0] = ports[1] = xtables_parse_port(buffer, NULL);
        else {
index 3cc39271d465860f11f3f3d57b629a0fb068693e..a686a285c3cb811dc47a600283f34a5c2c12b077 100644 (file)
@@ -93,7 +93,7 @@ parse_port_range(const char *protocol, const char *portstring, uint16_t *ports)
        char *buffer;
        char *cp;
 
-       buffer = strdup(portstring);
+       buffer = xtables_strdup(portstring);
        if ((cp = strchr(buffer, ':')) == NULL)
                ports[0] = ports[1] = xtables_parse_port(buffer, NULL);
        else {
index 81ba572c33c1ad3ae7939adf324f83fb51de7da1..3e9e24474eb618a0ab103392e340c6faf7a74ca4 100644 (file)
@@ -90,7 +90,8 @@ static int parse_range(const char *portstring, void *lower, void *upper,
        uint32_t low_nr, upp_nr;
        int ret = 0;
 
-       buffer = strdup(portstring);
+       buffer = xtables_strdup(portstring);
+
        if ((cp = strchr(buffer, ':')) == NULL) {
                low_nr = strtoul(buffer, &end, 10);
                if (*end || low_nr < min || low_nr > max) {
index 89c5ceb1532503a22a40991143a1b4bfdb413d95..f1ad81436316b748af2f9fd4e67a2b8081d426ed 100644 (file)
@@ -58,9 +58,7 @@ parse_to(const char *orig_arg, int portok, struct nf_nat_range2 *range, int rev)
        char *arg, *start, *end = NULL, *colon = NULL, *dash, *error;
        const struct in6_addr *ip;
 
-       arg = strdup(orig_arg);
-       if (arg == NULL)
-               xtables_error(RESOURCE_PROBLEM, "strdup");
+       arg = xtables_strdup(orig_arg);
 
        start = strchr(arg, '[');
        if (start == NULL) {
index 7d74b3d76a93c159814eedbcf296c4c5d896ea6f..6d19614c7c7084178d26a0701b4d1aba75136dc0 100644 (file)
@@ -52,9 +52,7 @@ parse_to(const char *orig_arg, int portok, struct nf_nat_range *range)
        char *arg, *start, *end = NULL, *colon = NULL, *dash, *error;
        const struct in6_addr *ip;
 
-       arg = strdup(orig_arg);
-       if (arg == NULL)
-               xtables_error(RESOURCE_PROBLEM, "strdup");
+       arg = xtables_strdup(orig_arg);
 
        start = strchr(arg, '[');
        if (start == NULL) {
index fe7e3403468ce684981732444350e033be24ce2b..bf0e3e436665d2da02e3fa359f48f91360ebce69 100644 (file)
@@ -57,11 +57,9 @@ parse_options(const char *optsstr, uint16_t *opts)
 {
         char *buffer, *cp, *next, *range;
         unsigned int i;
-       
-       buffer = strdup(optsstr);
-        if (!buffer)
-               xtables_error(OTHER_PROBLEM, "strdup failed");
-                       
+
+       buffer = xtables_strdup(optsstr);
+
         for (cp = buffer, i = 0; cp && i < IP6T_OPTS_OPTSNR; cp = next, i++)
         {
                 next = strchr(cp, ',');
index 4cebecfd3d2f5e6fa8a76e92f0b30000e9163266..74e87cda7eea1ac98766b256c82a41d071b60352 100644 (file)
@@ -57,10 +57,9 @@ parse_options(const char *optsstr, uint16_t *opts)
 {
         char *buffer, *cp, *next, *range;
         unsigned int i;
-       
-       buffer = strdup(optsstr);
-       if (!buffer) xtables_error(OTHER_PROBLEM, "strdup failed");
-                       
+
+       buffer = xtables_strdup(optsstr);
+
         for (cp=buffer, i=0; cp && i<IP6T_OPTS_OPTSNR; cp=next,i++)
         {
                 next=strchr(cp, ',');
index 6f03087bb79d8d43c0d23f64d9cd4d75cd24bee1..9e34562966f8bfcebbfbf917edb3f0c30498ef88 100644 (file)
@@ -147,7 +147,7 @@ parse_header(const char *flags) {
         char *ptr;
         char *buffer;
 
-        buffer = strdup(flags);
+        buffer = xtables_strdup(flags);
 
         for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) 
                ret |= add_proto_to_mask(name_to_proto(ptr));
index f4c0fd9fc0bca48d77437970a97c4ddfb4b31586..64675405ac72487478d928599c7a7337d1bc55f1 100644 (file)
@@ -107,7 +107,7 @@ static void parse_mh_types(const char *mhtype, uint8_t *types)
        char *buffer;
        char *cp;
 
-       buffer = strdup(mhtype);
+       buffer = xtables_strdup(mhtype);
        if ((cp = strchr(buffer, ':')) == NULL)
                types[0] = types[1] = name_to_type(buffer);
        else {
index 3cb3b249d89954fc34a7dc404d1aaeb1d5349b0e..9708b5a0c42f31436c1d6338725e3b93e6a6beea 100644 (file)
@@ -73,10 +73,9 @@ parse_addresses(const char *addrstr, struct in6_addr *addrp)
 {
         char *buffer, *cp, *next;
         unsigned int i;
-       
-       buffer = strdup(addrstr);
-       if (!buffer) xtables_error(OTHER_PROBLEM, "strdup failed");
-                       
+
+       buffer = xtables_strdup(addrstr);
+
         for (cp=buffer, i=0; cp && i<IP6T_RT_HOPS; cp=next,i++)
         {
                 next=strchr(cp, ',');
index 4907a2e83d066b092fac203b943e02908ebe61c4..5b33fd23f6e3632e6d2508147a86fc7e25b2eccd 100644 (file)
@@ -79,9 +79,7 @@ parse_to(const char *orig_arg, int portok, struct ipt_natinfo *info)
        char *arg, *colon, *dash, *error;
        const struct in_addr *ip;
 
-       arg = strdup(orig_arg);
-       if (arg == NULL)
-               xtables_error(RESOURCE_PROBLEM, "strdup");
+       arg = xtables_strdup(orig_arg);
        memset(&range, 0, sizeof(range));
        colon = strchr(arg, ':');
 
@@ -302,9 +300,7 @@ parse_to_v2(const char *orig_arg, int portok, struct nf_nat_range2 *range)
        char *arg, *colon, *dash, *error;
        const struct in_addr *ip;
 
-       arg = strdup(orig_arg);
-       if (arg == NULL)
-               xtables_error(RESOURCE_PROBLEM, "strdup");
+       arg = xtables_strdup(orig_arg);
 
        colon = strchr(arg, ':');
        if (colon) {
index e92d811c2bc93326907b6bf01e993a06969cd492..c655439ec9192f71483efa1fb55c6546fe81b6db 100644 (file)
@@ -73,9 +73,7 @@ parse_to(const char *orig_arg, int portok, struct ipt_natinfo *info)
        char *arg, *colon, *dash, *error;
        const struct in_addr *ip;
 
-       arg = strdup(orig_arg);
-       if (arg == NULL)
-               xtables_error(RESOURCE_PROBLEM, "strdup");
+       arg = xtables_strdup(orig_arg);
        memset(&range, 0, sizeof(range));
        colon = strchr(arg, ':');
 
index aea3e20be4818cef8cba61e0ccb9c995b0967ce6..abd420fcc0032db91193f90e1eb1b935ed113ed7 100644 (file)
@@ -85,7 +85,7 @@ parse_dccp_types(const char *typestring)
        uint16_t typemask = 0;
        char *ptr, *buffer;
 
-       buffer = strdup(typestring);
+       buffer = xtables_strdup(typestring);
 
        for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
                unsigned int i;
index 7f1d2a402c4fd00128e90c4c37fe5fed34179af7..3f3c43010ee2a3b4a1d89576f720154d40f71b62 100644 (file)
@@ -508,10 +508,7 @@ static void hashlimit_mt6_init(struct xt_entry_match *match)
 static int parse_mode(uint32_t *mode, const char *option_arg)
 {
        char *tok;
-       char *arg = strdup(option_arg);
-
-       if (!arg)
-               return -1;
+       char *arg = xtables_strdup(option_arg);
 
        for (tok = strtok(arg, ",|");
             tok;
index 8be2481497b8d08a20155f2da3dc1b5436066d60..04ce7b364f1c68d8aa0e2616b2c6c81aa3074739 100644 (file)
@@ -73,11 +73,9 @@ iprange_parse_spec(const char *from, const char *to, union nf_inet_addr *range,
 static void iprange_parse_range(const char *oarg, union nf_inet_addr *range,
                                uint8_t family, const char *optname)
 {
-       char *arg = strdup(oarg);
+       char *arg = xtables_strdup(oarg);
        char *dash;
 
-       if (arg == NULL)
-               xtables_error(RESOURCE_PROBLEM, "strdup");
        dash = strchr(arg, '-');
        if (dash == NULL) {
                iprange_parse_spec(arg, arg, range, family, optname);
index 07ad4cfd4e519154e1a9a1867194576709d823d0..4a42fa38238b9326d9047988956f6cd9ef84839d 100644 (file)
@@ -87,8 +87,7 @@ parse_multi_ports(const char *portstring, uint16_t *ports, const char *proto)
        char *buffer, *cp, *next;
        unsigned int i;
 
-       buffer = strdup(portstring);
-       if (!buffer) xtables_error(OTHER_PROBLEM, "strdup failed");
+       buffer = xtables_strdup(portstring);
 
        for (cp=buffer, i=0; cp && i<XT_MULTI_PORTS; cp=next,i++)
        {
@@ -109,8 +108,7 @@ parse_multi_ports_v1(const char *portstring,
        char *buffer, *cp, *next, *range;
        unsigned int i;
 
-       buffer = strdup(portstring);
-       if (!buffer) xtables_error(OTHER_PROBLEM, "strdup failed");
+       buffer = xtables_strdup(portstring);
 
        for (i=0; i<XT_MULTI_PORTS; i++)
                multiinfo->pflags[i] = 0;
index 5d8ab85cacf42435130180d4aeaafaff850e15e1..a4c5415f2036e384c4f8c586aa6d246d42ff3d52 100644 (file)
@@ -69,7 +69,7 @@ parse_sctp_ports(const char *portstring,
        char *buffer;
        char *cp;
 
-       buffer = strdup(portstring);
+       buffer = xtables_strdup(portstring);
        DEBUGP("%s\n", portstring);
        if ((cp = strchr(buffer, ':')) == NULL) {
                ports[0] = ports[1] = xtables_parse_port(buffer, "sctp");
@@ -164,7 +164,7 @@ parse_sctp_chunk(struct xt_sctp_info *einfo,
        int found = 0;
        char *chunk_flags;
 
-       buffer = strdup(chunks);
+       buffer = xtables_strdup(chunks);
        DEBUGP("Buffer: %s\n", buffer);
 
        SCTP_CHUNKMAP_RESET(einfo->chunkmap);
index 41dfbd30fc7c1fb5f5604a572d8e9ba725555ff3..ad895a7504d9d7679b5033bdc8ce7d0781e36620 100644 (file)
@@ -141,7 +141,7 @@ get_set_byname(const char *setname, struct xt_set_info *info)
 static void
 parse_dirs_v0(const char *opt_arg, struct xt_set_info_v0 *info)
 {
-       char *saved = strdup(opt_arg);
+       char *saved = xtables_strdup(opt_arg);
        char *ptr, *tmp = saved;
        int i = 0;
        
@@ -167,7 +167,7 @@ parse_dirs_v0(const char *opt_arg, struct xt_set_info_v0 *info)
 static void
 parse_dirs(const char *opt_arg, struct xt_set_info *info)
 {
-       char *saved = strdup(opt_arg);
+       char *saved = xtables_strdup(opt_arg);
        char *ptr, *tmp = saved;
        
        while (info->dim < IPSET_DIM_MAX && tmp != NULL) {
index 58f3c0a0c3c28c2022e6d7200a49ae34b02f6407..383e4db5b5e231dd53350203bca6fc25d9eeb17f 100644 (file)
@@ -43,7 +43,7 @@ parse_tcp_ports(const char *portstring, uint16_t *ports)
        char *buffer;
        char *cp;
 
-       buffer = strdup(portstring);
+       buffer = xtables_strdup(portstring);
        if ((cp = strchr(buffer, ':')) == NULL)
                ports[0] = ports[1] = xtables_parse_port(buffer, "tcp");
        else {
@@ -83,7 +83,7 @@ parse_tcp_flag(const char *flags)
        char *ptr;
        char *buffer;
 
-       buffer = strdup(flags);
+       buffer = xtables_strdup(flags);
 
        for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
                unsigned int i;
index 1fd5f63ac4b69a3033f262c6713234f0a5e6c990..5b13f348f768de41b91b4612c90f1970f74713e8 100644 (file)
@@ -453,6 +453,7 @@ extern void xtables_set_nfproto(uint8_t);
 extern void *xtables_calloc(size_t, size_t);
 extern void *xtables_malloc(size_t);
 extern void *xtables_realloc(void *, size_t);
+char *xtables_strdup(const char *);
 
 extern int xtables_insmod(const char *, const char *, bool);
 extern int xtables_load_ko(const char *, bool);
index 98d03dda98d2b7ff38199293da6095cec44694c8..6cf059fb67292d609f15b79ca8269e4896946667 100644 (file)
@@ -213,8 +213,8 @@ saveChain(char *chain, char *policy, struct xt_counters *ctr)
                           "%s: line %u chain name invalid\n",
                           prog_name, line);
 
-       chains[nextChain].chain = strdup(chain);
-       chains[nextChain].policy = strdup(policy);
+       chains[nextChain].chain = xtables_strdup(chain);
+       chains[nextChain].policy = xtables_strdup(policy);
        chains[nextChain].count = *ctr;
        chains[nextChain].created = 0;
        nextChain++;
index 8fbf9727826d85512951cbd86a7cf4933c458371..2c88301cc74458925f13f6b81ca830b75b59f7d2 100644 (file)
@@ -40,7 +40,7 @@ static void cache_chain_list_insert(struct list_head *list, const char *name)
        }
 
        new = xtables_malloc(sizeof(*new));
-       new->name = strdup(name);
+       new->name = xtables_strdup(name);
        list_add_tail(&new->head, pos ? &pos->head : list);
 }
 
@@ -56,7 +56,7 @@ void nft_cache_level_set(struct nft_handle *h, int level,
                return;
 
        if (!req->table)
-               req->table = strdup(cmd->table);
+               req->table = xtables_strdup(cmd->table);
        else
                assert(!strcmp(req->table, cmd->table));
 
index c3f6c14e0b99e729482f7d0349192592e6fa2b17..a0c76a795e59c526980ec98994f8e783734a4637 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <xtables.h>
 #include "nft.h"
 #include "nft-cmd.h"
 
@@ -27,9 +28,9 @@ struct nft_cmd *nft_cmd_new(struct nft_handle *h, int command,
                return NULL;
 
        cmd->command = command;
-       cmd->table = strdup(table);
+       cmd->table = xtables_strdup(table);
        if (chain)
-               cmd->chain = strdup(chain);
+               cmd->chain = xtables_strdup(chain);
        cmd->rulenum = rulenum;
        cmd->verbose = verbose;
 
@@ -43,7 +44,7 @@ struct nft_cmd *nft_cmd_new(struct nft_handle *h, int command,
                cmd->obj.rule = rule;
 
                if (!state->target && strlen(state->jumpto) > 0)
-                       cmd->jumpto = strdup(state->jumpto);
+                       cmd->jumpto = xtables_strdup(state->jumpto);
        }
 
        list_add_tail(&cmd->head, &h->cmd_list);
@@ -238,7 +239,7 @@ int nft_cmd_chain_user_rename(struct nft_handle *h,const char *chain,
        if (!cmd)
                return 0;
 
-       cmd->rename = strdup(newname);
+       cmd->rename = xtables_strdup(newname);
 
        nft_cache_level_set(h, NFT_CL_CHAINS, cmd);
 
@@ -304,7 +305,7 @@ int nft_cmd_chain_set(struct nft_handle *h, const char *table,
        if (!cmd)
                return 0;
 
-       cmd->policy = strdup(policy);
+       cmd->policy = xtables_strdup(policy);
        if (counters)
                cmd->counters = *counters;
 
@@ -389,7 +390,7 @@ int ebt_cmd_user_chain_policy(struct nft_handle *h, const char *table,
        if (!cmd)
                return 0;
 
-       cmd->policy = strdup(policy);
+       cmd->policy = xtables_strdup(policy);
 
        nft_cache_level_set(h, NFT_CL_RULES, cmd);
 
index 49a88de518466b33647d65ea24b8bbba2d9330a8..ed3e9c5a4426a461c658d9cc284fe7b50d8ef001 100644 (file)
@@ -435,7 +435,7 @@ void add_argv(struct argv_store *store, const char *what, int quoted)
                xtables_error(PARAMETER_PROBLEM,
                              "Trying to store NULL argument\n");
 
-       store->argv[store->argc] = strdup(what);
+       store->argv[store->argc] = xtables_strdup(what);
        store->argvattr[store->argc] = quoted;
        store->argv[++store->argc] = NULL;
 }
index 82815cae70576db109cac34665d231582d486ee0..77bc1493116c7ef08ece2aa1ba553d3bc5889f68 100644 (file)
@@ -369,6 +369,18 @@ void *xtables_realloc(void *ptr, size_t size)
        return p;
 }
 
+char *xtables_strdup(const char *s)
+{
+       char *dup = strdup(s);
+
+       if (!dup) {
+               perror("ip[6]tables: strdup failed");
+               exit(1);
+       }
+
+       return dup;
+}
+
 static char *get_modprobe(void)
 {
        int procfile;
index 0dcdf607f46782615dba7a26ff2b65be66d67c75..9d3ac5c8066cb3c26fa2c34d368372fde064c805 100644 (file)
@@ -604,9 +604,7 @@ static void xtopt_parse_mport(struct xt_option_call *cb)
        unsigned int maxiter;
        int value;
 
-       wp_arg = lo_arg = strdup(cb->arg);
-       if (lo_arg == NULL)
-               xt_params->exit_err(RESOURCE_PROBLEM, "strdup");
+       wp_arg = lo_arg = xtables_strdup(cb->arg);
 
        maxiter = entry->size / esize;
        if (maxiter == 0)
@@ -747,9 +745,7 @@ static void xtopt_parse_hostmask(struct xt_option_call *cb)
                xtopt_parse_host(cb);
                return;
        }
-       work = strdup(orig_arg);
-       if (work == NULL)
-               xt_params->exit_err(PARAMETER_PROBLEM, "strdup");
+       work = xtables_strdup(orig_arg);
        p = strchr(work, '/'); /* by def this can't be NULL now */
        *p++ = '\0';
        /*
@@ -1139,11 +1135,7 @@ struct xtables_lmap *xtables_lmap_init(const char *file)
                        goto out;
                }
                lmap_this->id   = id;
-               lmap_this->name = strdup(cur);
-               if (lmap_this->name == NULL) {
-                       free(lmap_this);
-                       goto out;
-               }
+               lmap_this->name = xtables_strdup(cur);
                lmap_this->next = NULL;
 
                if (lmap_prev != NULL)