mailing list of musl libc
 help / color / mirror / Atom feed
From: Benedict <>
To: musl <>
Subject: [musl] realloc buffer overflow issue
Date: Sun, 28 Feb 2021 23:54:58 +0800 (CST)
Message-ID: <> (raw)

[-- Attachment #1: Type: text/plain, Size: 1368 bytes --]

I found an memory leak issue in my multi-thread, which had fixed issue by the following patch

about this patch, I think it involved a bug for realloc function: when the request size 'n' (the second paramter passed to realloc) lesser than the original memory size 'n0' (the memory block that first paramter of realloc pointed to) and the next chunk of  'self ' is in used state, the realloc should call trim(self, n) to split current chunk rather than call malloc, that will cause memcpy buffer overflow due to 'n < n0', and it will cause next malloc crash...

after fix, I think it should be like:

size_t nsize = next->csize & C_INUSE ? 0 : CHUNK_SIZE(next);
if (nsize)) {
int i = bin_index(nsize);
if (!(next->csize & C_INUSE)) {
unbin(next, i);
next = NEXT_CHUNK(next);
self->csize = next->psize = n0+nsize | C_INUSE;
    if(CHUNK_SIZE(self) >= n) {
        trim(self, n);
return CHUNK_TO_MEM(self);

/* As a last resort, allocate a new chunk and copy to it. */
new = malloc(n-OVERHEAD);
if (!new) return 0;
memcpy(new, p, n0-OVERHEAD);
return new;


[-- Attachment #2: Type: text/html, Size: 8992 bytes --]

             reply	other threads:[~2021-02-28 15:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-28 15:54 Benedict [this message]
2021-02-28 16:48 ` Rich Felker

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:

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

  git send-email \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

mailing list of musl libc

This inbox may be cloned and mirrored by anyone:

	git clone --mirror

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 musl musl/ \
	public-inbox-index musl

Example config snippet for mirrors.
Newsgroup available over NNTP:

code repositories for the project(s) associated with this inbox:

AGPL code for this site: git clone