public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: "Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>
Cc: "libc-alpha@sourceware.org" <libc-alpha@sourceware.org>,
	linux-man <linux-man@vger.kernel.org>,
	"Alejandro Colomar (man-pages)" <alx.manpages@gmail.com>,
	Paul Eggert <eggert@cs.ucla.edu>
Subject: Re: Adding a realloc() usage note to the malloc(3) manual page
Date: Tue, 31 Aug 2021 09:07:12 +0200	[thread overview]
Message-ID: <87lf4im6sf.fsf@oldenburg.str.redhat.com> (raw)
In-Reply-To: <CAKgNAkjv2vVDU+z4ActGqo8gcZekCnZFRVESz_hhNjdPvbV96w@mail.gmail.com> (Michael Kerrisk's message of "Tue, 31 Aug 2021 08:00:00 +0200")

* Michael Kerrisk:

>    Correct usage of realloc() (and reallocarray())
>        Since, on the one hand, realloc() (and  reallocarray())  may  move
>        the block of memory, and on the other, it may return NULL on fail‐
>        ure and leave the memory contents and location unchanged,  correct
>        usage is something like the following:
>
>            void *ptr, *nptr;
>            ptr = malloc(origsize);
>            ...
>            /* In the following, we presume 'newsize' is not 0.
>               (If 'newsize' is zero, realloc() may return NULL,
>               and that is not an error.) */
>
>            nptr = realloc(ptr, newsize);
>            if (nptr == NULL) {
>                /* Handle error; the block pointed to by 'ptr' is
>                   still usable. */
>            } else {
>                /* realloc() succeeded; update 'ptr' to point to
>                   the (possibly moved) block. */
>                ptr = nptr;
>            }

Maybe add this to the else branch: “The original value of ptr itself and
the memory it pointed to are invalid at this point.”

That is, the somewhat common idiom of adjusting internal pointers in the
allocation to point to the new allocation is invalid.

Thanks,
Florian


  reply	other threads:[~2021-08-31  7:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-31  6:00 Adding a realloc() usage note to the malloc(3) manual page Michael Kerrisk (man-pages)
2021-08-31  7:07 ` Florian Weimer [this message]
2021-08-31  9:29   ` Paul Eggert
2021-09-02  0:21     ` Michael Kerrisk (man-pages)
2021-09-02  1:23       ` Paul Eggert
2021-09-02  5:22         ` Michael Kerrisk (man-pages)
2021-09-02  7:46           ` Paul Eggert

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=87lf4im6sf.fsf@oldenburg.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=alx.manpages@gmail.com \
    --cc=eggert@cs.ucla.edu \
    --cc=libc-alpha@sourceware.org \
    --cc=linux-man@vger.kernel.org \
    --cc=mtk.manpages@gmail.com \
    /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