reduce size by de-duplicate netlink message length check

function                                             old     new   delta
check_nlmsg_len                                        -      22     +22
print_route                                         1901    1893      -8
print_neigh                                         1082    1073      -9
iproute_get                                          889     880      -9
.rodata                                           139741  139723     -18
------------------------------------------------------------------------------

v2:
 - use void instead of int for check_nlmsg_len()

(add/remove: 1/0 grow/shrink: 0/4 up/down: 22/-44)            Total: -22
bytes
   text    data     bss     dec     hex filename
 876539   15636    2080  894255   da52f busybox_old
 876517   15636    2080  894233   da519 busybox_unstripped
---
 networking/libiproute/ip_common.h |  1 +
 networking/libiproute/ipneigh.c   |  4 +---
 networking/libiproute/iproute.c   | 14 +++++++++-----
 3 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/networking/libiproute/ip_common.h 
b/networking/libiproute/ip_common.h
index 40171bed9..15f9bb4df 100644
--- a/networking/libiproute/ip_common.h
+++ b/networking/libiproute/ip_common.h
@@ -30,6 +30,7 @@ int FAST_FUNC do_iplink(char **argv);
 //int FAST_FUNC do_ipmonitor(char **argv);
 //int FAST_FUNC do_multiaddr(char **argv);
 //int FAST_FUNC do_multiroute(char **argv);
+void FAST_FUNC check_nlmsg_len(int len);
 
 POP_SAVED_FUNCTION_VISIBILITY
 
diff --git a/networking/libiproute/ipneigh.c b/networking/libiproute/ipneigh.c
index b9b4f4b31..8771a9ad9 100644
--- a/networking/libiproute/ipneigh.c
+++ b/networking/libiproute/ipneigh.c
@@ -102,9 +102,7 @@ static int FAST_FUNC print_neigh(const struct sockaddr_nl 
*who UNUSED_PARAM,
                                     n->nlmsg_flags);
        }
        len -= NLMSG_LENGTH(sizeof(*r));
-       if (len < 0) {
-               bb_error_msg_and_die("BUG: wrong nlmsg len %d", len);
-       }
+       check_nlmsg_len(len);
 
        if (G_filter.flushb && n->nlmsg_type != RTM_NEWNEIGH)
                return 0;
diff --git a/networking/libiproute/iproute.c b/networking/libiproute/iproute.c
index 5a972f8b2..9a58fdbb5 100644
--- a/networking/libiproute/iproute.c
+++ b/networking/libiproute/iproute.c
@@ -64,6 +64,13 @@ static int flush_update(void)
        return 0;
 }
 
+void check_nlmsg_len(int len)
+{
+       if (len < 0) {
+               bb_error_msg_and_die("wrong nlmsg len %d", len);
+       }
+}
+
 static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
                struct nlmsghdr *n, void *arg UNUSED_PARAM)
 {
@@ -83,8 +90,7 @@ static int FAST_FUNC print_route(const struct sockaddr_nl 
*who UNUSED_PARAM,
        if (G_filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
                return 0;
        len -= NLMSG_LENGTH(sizeof(*r));
-       if (len < 0)
-               bb_error_msg_and_die("wrong nlmsg len %d", len);
+       check_nlmsg_len(len);
 
        //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
        parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
@@ -1080,9 +1086,7 @@ static int iproute_get(char **argv)
                        bb_simple_error_msg_and_die("not a route?");
                }
                len -= NLMSG_LENGTH(sizeof(*r));
-               if (len < 0) {
-                       bb_error_msg_and_die("wrong len %d", len);
-               }
+               check_nlmsg_len(len);
 
                //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
                parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
-- 
2.25.0

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to