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 2024-06-11 08:54
Reporter alanc View Status public  
Assigned To
Priority normal Resolution Accepted As Marked  
Status Closed  
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 See Note: 0005543
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: finds 1713
software packages referencing asprintf().

GNU C library documentation: [^]

FreeBSD man page (added in 2.2 in 1997): [^]

OpenBSD man page (added in 2.3 in 1998): [^]

NetBSD man page: [^]

Solaris man page (added in Solaris 10 Update 10 in 2011): [^]

illumos man page: [^]
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 issue8
Attached Files pdf file icon tr24731-2.pdf [^] (81,033 bytes) 2021-12-02 16:17

- Relationships

-  Notes
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).
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).
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.
nick (manager)
2021-12-05 12:48
edited on: 2021-12-09 16:39

(Bugnote updated after the 2021-12-09 teleconference):

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

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( ):

Excluding asprintf() and dprintf():

Before line 30754 insert a new paragraph, CX shaded:

The asprintf() function shall be equivalent to sprintf(),
except that the output string shall be 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 call to asprintf() is successful, the address of this dynamically allocated string shall be stored in the location referenced by ptr.

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 the location referenced by 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, but shall not refer to allocated memory.

Before line 31032 add:

[CX]The asprintf() function shall fail if:

[ENOMEM] Insufficient storage space is available.[/CX]

Add CX shading to lines 31032-33.

On page 920, APPLICATION USAGE, after line 31260 add a new paragraph:

The space allocated by a successful call to asprintf() should be subsequently freed
by a call to free().

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

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

At line 70666, change

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


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

At line 70667, change

[CX]dprintf()[/CX], ...


[CX]asprintf(), dprintf()[/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
2021-12-09 16:39 nick Note Edited: 0005543
2021-12-09 16:40 nick Interp Status => ---
2021-12-09 16:40 nick Final Accepted Text => See Note: 0005543
2021-12-09 16:40 nick Status New => Resolved
2021-12-09 16:40 nick Resolution Open => Accepted As Marked
2021-12-09 16:40 nick Tag Attached: issue8
2022-01-13 10:05 geoffclare Status Resolved => Applied
2024-06-11 08:54 agadmin Status Applied => Closed

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