Skip to content

Commit 9b44766

Browse files
author
David Sommerseth
committed
Improved error handling, clean up
1 parent 8d9878c commit 9b44766

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

python-ethtool/etherinfo.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
struct nl_request {
3838
struct nlmsghdr nlmsg_info;
3939
struct ifaddrmsg ifaddrmsg_info;
40-
char buffer[2048];
4140
};
4241

4342
/*
@@ -51,7 +50,9 @@ inline struct etherinfo *new_etherinfo_record()
5150
struct etherinfo *ptr;
5251

5352
ptr = (struct etherinfo *) malloc(sizeof(struct etherinfo)+1);
54-
memset(ptr, 0, sizeof(struct etherinfo)+1);
53+
if( ptr ) {
54+
memset(ptr, 0, sizeof(struct etherinfo)+1);
55+
}
5556

5657
return ptr;
5758
}
@@ -130,12 +131,12 @@ int open_netlink_socket(struct sockaddr_nl *local)
130131

131132
fd = socket(local->nl_family, SOCK_RAW, NETLINK_ROUTE);
132133
if(fd < 0) {
133-
perror("socket\n");
134+
PyErr_SetString(PuExc_OSError, strerror(errno));
134135
return -1;
135136
}
136137

137138
if(bind(fd, (struct sockaddr*) local, sizeof(*local)) < 0) {
138-
perror("sock bind");
139+
PyErr_SetString(PuExc_OSError, strerror(errno));
139140
return -1;
140141
}
141142

@@ -175,7 +176,7 @@ int send_netlink_query(int fd, int get_type)
175176
msg_info.msg_iovlen = 1;
176177

177178
if( sendmsg(fd, &msg_info, 0) < 0 ) {
178-
perror("sendmsg");
179+
PyErr_SetString(PuExc_OSError, strerror(errno));
179180
return 0;
180181
}
181182
return 1;
@@ -213,7 +214,7 @@ int read_netlink_results(int fd, struct sockaddr_nl *local,
213214
if (status < 0) {
214215
if (errno == EINTR || errno == EAGAIN)
215216
continue;
216-
perror("recvmsg");
217+
PyErr_SetString(PuExc_OSError, strerror(errno));
217218
return 0;
218219
}
219220

@@ -240,7 +241,7 @@ int read_netlink_results(int fd, struct sockaddr_nl *local,
240241
fprintf(stderr, "** ERROR ** Error message truncated\n");
241242
} else {
242243
errno = -err->error;
243-
perror("RTNETLINK error");
244+
PyErr_SetString(PuExc_OSError, strerror(errno));
244245
}
245246
return 0;
246247
}
@@ -318,6 +319,9 @@ int etherinfo_proc_getlink(struct nlmsghdr *msg, struct etherinfo *ethinfchain,
318319
if( (*idxptr)->next == NULL ) {
319320
// Append new record if we hit the end of the chain
320321
(*idxptr)->next = new_etherinfo_record();
322+
if( *idxptr == NULL ) {
323+
return 0;
324+
}
321325
}
322326

323327
// Store information
@@ -440,6 +444,9 @@ struct etherinfo *get_etherinfo()
440444

441445
// Create an empty record, where ethernet information will be saved
442446
ethinf = new_etherinfo_record();
447+
if( !ethinf ) {
448+
return NULL;
449+
}
443450

444451
// Get some hardware info - ifname, type and hwaddress. Populates ethinf
445452
if( !send_netlink_query(fd, GET_LINK) ) {

0 commit comments

Comments
 (0)