tools/bpf/bpftool/feature.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
Since both conditions are used to check whether len is valid, we can combine the two conditions into a single if statement
Signed-off-by: Liu Jing <liujing@cmss.chinamobile.com>
---
tools/bpf/bpftool/feature.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/tools/bpf/bpftool/feature.c b/tools/bpf/bpftool/feature.c
index 4dbc4fcdf473..0121e0fd6949 100644
--- a/tools/bpf/bpftool/feature.c
+++ b/tools/bpf/bpftool/feature.c
@@ -158,10 +158,9 @@ static int get_vendor_id(int ifindex)
len = read(fd, buf, sizeof(buf));
close(fd);
- if (len < 0)
- return -1;
- if (len >= (ssize_t)sizeof(buf))
+ if ((len < 0) || (len >= (ssize_t)sizeof(buf)))
return -1;
+
buf[len] = '\0';
return strtol(buf, NULL, 0);
--
2.27.0
2024-10-15 19:09 UTC+0800 ~ Liu Jing <liujing@cmss.chinamobile.com> > Since both conditions are used to check whether len is valid, we can combine the two conditions into a single if statement > Signed-off-by: Liu Jing <liujing@cmss.chinamobile.com> > --- > tools/bpf/bpftool/feature.c | 5 ++--- > 1 file changed, 2 insertions(+), 3 deletions(-) > > diff --git a/tools/bpf/bpftool/feature.c b/tools/bpf/bpftool/feature.c > index 4dbc4fcdf473..0121e0fd6949 100644 > --- a/tools/bpf/bpftool/feature.c > +++ b/tools/bpf/bpftool/feature.c > @@ -158,10 +158,9 @@ static int get_vendor_id(int ifindex) > > len = read(fd, buf, sizeof(buf)); > close(fd); > - if (len < 0) > - return -1; > - if (len >= (ssize_t)sizeof(buf)) > + if ((len < 0) || (len >= (ssize_t)sizeof(buf))) > return -1; > + > buf[len] = '\0'; > > return strtol(buf, NULL, 0); Thanks. I'm not strictly opposed to the change, but it doesn't bring much value in my opinion. I don't think this will "optimize" the statement beyond what the compiler does already. Quentin
On 10/15/24 5:11 AM, Quentin Monnet wrote: > 2024-10-15 19:09 UTC+0800 ~ Liu Jing <liujing@cmss.chinamobile.com> >> Since both conditions are used to check whether len is valid, we can combine >> the two conditions into a single if statement >> Signed-off-by: Liu Jing <liujing@cmss.chinamobile.com> >> --- >> tools/bpf/bpftool/feature.c | 5 ++--- >> 1 file changed, 2 insertions(+), 3 deletions(-) >> >> diff --git a/tools/bpf/bpftool/feature.c b/tools/bpf/bpftool/feature.c >> index 4dbc4fcdf473..0121e0fd6949 100644 >> --- a/tools/bpf/bpftool/feature.c >> +++ b/tools/bpf/bpftool/feature.c >> @@ -158,10 +158,9 @@ static int get_vendor_id(int ifindex) >> len = read(fd, buf, sizeof(buf)); >> close(fd); >> - if (len < 0) >> - return -1; >> - if (len >= (ssize_t)sizeof(buf)) >> + if ((len < 0) || (len >= (ssize_t)sizeof(buf))) >> return -1; >> + >> buf[len] = '\0'; >> return strtol(buf, NULL, 0); > > > Thanks. I'm not strictly opposed to the change, but it doesn't bring much value > in my opinion. I don't think this will "optimize" the statement beyond what the > compiler does already. +1 The current code is good as is. pw-bot: cr
© 2016 - 2024 Red Hat, Inc.