All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH iproute2] libnetlink.c, ss.c: properly handle fread() error
@ 2019-10-24 21:20 Michał Łyszczek
  2019-10-29  4:21 ` Stephen Hemminger
  0 siblings, 1 reply; 5+ messages in thread
From: Michał Łyszczek @ 2019-10-24 21:20 UTC (permalink / raw)
  To: netdev; +Cc: Michał Łyszczek

fread(3) returns size_t data type which is unsigned, thus check
`if (fread(...) < 0)' is always false. To check if fread(3) has
failed, user should check if return is 0 and then check error
indicator with ferror(3).

Signed-off-by: Michał Łyszczek <michal.lyszczek@bofc.pl>
---
 lib/libnetlink.c | 6 +++---
 misc/ss.c        | 7 ++++---
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/lib/libnetlink.c b/lib/libnetlink.c
index 6ce8b199..76c383f9 100644
--- a/lib/libnetlink.c
+++ b/lib/libnetlink.c
@@ -1174,7 +1174,7 @@ int rtnl_listen(struct rtnl_handle *rtnl,
 int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
 		   void *jarg)
 {
-	int status;
+	size_t status;
 	char buf[16384];
 	struct nlmsghdr *h = (struct nlmsghdr *)buf;
 
@@ -1184,7 +1184,7 @@ int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
 
 		status = fread(&buf, 1, sizeof(*h), rtnl);
 
-		if (status < 0) {
+		if (status == 0 && ferror(rtnl)) {
 			if (errno == EINTR)
 				continue;
 			perror("rtnl_from_file: fread");
@@ -1204,7 +1204,7 @@ int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler,
 
 		status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
 
-		if (status < 0) {
+		if (status == 0 && ferror(rtnl)) {
 			perror("rtnl_from_file: fread");
 			return -1;
 		}
diff --git a/misc/ss.c b/misc/ss.c
index 363b4c8d..769332e9 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -3329,12 +3329,13 @@ static int tcp_show_netlink_file(struct filter *f)
 	}
 
 	while (1) {
-		int status, err2;
+		int err2;
+		size_t status;
 		struct nlmsghdr *h = (struct nlmsghdr *)buf;
 		struct sockstat s = {};
 
 		status = fread(buf, 1, sizeof(*h), fp);
-		if (status < 0) {
+		if (status == 0 && ferror(fp)) {
 			perror("Reading header from $TCPDIAG_FILE");
 			break;
 		}
@@ -3345,7 +3346,7 @@ static int tcp_show_netlink_file(struct filter *f)
 
 		status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp);
 
-		if (status < 0) {
+		if (status == 0 && ferror(fp)) {
 			perror("Reading $TCPDIAG_FILE");
 			break;
 		}
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-11-01 16:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-10-24 21:20 [PATCH iproute2] libnetlink.c, ss.c: properly handle fread() error Michał Łyszczek
2019-10-29  4:21 ` Stephen Hemminger
2019-10-29 11:13   ` [PATCH v2 iproute2] libnetlink.c, ss.c: properly handle fread() errors Michał Łyszczek
2019-11-01 16:38     ` Stephen Hemminger
2019-10-29 11:35   ` [PATCH iproute2] libnetlink.c, ss.c: properly handle fread() error michal.lyszczek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.