Skip to content

Commit d17b136

Browse files
Phil SutterStephen Hemminger
Phil Sutter
authored and
Stephen Hemminger
committed
Use C99 style initializers everywhere
This big patch was compiled by vimgrepping for memset calls and changing to C99 initializer if applicable. One notable exception is the initialization of union bpf_attr in tc/tc_bpf.c: changing it would break for older gcc versions (at least <=3.4.6). Calls to memset for struct rtattr pointer fields for parse_rtattr*() were just dropped since they are not needed. The changes here allowed the compiler to discover some unused variables, so get rid of them, too. Signed-off-by: Phil Sutter <[email protected]> Acked-by: David Ahern <[email protected]>
1 parent d892aaf commit d17b136

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+532
-913
lines changed

bridge/fdb.c

+11-14
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,15 @@ static int fdb_show(int argc, char **argv)
289289
struct nlmsghdr n;
290290
struct ifinfomsg ifm;
291291
char buf[256];
292-
} req;
292+
} req = {
293+
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
294+
.ifm.ifi_family = PF_BRIDGE,
295+
};
293296

294297
char *filter_dev = NULL;
295298
char *br = NULL;
296299
int msg_size = sizeof(struct ifinfomsg);
297300

298-
memset(&req, 0, sizeof(req));
299-
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
300-
req.ifm.ifi_family = PF_BRIDGE;
301-
302301
while (argc > 0) {
303302
if ((strcmp(*argv, "brport") == 0) || strcmp(*argv, "dev") == 0) {
304303
NEXT_ARG();
@@ -371,7 +370,13 @@ static int fdb_modify(int cmd, int flags, int argc, char **argv)
371370
struct nlmsghdr n;
372371
struct ndmsg ndm;
373372
char buf[256];
374-
} req;
373+
} req = {
374+
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)),
375+
.n.nlmsg_flags = NLM_F_REQUEST | flags,
376+
.n.nlmsg_type = cmd,
377+
.ndm.ndm_family = PF_BRIDGE,
378+
.ndm.ndm_state = NUD_NOARP,
379+
};
375380
char *addr = NULL;
376381
char *d = NULL;
377382
char abuf[ETH_ALEN];
@@ -383,14 +388,6 @@ static int fdb_modify(int cmd, int flags, int argc, char **argv)
383388
char *endptr;
384389
short vid = -1;
385390

386-
memset(&req, 0, sizeof(req));
387-
388-
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
389-
req.n.nlmsg_flags = NLM_F_REQUEST|flags;
390-
req.n.nlmsg_type = cmd;
391-
req.ndm.ndm_family = PF_BRIDGE;
392-
req.ndm.ndm_state = NUD_NOARP;
393-
394391
while (argc > 0) {
395392
if (strcmp(*argv, "dev") == 0) {
396393
NEXT_ARG();

bridge/link.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,12 @@ static int brlink_modify(int argc, char **argv)
255255
struct nlmsghdr n;
256256
struct ifinfomsg ifm;
257257
char buf[512];
258-
} req;
258+
} req = {
259+
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
260+
.n.nlmsg_flags = NLM_F_REQUEST,
261+
.n.nlmsg_type = RTM_SETLINK,
262+
.ifm.ifi_family = PF_BRIDGE,
263+
};
259264
char *d = NULL;
260265
__s8 learning = -1;
261266
__s8 learning_sync = -1;
@@ -271,13 +276,6 @@ static int brlink_modify(int argc, char **argv)
271276
__u16 flags = 0;
272277
struct rtattr *nest;
273278

