public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Corinna Vinschen <vinschen@redhat.com>
To: Keith Packard <keithp@keithp.com>
Cc: newlib@sourceware.org
Subject: Re: [PATCH 2/4] libm/stdlib: don't read past source in nano_realloc
Date: Wed, 12 Aug 2020 10:01:19 +0200	[thread overview]
Message-ID: <20200812080119.GL53219@calimero.vinschen.de> (raw)
In-Reply-To: <20200811230543.2169774-3-keithp@keithp.com>

On Aug 11 16:05, Keith Packard via Newlib wrote:
> Save the computed block size and use it to avoid reading past
> the end of the source block.
> 
> Signed-off-by: Keith Packard <keithp@keithp.com>
> ---
>  newlib/libc/stdlib/nano-mallocr.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c
> index 04465eb9e..cef23977e 100644
> --- a/newlib/libc/stdlib/nano-mallocr.c
> +++ b/newlib/libc/stdlib/nano-mallocr.c
> @@ -466,6 +466,7 @@ void * nano_realloc(RARG void * ptr, malloc_size_t size)
>  {
>      void * mem;
>      chunk * p_to_realloc;
> +    malloc_size_t old_size;
>  
>      if (ptr == NULL) return nano_malloc(RCALL size);
>  
> @@ -477,12 +478,15 @@ void * nano_realloc(RARG void * ptr, malloc_size_t size)
>  
>      /* TODO: There is chance to shrink the chunk if newly requested
>       * size is much small */
> -    if (nano_malloc_usable_size(RCALL ptr) >= size)
> +    old_size = nano_malloc_usable_size(RCALL ptr);
> +    if (old_size >= size)
>        return ptr;

So, after this statement, we can be sure that size > old_size, right?

>      mem = nano_malloc(RCALL size);
>      if (mem != NULL)
>      {
> +	if (size > old_size)

...which makes this condition useless.

> +	    size = old_size;
>          memcpy(mem, ptr, size);

why not just

           memcpy(mem, ptr, old_size);

instead?

>          nano_free(RCALL ptr);
>      }
> -- 
> 2.28.0

Thanks,
Corinna


-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat


  reply	other threads:[~2020-08-12  8:01 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-11 23:05 [PATCH 0/4] Fixes for memory allocation bugs Keith Packard
2020-08-11 23:05 ` [PATCH 1/4] libc/stdlib: Use __builtin_mul_overflow for reallocarray and calloc Keith Packard
2020-08-12  8:13   ` Corinna Vinschen
2020-08-11 23:05 ` [PATCH 2/4] libm/stdlib: don't read past source in nano_realloc Keith Packard
2020-08-12  8:01   ` Corinna Vinschen [this message]
2020-08-11 23:05 ` [PATCH 3/4] libm/stdlib: Realloc when shrinking by 2* or more Keith Packard
2020-08-12  8:08   ` Corinna Vinschen
2020-08-12 15:01     ` Keith Packard
2020-08-13  7:59       ` Corinna Vinschen
2020-08-14  0:48         ` Keith Packard
2020-08-17  9:54           ` Corinna Vinschen
2020-08-11 23:05 ` [PATCH 4/4] libm/stdlib: Recover from realloc failure when shrinking Keith Packard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200812080119.GL53219@calimero.vinschen.de \
    --to=vinschen@redhat.com \
    --cc=keithp@keithp.com \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).