Bug 27476 - getcwd(NULL, PATH_MAX) generates warnings on -Wnonnull
Summary: getcwd(NULL, PATH_MAX) generates warnings on -Wnonnull
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: 2.33
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2021-02-26 18:01 UTC by Sergei Trofimovich
Modified: 2021-09-10 20:45 UTC (History)
7 users (show)

See Also:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Sergei Trofimovich 2021-02-26 18:01:41 UTC
Extracted from https://github.com/libarchive/libarchive/issues/1498. Small example:

$ cat a.c
#include <limits.h> /* PATH_MAX */
#include <unistd.h> /* getcwd() */

// from https://github.com/libarchive/libarchive/issues/1498
char * bug(void) {
    return getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */

$ gcc-10.2.0 -O2 -c a.c -O2 -Wall -Werror=nonnull -D_FORTIFY_SOURCE=0
<command-line>: warning: "_FORTIFY_SOURCE" redefined
<built-in>: note: this is the location of the previous definition
a.c: In function 'bug':
a.c:6:12: error: argument 1 is null but the corresponding size argument 2 value is 4096 [-Werror=nonnull]
    6 |     return getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
      |            ^~~~~~~~~~~~~~~~~~~~~~
In file included from a.c:2:
/usr/include/unistd.h:520:14: note: in a call to function 'getcwd' declared with attribute 'write_only (1, 2)'
  520 | extern char *getcwd (char *__buf, size_t __size) __THROW __wur
      |              ^~~~~~
cc1: some warnings being treated as errors

I see https://sourceware.org/PR26545 where there is a discussion of possibly changing the behaviour of the function.

I'm opening the bug to consider at least removing the warning whic the behaviour is valid

Comment 1 Aurelien Jarno 2021-09-08 22:54:15 UTC
Proposed patch here: https://sourceware.org/pipermail/libc-alpha/2021-September/130857.html