A NOTE has been added to this issue. ====================================================================== http://austingroupbugs.net/view.php?id=374 ====================================================================== Reported By: eblake Assigned To: ajosey ====================================================================== Project: 1003.1(2008)/Issue 7 Issue ID: 374 Category: System Interfaces Type: Omission Severity: Objection Priority: normal Status: Under Review Name: Eric Blake Organization: Red Hat User Reference: ebb.malloc Section: malloc Page Number: 1268 Line Number: 41715 Interp Status: --- Final Accepted Text: ====================================================================== Date Submitted: 2011-02-03 17:12 UTC Last Modified: 2019-08-02 11:18 UTC ====================================================================== Summary: malloc(0) and realloc(p,0) must not change errno on success ====================================================================== Relationships ID Summary ---------------------------------------------------------------------- related to 0000526 Adopt C99 wording for zero size calloc(... related to 0000688 realloc(NULL, 0) vs. malloc(0) child of 0000400 realloc wording conflicts with C99 ======================================================================
---------------------------------------------------------------------- (0004507) geoffclare (manager) - 2019-08-02 11:18 http://austingroupbugs.net/view.php?id=374#c4507 ---------------------------------------------------------------------- We need to decide how to handle the overlap between this bug and the work to align Issue 8 with C17. A major rewrite of the realloc() DESCRIPTION and RETURN VALUE sections is needed to align with C17. This will mean most of the changes requested in http://austingroupbugs.net/view.php?id=374#c708 are taken care of by that. The only thing left for this bug to do is to address any changes to CX functionality that we want to make. The original request to leave errno unchanged when a memory allocation function is called with size zero and a null pointer is returned because the implementation did not attempt an allocation appears to conflict with C17, which says (7.22.3 para 1; emphasis added):<blockquote>If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned <b>to indicate an error</b>, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.</blockquote> So if we want applications to be able to distinguish between implementations that attempt allocation and those that don't, we will need to specify two different non-zero errno values -- http://austingroupbugs.net/view.php?id=374#c708 already suggests using EINVAL for this for realloc() and this seems like an appropriate way to handle it in all the functions. Note also that C17 makes calling realloc() with size 0 an obsolescent feature. I suggest that we should resolve this bug first and then the C17 alignment changes can incorporate the resolution into the rewritten realloc() DESCRIPTION and RETURN VALUE sections. Proposed changes (page and line numbers are for the 2018 edition) ... On page 637 line 22010 section calloc(), change: <blockquote>Upon successful completion with both <i>nelem</i> and <i>elsize</i> non-zero, <i>calloc</i>() shall return a pointer to the allocated space. If either <i>nelem</i> or <i>elsize</i> is 0, then either: <ul> <li>A null pointer shall be returned [CX]and <i>errno</i> may be set to an implementation-defined value[/CX], or</li> <li>A pointer to the allocated space shall be returned. The application shall ensure that the pointer is not used to access an object.</li> </ul> Otherwise, it shall return a null pointer [CX]and set <i>errno</i> to indicate the error[/CX].</blockquote> to: <blockquote>Upon successful completion, <i>calloc</i>() shall return a pointer to the allocated space; if either <i>nelem</i> or <i>elsize</i> is 0, the application shall ensure that the pointer is not used to access an object. Otherwise, it shall return a null pointer [CX]and set <i>errno</i> to indicate the error[/CX].</blockquote> On page 637 line 22019 section calloc(), add: <blockquote>The <i>calloc</i>() function may fail if: <blockquote>[EINVAL] <i>nelem</i> or <i>elsize</i> is 0 and the implementation does not support 0 sized allocations.</blockquote></blockquote> On page 1295 line 43152 section malloc(), change: <blockquote>Upon successful completion with <i>size</i> not equal to 0, <i>malloc</i>() shall return a pointer to the allocated space. If <i>size</i> is 0, then either: <ul> <li>A null pointer shall be returned [CX]and <i>errno</i> may be set to an implementation-defined value[/CX], or</li> <li>A pointer to the allocated space shall be returned. The application shall ensure that the pointer is not used to access an object.</li> </ul> Otherwise, it shall return a null pointer [CX]and set <i>errno</i> to indicate the error[/CX].</blockquote> to: <blockquote>Upon successful completion, <i>malloc</i>() shall return a pointer to the allocated space; if <i>size</i> is 0, the application shall ensure that the pointer is not used to access an object. Otherwise, it shall return a null pointer [CX]and set <i>errno</i> to indicate the error[/CX].</blockquote> On page 1295 line 43161 section malloc(), add: <blockquote>The <i>malloc</i>() function may fail if: <blockquote>[EINVAL] <i>size</i> is 0 and the implementation does not support 0 sized allocations.</blockquote></blockquote> On page 1788 line 57886 section realloc(), change: <blockquote>A null pointer shall be returned [CX]and, if <i>ptr</i> is not a null pointer, <i>errno</i> shall be set to an implementation-defined value[/CX].</blockquote> to: <blockquote>A null pointer shall be returned [CX]and, if <i>ptr</i> is not a null pointer, <i>errno</i> shall be set to [EINVAL][/CX].</blockquote> On page 1788 line 57895 section realloc(), add: <blockquote>The <i>realloc</i>() function may fail if: <blockquote>[EINVAL] <i>size</i> is 0 and the implementation does not support 0 sized allocations.</blockquote></blockquote> Issue History Date Modified Username Field Change ====================================================================== 2011-02-03 17:12 eblake New Issue 2011-02-03 17:12 eblake Status New => Under Review 2011-02-03 17:12 eblake Assigned To => ajosey 2011-02-03 17:12 eblake Name => Eric Blake 2011-02-03 17:12 eblake Organization => Red Hat 2011-02-03 17:12 eblake User Reference => ebb.malloc 2011-02-03 17:12 eblake Section => malloc 2011-02-03 17:12 eblake Page Number => 1268 2011-02-03 17:12 eblake Line Number => 41715 2011-02-03 17:12 eblake Interp Status => --- 2011-02-03 17:15 nick Tag Attached: c99 2011-02-03 17:23 eblake Desired Action Updated 2011-02-03 17:25 eblake Note Added: 0000664 2011-03-17 12:30 nick Note Added: 0000708 2011-03-17 12:31 nick Note Added: 0000709 2011-03-17 16:07 nick Note Deleted: 0000709 2011-03-17 16:09 nick Note Edited: 0000708 2011-03-24 16:31 eblake Tag Attached: issue8 2011-03-24 16:38 eblake Relationship added child of 0000400 2011-03-24 16:41 eblake Note Added: 0000722 2011-04-07 20:45 eblake Note Added: 0000737 2011-11-03 15:22 nick Note Added: 0001001 2012-02-16 16:13 nick Note Added: 0001130 2012-02-16 17:15 nick Relationship added related to 0000526 2013-05-09 15:57 eblake Relationship added child of 0000688 2013-05-09 16:23 eblake Relationship deleted child of 0000688 2013-05-09 16:23 eblake Relationship added related to 0000688 2014-04-10 15:57 geoffclare Note Added: 0002221 2014-04-10 15:59 eblake Note Added: 0002222 2019-08-02 11:18 geoffclare Note Added: 0004507 ======================================================================