From bdc674d97ba8b59e22b1f45fa1a37862764fcc75 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 11 Apr 2021 19:06:00 -0700 Subject: Improve documentation for malloc etc. (BZ#27719) Cover key corner cases (e.g., whether errno is set) that are well settled in glibc, fix some examples to avoid integer overflow, and update some other dated examples (code needed for K&R C, e.g.). * manual/charset.texi (Non-reentrant String Conversion): * manual/filesys.texi (Symbolic Links): * manual/memory.texi (Allocating Cleared Space): * manual/socket.texi (Host Names): * manual/string.texi (Concatenating Strings): * manual/users.texi (Setting Groups): Use reallocarray instead of realloc, to avoid integer overflow issues. * manual/filesys.texi (Scanning Directory Content): * manual/memory.texi (The GNU Allocator, Hooks for Malloc): * manual/tunables.texi: Use code font for 'malloc' instead of roman font. (Symbolic Links): Don't assume readlink return value fits in 'int'. * manual/memory.texi (Memory Allocation and C, Basic Allocation) (Malloc Examples, Alloca Example): * manual/stdio.texi (Formatted Output Functions): * manual/string.texi (Concatenating Strings, Collation Functions): Omit pointer casts that are needed only in ancient K&R C. * manual/memory.texi (Basic Allocation): Say that malloc sets errno on failure. Say "convert" rather than "cast", since casts are no longer needed. * manual/memory.texi (Basic Allocation): * manual/string.texi (Concatenating Strings): In examples, use C99 declarations after statements for brevity. * manual/memory.texi (Malloc Examples): Add portability notes for malloc (0), errno setting, and PTRDIFF_MAX. (Changing Block Size): Say that realloc (p, 0) acts like (p ? (free (p), NULL) : malloc (0)). Add xreallocarray example, since other examples can use it. Add portability notes for realloc (0, 0), realloc (p, 0), PTRDIFF_MAX, and improve notes for reallocating to the same size. (Allocating Cleared Space): Reword now-confusing discussion about replacement, and xref "Replacing malloc". * manual/stdio.texi (Formatted Output Functions): Don't assume message size fits in 'int'. * manual/string.texi (Concatenating Strings): Fix undefined behavior involving arithmetic on a freed pointer. --- manual/socket.texi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'manual/socket.texi') diff --git a/manual/socket.texi b/manual/socket.texi index cd7c0e7b12..68c930b552 100644 --- a/manual/socket.texi +++ b/manual/socket.texi @@ -1539,8 +1539,8 @@ gethostname (char *host) &hp, &herr)) == ERANGE) @{ /* Enlarge the buffer. */ + tmphstbuf = reallocarray (tmphstbuf, hstbuflen, 2); hstbuflen *= 2; - tmphstbuf = realloc (tmphstbuf, hstbuflen); @} free (tmphstbuf); -- cgit 1.4.1