From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by sourceware.org (Postfix) with ESMTPS id BB028385840A for ; Thu, 11 Nov 2021 13:04:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BB028385840A Received: by mail-wm1-x32b.google.com with SMTP id i8-20020a7bc948000000b0030db7b70b6bso7203301wml.1 for ; Thu, 11 Nov 2021 05:04:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=6tH3NiZfhNZu/V5Lj83c2m7rdUwuBR/Zf8gaEvg1vqg=; b=qpC1BRetmfR9IUosSDTSvu6s0Ukm4r3ZAQTzzYa783DDLFSJkI/SSl0pekkoP0s1GC rTZw4M8QgaEQYc60S8egX5RsQvy/XpKQ+5p8YdGJqo03iNIbQ+M99qcioROvwyFgq7h7 PdhwpZpSPL4DX/NbTb3SH2jeC4qVZRONIWxgUk43P35wWhC9nowaoQj66PDgYPkB0DtZ Bkc0feSiTMd+jV1q92nXbYP6DwPDqutD8y2+AjUpeGbuGWc+AOQVW4uF8mf9RTuvsgD4 M3kCEIdf9MJsz3q7QS1aUHu1MuvI2Oev9eTNa0tb1ASmfJjjWWkOSyP48p7fCxRrGoNH DV9w== X-Gm-Message-State: AOAM530SVY6hP8DL65xck+2MdYBQ5c5Zj2hGtIciyGKM0aTM9/CoB/lQ dnSKYqhrcDr9sQY5gEWv8P7aa67fyuMkJNOM7gBqYw== X-Google-Smtp-Source: ABdhPJypQsxqyi62WTljpK/0F9sbxuUoqf+TULpVIz5rmMGmpKC2N64SJyfBUwoEtb76fTO0CiHyV8flupojB2fK3ok= X-Received: by 2002:a1c:7405:: with SMTP id p5mr24424560wmc.152.1636635879511; Thu, 11 Nov 2021 05:04:39 -0800 (PST) MIME-Version: 1.0 References: <109aefbeac593ab5660a71df38f1727002c19e39.camel@mengyan1223.wang> In-Reply-To: <109aefbeac593ab5660a71df38f1727002c19e39.camel@mengyan1223.wang> From: Eric Gallager Date: Thu, 11 Nov 2021 08:04:28 -0500 Message-ID: Subject: Re: [PATCH] fixincludes: don't assume getcwd() can handle NULL argument To: Xi Ruoyao Cc: gcc-patches , Bruce Korb Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Nov 2021 13:04:42 -0000 On Tue, Nov 9, 2021 at 8:50 AM Xi Ruoyao via Gcc-patches wrote: > > POSIX says: > > On some implementations, if buf is a null pointer, getcwd() may obtai= n > size bytes of memory using malloc(). In this case, the pointer return= ed > by getcwd() may be used as the argument in a subsequent call to free(= ). > Invoking getcwd() with buf as a null pointer is not recommended in > conforming applications. > > This produces an error building GCC with --enable-werror-always: > > ../../../fixincludes/fixincl.c: In function =E2=80=98process=E2=80=99= : > ../../../fixincludes/fixincl.c:1356:7: error: argument 1 is null but > the corresponding size argument 2 value is 4096 [-Werror=3Dnonnull] > > And, at least we've been leaking memory even if getcwd() supports this > non-standard extension. > > fixincludes/ChangeLog: > > * fixincl.c (process): Allocate and deallocate the buffer for > getcwd() explicitly. > --- > fixincludes/fixincl.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/fixincludes/fixincl.c b/fixincludes/fixincl.c > index 6dba2f6e830..b4b1e38ede7 100644 > --- a/fixincludes/fixincl.c > +++ b/fixincludes/fixincl.c > @@ -1353,9 +1353,11 @@ process (void) > if (access (pz_curr_file, R_OK) !=3D 0) > { > int erno =3D errno; > + char *buf =3D xmalloc (MAXPATHLEN); > fprintf (stderr, "Cannot access %s from %s\n\terror %d (%s)\n", > - pz_curr_file, getcwd ((char *) NULL, MAXPATHLEN), > + pz_curr_file, getcwd (buf, MAXPATHLEN), > erno, xstrerror (erno)); > + free (buf); > return; > } > > -- > 2.33.1 This seems to contradict bug 21823: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D21823 It would fix bug 80047, though: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D80047