274-
memset(&req, 0, sizeof(req));
275-
276-
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
277-
req.n.nlmsg_flags = NLM_F_REQUEST;
278-
req.n.nlmsg_type = RTM_SETLINK;
279-
req.ifm.ifi_family = PF_BRIDGE;
280-
281279
while (argc > 0) {
282280
if (strcmp(*argv, "dev") == 0) {
283281
NEXT_ARG();

bridge/mdb.c

+7-10
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,16 @@ static int mdb_modify(int cmd, int flags, int argc, char **argv)
234234
struct nlmsghdr n;
235235
struct br_port_msg bpm;
236236
char buf[1024];
237-
} req;
238-
struct br_mdb_entry entry;
237+
} req = {
238+
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct br_port_msg)),
239+
.n.nlmsg_flags = NLM_F_REQUEST | flags,
240+
.n.nlmsg_type = cmd,
241+
.bpm.family = PF_BRIDGE,
242+
};
243+
struct br_mdb_entry entry = {};
239244
char *d = NULL, *p = NULL, *grp = NULL;
240245
short vid = 0;
241246

242-
memset(&req, 0, sizeof(req));
243-
memset(&entry, 0, sizeof(entry));
244-
245-
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct br_port_msg));
246-
req.n.nlmsg_flags = NLM_F_REQUEST|flags;
247-
req.n.nlmsg_type = cmd;
248-
req.bpm.family = PF_BRIDGE;
249-
250247
while (argc > 0) {
251248
if (strcmp(*argv, "dev") == 0) {
252249
NEXT_ARG();

bridge/vlan.c

+7-10
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,19 @@ static int vlan_modify(int cmd, int argc, char **argv)
3232
struct nlmsghdr n;
3333
struct ifinfomsg ifm;
3434
char buf[1024];
35-
} req;
35+
} req = {
36+
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
37+
.n.nlmsg_flags = NLM_F_REQUEST,
38+
.n.nlmsg_type = cmd,
39+
.ifm.ifi_family = PF_BRIDGE,
40+
};
3641
char *d = NULL;
3742
short vid = -1;
3843
short vid_end = -1;
3944
struct rtattr *afspec;
40-
struct bridge_vlan_info vinfo;
45+
struct bridge_vlan_info vinfo = {};
4146
unsigned short flags = 0;
4247

