Austin Group Defect Tracker

Aardvark Mark IV


Viewing Issue Simple Details Jump to Notes ] Issue History ] Print ]
ID Category Severity Type Date Submitted Last Update
0001496 [1003.1(2013)/Issue7+TC1] System Interfaces Comment Enhancement Request 2021-08-01 17:09 2021-12-05 12:48
Reporter alanc View Status public  
Assigned To
Priority normal Resolution Open  
Status New  
Name Alan Coopersmith
Organization Oracle Solaris Engineering
User Reference
Section printf()
Page Number (page or range of pages)
Line Number (Line or range of lines)
Interp Status ---
Final Accepted Text
Summary 0001496: Add asprintf() and vasprintf()
Description The GNU C library has provided asprintf() and vasprintf() since at least 1991,
and many other OS'es have added them as well since. These variants of sprintf()
allocate new memory for the string, making it easier to have a safe usage
without having to re-implement much of the printf format string handling logic
to determine how much memory to allocate. These have been widely adopted in
software trying to avoid buffer overflows: codesearch.debian.net finds 1713
software packages referencing asprintf().

GNU C library documentation:
https://www.gnu.org/software/libc/manual/html_node/Dynamic-Output.html [^]

FreeBSD man page (added in 2.2 in 1997):
https://www.freebsd.org/cgi/man.cgi?query=asprintf [^]

OpenBSD man page (added in 2.3 in 1998):
https://man.openbsd.org/asprintf.3 [^]

NetBSD man page:
https://man.netbsd.org/asprintf.3 [^]

Solaris man page (added in Solaris 10 Update 10 in 2011):
https://docs.oracle.com/cd/E88353_01/html/E37843/asprintf-3c.html [^]

illumos man page:
https://illumos.org/man/3c/asprintf [^]
Desired Action These functions should be added to the standard:

#include <stdio.h>

int asprintf(char **restrict strp, const char *restrict fmt, ...);
int vasprintf(char **restrict strp, const char *restrict fmt,
              va_list ap);
Tags No tags attached.
Attached Files pdf file icon tr24731-2.pdf [^] (81,033 bytes) 2021-12-02 16:17

- Relationships

-  Notes
(0005427)
jsm28 (reporter)
2021-08-02 16:31

Note that these functions (and corresponding aswprintf, vaswprintf) are in ISO/IEC TR 24731-2:2010, so care should be taken to be compatible with the versions there and to raise any issues with the interfaces through the liaison with WG14.

Note also the known error handling issue (it's unspecified whether *ptr is set to NULL on error or left unchanged - apparently this is deliberate in TR 24731-2 to be compatible with multiple existing implementations that make different choices there).
(0005428)
alanc (reporter)
2021-08-03 19:19

Thanks, I was unaware that asprintf() had been specified in that ISO TR.

Of the above listed implementations, all but GNU libc document that they will
set *ptr to NULL on error, while GNU libc explicitly states that you cannot
rely on it to do so (and looking at its code, sometimes it does, sometimes
it doesn't).
(0005540)
nick (manager)
2021-12-02 16:19

I have attached the final draft of TR24731-2, and since this is an ISO document, it is sponsored by ISO.
(0005543)
nick (manager)
2021-12-05 12:48
edited on: 2021-12-05 12:50

Add pointer page for asprintf referencing fprintf.
Add pointer page for vasprintf referencing vprintf.

On p909 line 30737 add asprintf to the title line.

On P 909, before line 30740, add
[CX]<tt>int asprintf(char ** restrict ptr, const char * restrict format, ...);</tt>

(the next line, dprintf() is already CX shaded, so this extends the shading backwards).

On line 30747, change
Excluding dprintf( ):

to
Excluding asprintf() and dprintf():


Before line 30754 insert a new paragraph, CX shaded:

The asprintf() function behaves as sprintf(),
except that the output string is written to
dynamically allocated memory, allocated as if by a call to malloc(), of sufficient
length to hold the resulting string, including a terminating null byte.
If the allocation is successful, the address of this dynamically allocated
string shall be stored in the location referenced by ptr.


At page 915 line 31014, change
Upon successful completion, the [CX]dprintf(),[/CX]

to
Upon successful completion, the [CX]asprintf(), dprintf(),[/CX]


Before line 31016, Add a new paragraph, CX shaded:

Upon successful completion, the asprintf() function shall return the number of bytes written to
the allocated string stored in ptr>, excluding the terminating null byte.


At line 31021 append a sentence CX shaded:

For asprintf(), if memory allocation was not possible, or if some other
error occurs, the function shall return a negative value, and the contents of
ptr are undefined.


Before line 31032 add (nb, 31032 is not CX shaded, so unclear as to shading for this):

The asprintf() function shall fail if:

[ENOMEM] Insufficient storage space is available.



On page 2212, line 70651 add vasprintf to the title line.

Insert before line 70655 (extending the CX shading backwards a line)
[CX]
<tt>int vasprintf(char ** restrict ptr, const char * restrict format, va_list arg);</tt>


At line 70666, change

The [CX}vdprintf()[/CX], ...

to

The [CX}vasprintf(), vdprintf()[/CX], ...


Though the ISO TR includes aswprintf, no current implementations appear to
provide this, so it is not included here. Additionally, such functionality
is available using open_wmemstream, so invention here is really unecessary.


- Issue History
Date Modified Username Field Change
2021-08-01 17:09 alanc New Issue
2021-08-01 17:09 alanc Name => Alan Coopersmith
2021-08-01 17:09 alanc Organization => Oracle Solaris Engineering
2021-08-01 17:09 alanc Section => printf()
2021-08-01 17:09 alanc Page Number => (page or range of pages)
2021-08-01 17:09 alanc Line Number => (Line or range of lines)
2021-08-02 16:31 jsm28 Note Added: 0005427
2021-08-03 19:19 alanc Note Added: 0005428
2021-12-02 16:17 nick File Added: tr24731-2.pdf
2021-12-02 16:19 nick Note Added: 0005540
2021-12-05 12:48 nick Note Added: 0005543
2021-12-05 12:49 nick Note Edited: 0005543
2021-12-05 12:49 nick Note Edited: 0005543
2021-12-05 12:50 nick Note View State: public: 5543
2021-12-05 12:50 nick Note Edited: 0005543


Mantis 1.1.6[^]
Copyright © 2000 - 2008 Mantis Group
Powered by Mantis Bugtracker