From 80ba361b2fb61b8f500df04dc6bf6bf86631f8d0 Mon Sep 17 00:00:00 2001 From: Konstantin Belousov Date: Tue, 3 Nov 2020 14:33:04 +0000 Subject: [PATCH] if_media.c SIOCGMEDIAX handler: improve loop Stop advancing counter past the current iteration number at the start of iteration. This removes the need of subtracting one when calculating index for copyout, and arguably fixes off-by-one reporting of copied out elements when copyout failed. Reviewed by: hselasky Sponsored by: Mellanox Technologies / NVidia Networking MFC after: 1 week Differential revision: https://reviews.freebsd.org/D27073 --- sys/net/if_media.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/sys/net/if_media.c b/sys/net/if_media.c index 1b06d51f371..4100aaab952 100644 --- a/sys/net/if_media.c +++ b/sys/net/if_media.c @@ -300,15 +300,17 @@ ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm, * allocate. */ i = 0; - LIST_FOREACH(ep, &ifm->ifm_list, ifm_list) - if (i++ < ifmr->ifm_count) { + LIST_FOREACH(ep, &ifm->ifm_list, ifm_list) { + if (i < ifmr->ifm_count) { error = copyout(&ep->ifm_media, - ifmr->ifm_ulist + i - 1, sizeof(int)); - if (error) + ifmr->ifm_ulist + i, sizeof(int)); + if (error != 0) break; } + i++; + } if (error == 0 && i > ifmr->ifm_count) - error = ifmr->ifm_count ? E2BIG : 0; + error = ifmr->ifm_count != 0 ? E2BIG : 0; ifmr->ifm_count = i; break; }