public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Florian Weimer <fweimer@redhat.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>,
	"Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>
Subject: Re: Adding a realloc() usage note to the malloc(3) manual page
Date: Tue, 31 Aug 2021 02:29:41 -0700	[thread overview]
Message-ID: <03f3b96f-1dd4-e9cb-2f24-7fc8ae7252bc@cs.ucla.edu> (raw)
In-Reply-To: <87lf4im6sf.fsf@oldenburg.str.redhat.com>

On 8/31/21 12:07 AM, Florian Weimer wrote:
> the somewhat common idiom of adjusting internal pointers in the
> allocation to point to the new allocation is invalid.

Good point. Also, the example call to malloc should check the return value.

Something like this, perhaps:

   char *ptr = malloc(origsize);
   if (ptr == NULL)
     return NULL;
   char *p = ptr + some_random_value();

   /* In the following, we presume 'newsize' is not 0.
      (If 'newsize' is zero, realloc() may return NULL,
      and that is not an error.) */

   ptrdiff_t p_offset = p - ptr;
   char *nptr = realloc(ptr, newsize);
   if (nptr == NULL) {
     /* Handle error; the block pointed to by 'ptr' is
        still usable. */
   } else {
     /* realloc() succeeded; update 'ptr' and 'p' to point to
        the (possibly moved) block.  'p += nptr - ptr; ptr = nptr;'
        would be invalid here, since 'ptr' is invalid immediately
        after the successful realloc().  */
     ptr = nptr;
     p = nptr + p_offset;
   }

  reply	other threads:[~2021-08-31  9:29 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
2021-08-31  9:29   ` Paul Eggert [this message]
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=03f3b96f-1dd4-e9cb-2f24-7fc8ae7252bc@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=alx.manpages@gmail.com \
    --cc=fweimer@redhat.com \
    --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