43-
memset(&vinfo, 0, sizeof(vinfo));
44-
memset(&req, 0, sizeof(req));
45-
46-
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
47-
req.n.nlmsg_flags = NLM_F_REQUEST;
48-
req.n.nlmsg_type = cmd;
49-
req.ifm.ifi_family = PF_BRIDGE;
50-
5148
while (argc > 0) {
5249
if (strcmp(*argv, "dev") == 0) {
5350
NEXT_ARG();

genl/ctrl.c

+17-27
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,19 @@ static int usage(void)
4242
int genl_ctrl_resolve_family(const char *family)
4343
{
4444
struct rtnl_handle rth;
45-
struct nlmsghdr *nlh;
46-
struct genlmsghdr *ghdr;
4745
int ret = 0;
4846
struct {
4947
struct nlmsghdr n;
48+
struct genlmsghdr g;
5049
char buf[4096];
51-
} req;
52-
53-
memset(&req, 0, sizeof(req));
54-
55-
nlh = &req.n;
56-
nlh->nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
57-
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
58-
nlh->nlmsg_type = GENL_ID_CTRL;
59-
60-
ghdr = NLMSG_DATA(&req.n);
61-
ghdr->cmd = CTRL_CMD_GETFAMILY;
50+
} req = {
51+
.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN),
52+
.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
53+
.n.nlmsg_type = GENL_ID_CTRL,
54+
.g.cmd = CTRL_CMD_GETFAMILY,
55+
};
56+
struct nlmsghdr *nlh = &req.n;
57+
struct genlmsghdr *ghdr = &req.g;
6258

6359
if (rtnl_open_byproto(&rth, 0, NETLINK_GENERIC) < 0) {
6460
fprintf(stderr, "Cannot open generic netlink socket\n");
@@ -74,7 +70,6 @@ int genl_ctrl_resolve_family(const char *family)
7470

7571
{
7672
struct rtattr *tb[CTRL_ATTR_MAX + 1];
77-
struct genlmsghdr *ghdr = NLMSG_DATA(nlh);
7873
int len = nlh->nlmsg_len;
7974
struct rtattr *attrs;
8075

@@ -291,24 +286,19 @@ static int print_ctrl2(const struct sockaddr_nl *who,
291286
static int ctrl_list(int cmd, int argc, char **argv)
292287
{
293288
struct rtnl_handle rth;
294-
struct nlmsghdr *nlh;
295-
struct genlmsghdr *ghdr;
296289
int ret = -1;
297290
char d[GENL_NAMSIZ];
298291
struct {
299292
struct nlmsghdr n;
293+
struct genlmsghdr g;
300294
char buf[4096];
301-
} req;
302-
303-
memset(&req, 0, sizeof(req));
304-
305-
nlh = &req.n;
306-
nlh->nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
307-
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
308-
nlh->nlmsg_type = GENL_ID_CTRL;
309-
310-
ghdr = NLMSG_DATA(&req.n);
311-
ghdr->cmd = CTRL_CMD_GETFAMILY;
295+
} req = {
296+
.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN),
297+
.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
298+
.n.nlmsg_type = GENL_ID_CTRL,
299+
.g.cmd = CTRL_CMD_GETFAMILY,
300+
};
301+
struct nlmsghdr *nlh = &req.n;
312302

313303
if (rtnl_open_byproto(&rth, 0, NETLINK_GENERIC) < 0) {
314304
fprintf(stderr, "Cannot open generic netlink socket\n");

ip/ip6tunnel.c

+3-7
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ static void print_tunnel(struct ip6_tnl_parm2 *p)
135135
static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm2 *p)
136136
{
137137
int count = 0;
138-
char medium[IFNAMSIZ];
139-
140-
memset(medium, 0, sizeof(medium));
138+
char medium[IFNAMSIZ] = {};
141139

142140
while (argc > 0) {
143141
if (strcmp(*argv, "mode") == 0) {
@@ -276,9 +274,8 @@ static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm2 *p)
276274
duparg2("name", *argv);
277275
strncpy(p->name, *argv, IFNAMSIZ - 1);
278276
if (cmd == SIOCCHGTUNNEL && count == 0) {
279-
struct ip6_tnl_parm2 old_p;
277+
struct ip6_tnl_parm2 old_p = {};
280278

281-
memset(&old_p, 0, sizeof(old_p));
282279
if (tnl_get_ioctl(*argv, &old_p))
283280
return -1;
284281
*p = old_p;
@@ -351,7 +348,7 @@ static int do_tunnels_list(struct ip6_tnl_parm2 *p)
351348
while (fgets(buf, sizeof(buf), fp) != NULL) {
352349
char name[IFNAMSIZ];
353350
int index, type;
354-
struct ip6_tnl_parm2 p1;
351+
struct ip6_tnl_parm2 p1 = {};
355352
char *ptr;
356353

357354
buf[sizeof(buf) - 1] = '\0';
@@ -372,7 +369,6 @@ static int do_tunnels_list(struct ip6_tnl_parm2 *p)
372369
}
373370
if (type != ARPHRD_TUNNEL6 && type != ARPHRD_IP6GRE)
374371
continue;
375-
memset(&p1, 0, sizeof(p1));
376372
ip6_tnl_parm_init(&p1, 0);
377373
if (type == ARPHRD_IP6GRE)
378374
p1.proto = IPPROTO_GRE;

ip/ipaddress.c

+13-18
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,12 @@ static void print_queuelen(FILE *f, struct rtattr *tb[IFLA_MAX + 1])
173173
if (tb[IFLA_TXQLEN])
174174
qlen = *(int *)RTA_DATA(tb[IFLA_TXQLEN]);
175175
else {
176-
struct ifreq ifr;
176+
struct ifreq ifr = {};
177177
int s = socket(AF_INET, SOCK_STREAM, 0);
178178

179179
if (s < 0)
180180
return;
181181

182-
memset(&ifr, 0, sizeof(ifr));
183182
strcpy(ifr.ifr_name, rta_getattr_str(tb[IFLA_IFNAME]));
184183
if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
185184
fprintf(f, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno));
@@ -1037,10 +1036,8 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
10371036
}
10381037
if (filter.pfx.family) {
10391038
if (rta_tb[IFA_LOCAL]) {
1040-
inet_prefix dst;
1039+
inet_prefix dst = { .family = ifa->ifa_family };
10411040

1042-
memset(&dst, 0, sizeof(dst));
1043-
dst.family = ifa->ifa_family;
10441041
memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
10451042
if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
10461043
return 0;
@@ -1396,10 +1393,10 @@ static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo)
13961393
tb[IFA_LOCAL] = tb[IFA_ADDRESS];
13971394

13981395
if (filter.pfx.family && tb[IFA_LOCAL]) {
1399-
inet_prefix dst;
1396+
inet_prefix dst = {
1397+
.family = ifa->ifa_family
1398+
};
14001399

1401-
memset(&dst, 0, sizeof(dst));
1402-
dst.family = ifa->ifa_family;
14031400
memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
14041401
if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
14051402
continue;
@@ -1845,7 +1842,12 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
18451842
struct nlmsghdr n;
18461843
struct ifaddrmsg ifa;
18471844
char buf[256];
1848-
} req;
1845+
} req = {
1846+
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
1847+
.n.nlmsg_flags = NLM_F_REQUEST | flags,
1848+
.n.nlmsg_type = cmd,
1849+
.ifa.ifa_family = preferred_family,
1850+
};
18491851
char *d = NULL;
18501852
char *l = NULL;
18511853
char *lcl_arg = NULL;
@@ -1860,16 +1862,8 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
18601862
int scoped = 0;
18611863
__u32 preferred_lft = INFINITY_LIFE_TIME;
18621864
__u32 valid_lft = INFINITY_LIFE_TIME;
1863-
struct ifa_cacheinfo cinfo;
18641865
unsigned int ifa_flags = 0;
18651866

1866-
memset(&req, 0, sizeof(req));
1867-
1868-
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1869-
req.n.nlmsg_flags = NLM_F_REQUEST | flags;
1870-
req.n.nlmsg_type = cmd;
1871-
req.ifa.ifa_family = preferred_family;
1872-
18731867
while (argc > 0) {
18741868
if (strcmp(*argv, "peer") == 0 ||
18751869
strcmp(*argv, "remote") == 0) {
@@ -2026,6 +2020,8 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
20262020
}
20272021

20282022
if (valid_lftp || preferred_lftp) {
2023+
struct ifa_cacheinfo cinfo = {};
2024+
20292025
if (!valid_lft) {
20302026
fprintf(stderr, "valid_lft is zero\n");
20312027
return -1;
@@ -2035,7 +2031,6 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
20352031
return -1;
20362032
}
20372033

2038-
memset(&cinfo, 0, sizeof(cinfo));
20392034
cinfo.ifa_prefered = preferred_lft;
20402035
cinfo.ifa_valid = valid_lft;
20412036
addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,

ip/ipaddrlabel.c

+8-13
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,18 @@ static int ipaddrlabel_modify(int cmd, int argc, char **argv)
127127
struct nlmsghdr n;
128128
struct ifaddrlblmsg ifal;
129129
char buf[1024];
130-
} req;
131-
132-
inet_prefix prefix;
130+
} req = {
131+
.n.nlmsg_type = cmd,
132+
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg)),
133+
.n.nlmsg_flags = NLM_F_REQUEST,
134+
.ifal.ifal_family = preferred_family,
135+
};
136+
137+
inet_prefix prefix = {};
133138
uint32_t label = 0xffffffffUL;
134139
char *p = NULL;
135140
char *l = NULL;
136141

137-
memset(&req, 0, sizeof(req));
138-
memset(&prefix, 0, sizeof(prefix));
139-
140-
req.n.nlmsg_type = cmd;
141-
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg));
142-
req.n.nlmsg_flags = NLM_F_REQUEST;
143-
req.ifal.ifal_family = preferred_family;
144-
req.ifal.ifal_prefixlen = 0;
145-
req.ifal.ifal_index = 0;
146-
147142
if (cmd == RTM_NEWADDRLABEL) {
148143
req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
149144
}

0 commit comments

Comments
 (0)