mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ELF: use __builtin_mul_overflow() more
@ 2023-04-21 18:54 Alexey Dobriyan
  2023-04-21 19:39 ` Andrew Morton
  0 siblings, 1 reply; 3+ messages in thread
From: Alexey Dobriyan @ 2023-04-21 18:54 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel

__builtin_mul_overflow() can do multiplication and overflow check
in one line.

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

 fs/binfmt_elf.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -1651,9 +1651,8 @@ static int fill_files_note(struct memelfnote *note, struct coredump_params *cprm
 
 	/* *Estimated* file count and total data size needed */
 	count = cprm->vma_count;
-	if (count > UINT_MAX / 64)
+	if (__builtin_mul_overflow(count, 64, &size))
 		return -EINVAL;
-	size = count * 64;
 
 	names_ofs = (2 + 3 * count) * sizeof(data[0]);
  alloc:

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ELF: use __builtin_mul_overflow() more
  2023-04-21 18:54 [PATCH] ELF: use __builtin_mul_overflow() more Alexey Dobriyan
@ 2023-04-21 19:39 ` Andrew Morton
  2023-04-22  9:45   ` Alexey Dobriyan
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2023-04-21 19:39 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: linux-kernel

On Fri, 21 Apr 2023 21:54:36 +0300 Alexey Dobriyan <adobriyan@gmail.com> wrote:

> __builtin_mul_overflow() can do multiplication and overflow check
> in one line.
> 
> --- a/fs/binfmt_elf.c
> +++ b/fs/binfmt_elf.c
> @@ -1651,9 +1651,8 @@ static int fill_files_note(struct memelfnote *note, struct coredump_params *cprm
>  
>  	/* *Estimated* file count and total data size needed */
>  	count = cprm->vma_count;
> -	if (count > UINT_MAX / 64)
> +	if (__builtin_mul_overflow(count, 64, &size))
>  		return -EINVAL;
> -	size = count * 64;

Huh, what the heck is that ;)


include/linux/overflow.h has check_mul_overflow() for us to use here.


tools/lib/bpf/libbpf_internal.h uses

	#if __has_builtin(__builtin_mul_overflow)

but check_mul_overflow() didn't bother testing for availability. 
Probably tools/lib/bpf/libbpf_internal.h should just use
check_mul_overflow().



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ELF: use __builtin_mul_overflow() more
  2023-04-21 19:39 ` Andrew Morton
@ 2023-04-22  9:45   ` Alexey Dobriyan
  0 siblings, 0 replies; 3+ messages in thread
From: Alexey Dobriyan @ 2023-04-22  9:45 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

On Fri, Apr 21, 2023 at 12:39:11PM -0700, Andrew Morton wrote:
> On Fri, 21 Apr 2023 21:54:36 +0300 Alexey Dobriyan <adobriyan@gmail.com> wrote:
> 
> > __builtin_mul_overflow() can do multiplication and overflow check
> > in one line.
> > 
> > --- a/fs/binfmt_elf.c
> > +++ b/fs/binfmt_elf.c
> > @@ -1651,9 +1651,8 @@ static int fill_files_note(struct memelfnote *note, struct coredump_params *cprm
> >  
> >  	/* *Estimated* file count and total data size needed */
> >  	count = cprm->vma_count;
> > -	if (count > UINT_MAX / 64)
> > +	if (__builtin_mul_overflow(count, 64, &size))
> >  		return -EINVAL;
> > -	size = count * 64;
> 
> Huh, what the heck is that ;)
> 
> 
> include/linux/overflow.h has check_mul_overflow() for us to use here.

Oh, no, wrappers.

> tools/lib/bpf/libbpf_internal.h uses
> 
> 	#if __has_builtin(__builtin_mul_overflow)
> 
> but check_mul_overflow() didn't bother testing for availability. 

gcc 5.1 has __builtin_mul_overflow()

> Probably tools/lib/bpf/libbpf_internal.h should just use
> check_mul_overflow().

I don't know, this is userspace stuff.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-04-22  9:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-21 18:54 [PATCH] ELF: use __builtin_mul_overflow() more Alexey Dobriyan
2023-04-21 19:39 ` Andrew Morton
2023-04-22  9:45   ` Alexey Dobriyan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®