diff mbox series

[5/5] modpost: squash if...else if in find_elf_symbol2()

Message ID 20220523164626.858340-5-masahiroy@kernel.org (mailing list archive)
State New, archived
Headers show
Series [1/5] modpost: fix undefined behavior of is_arm_mapping_symbol() | expand

Commit Message

Masahiro Yamada May 23, 2022, 4:46 p.m. UTC
if ((addr - sym->st_value) < distance) {
            distance = addr - sym->st_value;
            near = sym;
    } else if ((addr - sym->st_value) == distance) {
            near = sym;
    }

is equivalent to:

    if ((addr - sym->st_value) <= distance) {
            distance = addr - sym->st_value;
            near = sym;
    }

(The else-if part can overwrite 'distance' with the same value).

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 scripts/mod/modpost.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

Comments

Nick Desaulniers May 24, 2022, 8:51 p.m. UTC | #1
On Mon, May 23, 2022 at 9:48 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
>     if ((addr - sym->st_value) < distance) {
>             distance = addr - sym->st_value;
>             near = sym;
>     } else if ((addr - sym->st_value) == distance) {
>             near = sym;
>     }
>
> is equivalent to:
>
>     if ((addr - sym->st_value) <= distance) {
>             distance = addr - sym->st_value;
>             near = sym;
>     }
>
> (The else-if part can overwrite 'distance' with the same value).
>
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Sure, thanks for the patch!
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

> ---
>
>  scripts/mod/modpost.c | 10 +++-------
>  1 file changed, 3 insertions(+), 7 deletions(-)
>
> diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
> index 48a18b59f908..8c8d2a4bc0b0 100644
> --- a/scripts/mod/modpost.c
> +++ b/scripts/mod/modpost.c
> @@ -1270,13 +1270,9 @@ static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
>                         continue;
>                 if (!is_valid_name(elf, sym))
>                         continue;
> -               if (sym->st_value <= addr) {
> -                       if ((addr - sym->st_value) < distance) {
> -                               distance = addr - sym->st_value;
> -                               near = sym;
> -                       } else if ((addr - sym->st_value) == distance) {
> -                               near = sym;
> -                       }
> +               if (sym->st_value <= addr && addr - sym->st_value <= distance) {
> +                       distance = addr - sym->st_value;
> +                       near = sym;
>                 }
>         }
>         return near;
> --
> 2.32.0
>
diff mbox series

Patch

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 48a18b59f908..8c8d2a4bc0b0 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1270,13 +1270,9 @@  static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
 			continue;
 		if (!is_valid_name(elf, sym))
 			continue;
-		if (sym->st_value <= addr) {
-			if ((addr - sym->st_value) < distance) {
-				distance = addr - sym->st_value;
-				near = sym;
-			} else if ((addr - sym->st_value) == distance) {
-				near = sym;
-			}
+		if (sym->st_value <= addr && addr - sym->st_value <= distance) {
+			distance = addr - sym->st_value;
+			near = sym;
 		}
 	}
 	return near;