public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx.manpages@gmail.com>
To: linux-man@vger.kernel.org
Cc: Alejandro Colomar <alx@kernel.org>,
	Mingye Wang <arthur200126@gmail.com>,
	Siddhesh Poyarekar <siddhesh@gotplt.org>,
	DJ Delorie <dj@redhat.com>, Sam James <sam@gentoo.org>,
	Florian Weimer <fweimer@redhat.com>,
	Andreas Schwab <schwab@linux-m68k.org>,
	Zack Weinberg <zack@owlfolio.org>,
	Wilco Dijkstra <Wilco.Dijkstra@arm.com>
Subject: [PATCH] malloc_usable_size.3: The returned value should not be trusted
Date: Wed,  5 Apr 2023 23:19:26 +0200	[thread overview]
Message-ID: <20230405211925.32070-1-alx@kernel.org> (raw)

It might very well return a value larger than the actual usable size, so
writing to the excess bytes is Undefined Behavior.  There's absolutely
no promise about the value, except that it is no less than the size
that was once passed to malloc(3).

Link: <https://github.com/systemd/systemd/issues/22801#issuecomment-1343041481>
Link: <https://inbox.sourceware.org/libc-alpha/20221124213258.305192-1-siddhesh@gotplt.org/T/>
Reported-by: Mingye Wang <arthur200126@gmail.com>
Reported-by: Siddhesh Poyarekar <siddhesh@gotplt.org>
Cc: DJ Delorie <dj@redhat.com>
Cc: Sam James <sam@gentoo.org>
Cc: Florian Weimer <fweimer@redhat.com>
Cc: Andreas Schwab <schwab@linux-m68k.org>
Cc: Zack Weinberg <zack@owlfolio.org>
Cc: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
 man3/malloc_usable_size.3 | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)

diff --git a/man3/malloc_usable_size.3 b/man3/malloc_usable_size.3
index 754b255de..f96f1abb5 100644
--- a/man3/malloc_usable_size.3
+++ b/man3/malloc_usable_size.3
@@ -13,20 +13,17 @@ .SH SYNOPSIS
 .nf
 .B #include <malloc.h>
 .PP
-.BI "size_t malloc_usable_size(void *" ptr );
+.BI "size_t malloc_usable_size(void *_Nullable " ptr );
 .fi
 .SH DESCRIPTION
-The
-.BR malloc_usable_size ()
-function returns the number of usable bytes in the block pointed to by
-.IR ptr ,
-a pointer to a block of memory allocated by
+This function can be used for
+diagnostics or statistics about allocations from
 .BR malloc (3)
 or a related function.
 .SH RETURN VALUE
 .BR malloc_usable_size ()
-returns the number of usable bytes in
-the block of allocated memory pointed to by
+returns a value no less than
+the size of the block of allocated memory pointed to by
 .IR ptr .
 If
 .I ptr
@@ -50,17 +47,17 @@ .SH ATTRIBUTES
 .sp 1
 .SH STANDARDS
 GNU.
-.SH NOTES
+.SH CAVEATS
 The value returned by
 .BR malloc_usable_size ()
-may be greater than the requested size of the allocation because
-of alignment and minimum size constraints.
-Although the excess bytes can be overwritten by the application
-without ill effects,
-this is not good programming practice:
-the number of excess bytes in an allocation depends on
-the underlying implementation.
-.PP
-The main use of this function is for debugging and introspection.
+may be greater than the requested size of the allocation
+because of various internal implementation details,
+none of which the programmer should rely on.
+This function is intended to only be used
+for diagnostics and statistics;
+writing to the excess memory without first calling
+.BR realloc (3)
+to resize the allocation is not supported.
+The returned value is only valid at the time of the call.
 .SH SEE ALSO
 .BR malloc (3)
-- 
2.40.0


                 reply	other threads:[~2023-04-05 21:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20230405211925.32070-1-alx@kernel.org \
    --to=alx.manpages@gmail.com \
    --cc=Wilco.Dijkstra@arm.com \
    --cc=alx@kernel.org \
    --cc=arthur200126@gmail.com \
    --cc=dj@redhat.com \
    --cc=fweimer@redhat.com \
    --cc=linux-man@vger.kernel.org \
    --cc=sam@gentoo.org \
    --cc=schwab@linux-m68k.org \
    --cc=siddhesh@gotplt.org \
    --cc=zack@owlfolio.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