malloc: Print error when oldsize is not equal to the current size.

Message ID 6c575d27-3d31-3d59-33b3-403ca9db03ab@huawei.com
State Accepted, archived
Delegated to: DJ Delorie
Headers
Series malloc: Print error when oldsize is not equal to the current size. |

Commit Message

Qingqing Li March 26, 2021, 11:05 a.m. UTC
  This is used to detect errors early.

---
 malloc/malloc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

Qingqing Li April 1, 2021, 8:51 a.m. UTC | #1
ping again. 

the read of the oldsize is not protected by any lock, so check this value to avoid causing bigger mistakes. 

On 2021/3/26 19:05, liqingqing wrote:
>  This is used to detect errors early.
>
> ---
>  malloc/malloc.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/malloc/malloc.c b/malloc/malloc.c
> index 530c792997..243be0da68 100644
> --- a/malloc/malloc.c
> +++ b/malloc/malloc.c
> @@ -4784,7 +4784,8 @@ _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize,
>  
>    /* oldmem size */
>    if (__builtin_expect (chunksize_nomask (oldp) <= CHUNK_HDR_SZ, 0)
> -      || __builtin_expect (oldsize >= av->system_mem, 0))
> +      || __builtin_expect (oldsize >= av->system_mem, 0)
> +      || __builtin_expect (oldsize != chunksize (oldp), 0))
>      malloc_printerr ("realloc(): invalid old size");
>  
>    check_inuse_chunk (av, oldp);
  
DJ Delorie Sept. 22, 2022, 7:51 p.m. UTC | #2
LGTM; committed.  Sorry for the delay.

Reviewed-by: DJ Delorie <dj@redhat.com>
  

Patch

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 530c792997..243be0da68 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -4784,7 +4784,8 @@  _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize,
 
   /* oldmem size */
   if (__builtin_expect (chunksize_nomask (oldp) <= CHUNK_HDR_SZ, 0)
-      || __builtin_expect (oldsize >= av->system_mem, 0))
+      || __builtin_expect (oldsize >= av->system_mem, 0)
+      || __builtin_expect (oldsize != chunksize (oldp), 0))
     malloc_printerr ("realloc(): invalid old size");
 
   check_inuse_chunk (av, oldp);