0004-Fix-fread-error-checking.patch (2174B)
- From c3c72b271de395d0e4421e736700d60f96614725 Mon Sep 17 00:00:00 2001
- From: Michael Forney <mforney@mforney.org>
- Date: Sat, 1 May 2021 00:23:39 -0700
- Subject: [PATCH] Fix fread error checking
- fread returns size_t, an unsigned type, so the error check and
- TEMP_FAILURE_RETRY had no effect.
- Instead, errors can be detected when fread returns a partial amount
- and the ferror flag is set.
- ---
- libnetlink.c | 27 ++++++++++++++-------------
- 1 file changed, 14 insertions(+), 13 deletions(-)
- diff --git a/libnetlink.c b/libnetlink.c
- index 24948bf..d94bd5f 100644
- --- a/libnetlink.c
- +++ b/libnetlink.c
- @@ -432,7 +432,7 @@ int rtnl_listen(struct rtnl_handle *rtnl,
- int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
- void *jarg)
- {
- - int status;
- + size_t status;
- struct sockaddr_nl nladdr;
- char buf[8192];
- struct nlmsghdr *h = (void*)buf;
- @@ -442,18 +442,19 @@ int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
- nladdr.nl_pid = 0;
- nladdr.nl_groups = 0;
- - while (1) {
- + while (!feof(rtnl)) {
- int err, len;
- int l;
- - status = TEMP_FAILURE_RETRY ( fread(&buf, 1, sizeof(*h), rtnl) );
- + status = fread(&buf, 1, sizeof(*h), rtnl);
- - if (status < 0) {
- - perror("rtnl_from_file: fread");
- + if (status < sizeof(*h)) {
- + if (ferror(rtnl))
- + perror("rtnl_from_file: fread");
- + else
- + fprintf(stderr, "rtnl_from_file: truncated message\n");
- return -1;
- }
- - if (status == 0)
- - return 0;
- len = h->nlmsg_len;
- l = len - sizeof(*h);
- @@ -466,12 +467,11 @@ int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
- status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
- - if (status < 0) {
- - perror("rtnl_from_file: fread");
- - return -1;
- - }
- - if (status < l) {
- - fprintf(stderr, "rtnl-from_file: truncated message\n");
- + if (status < NLMSG_ALIGN(l)) {
- + if (ferror(rtnl))
- + perror("rtnl_from_file: fread");
- + else
- + fprintf(stderr, "rtnl_from_file: truncated message\n");
- return -1;
- }
- @@ -479,6 +479,7 @@ int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
- if (err < 0)
- return err;
- }
- + return 0;
- }
- int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
- --
- 2.37.3