commit:     2aea3a5ecfd9c48f7566048f9fc861186be1dc6a
Author:     Sam James <sam <AT> gentoo <DOT> org>
AuthorDate: Sat May 10 01:31:28 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sat May 10 01:31:28 2025 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=2aea3a5e

dev-util/unifdef: fix tests w/ _FORTIFY_SOURCE

The unifdef testsuite has been failing sometimes for years with
_FORTIFY_SOURCE in a non-obvious way, where glibc's fgets will return
\0 w/ n=1.

I'd found marxin's PR for this years ago and apparently completely forgot
about it (nor did I backport the patch into Gentoo at the time, as
I didn't maintain it, and I was fairly new then).

Backport marxin's patch from 2022 accordingly. Thanks to Ben Beasley
poking me on the upstream PR (19) which made me look at all of this
again.

Bug: https://gcc.gnu.org/PR120205
Bug: https://github.com/fanf2/unifdef/pull/15
Bug: https://github.com/fanf2/unifdef/pull/19
Closes: https://bugs.gentoo.org/836698
Signed-off-by: Sam James <sam <AT> gentoo.org>

 dev-util/unifdef/files/unifdef-2.12-fortify.patch | 96 +++++++++++++++++++++++
 dev-util/unifdef/unifdef-2.12-r2.ebuild           | 28 +++++++
 2 files changed, 124 insertions(+)

diff --git a/dev-util/unifdef/files/unifdef-2.12-fortify.patch 
b/dev-util/unifdef/files/unifdef-2.12-fortify.patch
new file mode 100644
index 000000000000..1974aaae7e39
--- /dev/null
+++ b/dev-util/unifdef/files/unifdef-2.12-fortify.patch
@@ -0,0 +1,96 @@
+https://bugs.gentoo.org/836698
+https://gcc.gnu.org/PR120205
+https://github.com/fanf2/unifdef/pull/15
+
+From 9791614057fe7fc72babd78708fdd07857fef9cf Mon Sep 17 00:00:00 2001
+From: Martin Liska <[email protected]>
+Date: Wed, 10 Aug 2022 10:06:55 +0200
+Subject: [PATCH] Fix fgets(..., size=1)
+
+I noticed the following 2 tests are failing with -O1
+-D_FORTIFY_SOURCE=1:
+
+[   44s] FAILED: whitespace-1.out: unifdef -DFOO whitespace.c
+[   44s] FAILED: whitespace-2.out: unifdef -DBAR whitespace.c
+
+It's caused by fact that:
+
+fgets returns '\0' if n == 1:
+
+char *
+_IO_fgets (char *buf, int n, FILE *fp)
+{
+  size_t count;
+  char *result;
+  int old_error;
+  CHECK_FILE (fp, NULL);
+  if (n <= 0)
+    return NULL;
+  if (__glibc_unlikely (n == 1))
+    {
+      /* Another irregular case: since we have to store a NUL byte and
+        there is only room for exactly one byte, we don't have to
+        read anything.  */
+      buf[0] = '\0';
+      return buf;
+    }
+---
+ tests/whitespace-1.experr | 2 --
+ tests/whitespace-1.expout | 5 +++++
+ tests/whitespace-1.exprc  | 2 +-
+ tests/whitespace-2.expout | 3 ++-
+ unifdef.c                 | 3 ++-
+ 5 files changed, 10 insertions(+), 5 deletions(-)
+
+diff --git a/tests/whitespace-1.experr b/tests/whitespace-1.experr
+index cb23fce..e69de29 100644
+--- a/tests/whitespace-1.experr
++++ b/tests/whitespace-1.experr
+@@ -1,2 +0,0 @@
+-unifdef: whitespace.c: 4: Obfuscated preprocessor control line (#if line 1 
depth 1)
+-unifdef: Output may be truncated
+diff --git a/tests/whitespace-1.expout b/tests/whitespace-1.expout
+index 257cc56..14dd150 100644
+--- a/tests/whitespace-1.expout
++++ b/tests/whitespace-1.expout
+@@ -1 +1,6 @@
+ foo
++                                                                              
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                 //spong
++
++#ifdef BAR
++bar
++#endif
+diff --git a/tests/whitespace-1.exprc b/tests/whitespace-1.exprc
+index 0cfbf08..d00491f 100644
+--- a/tests/whitespace-1.exprc
++++ b/tests/whitespace-1.exprc
+@@ -1 +1 @@
+-2
++1
+diff --git a/tests/whitespace-2.expout b/tests/whitespace-2.expout
+index 84cabfe..43f6399 100644
+--- a/tests/whitespace-2.expout
++++ b/tests/whitespace-2.expout
+@@ -1,5 +1,6 @@
+ #ifdef FOO
+ foo
+-#endif                                                                        
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                      
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                      
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                      
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                      
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                      
                       //spong
++#endif                                                                        
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                      
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                      
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                      
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                      
                                                                                
                             
++                                                                              
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                 //spong
+ 
+ bar
+diff --git a/unifdef.c b/unifdef.c
+index dc145a2..b7335aa 100644
+--- a/unifdef.c
++++ b/unifdef.c
+@@ -846,7 +846,8 @@ parseline(void)
+          newline or if there is too much whitespace in a directive */
+       if (linestate == LS_HASH) {
+               long len = cp - tline;
+-              if (fgets(tline + len, MAXLINE - len, input) == NULL) {
++              const char *line = fgets(tline + len, MAXLINE - len, input);
++              if (line == NULL || *line == '\0') {
+                       if (ferror(input))
+                               err(2, "can't read %s", filename);
+                       debug("parser insert newline at EOF", linenum);
+

diff --git a/dev-util/unifdef/unifdef-2.12-r2.ebuild 
b/dev-util/unifdef/unifdef-2.12-r2.ebuild
new file mode 100644
index 000000000000..7a54bf93f918
--- /dev/null
+++ b/dev-util/unifdef/unifdef-2.12-r2.ebuild
@@ -0,0 +1,28 @@
+# Copyright 1999-2025 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=8
+
+inherit toolchain-funcs
+
+DESCRIPTION="remove #ifdef'ed lines from a file while otherwise leaving the 
file alone"
+HOMEPAGE="https://dotat.at/prog/unifdef/";
+SRC_URI="https://dotat.at/prog/unifdef/${P}.tar.xz";
+
+LICENSE="BSD"
+SLOT="0"
+KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~loong ~m68k ~mips ~ppc ~ppc64 
~riscv ~s390 ~sparc ~x86 ~amd64-linux ~x86-linux"
+
+PATCHES=(
+       "${FILESDIR}"/${P}-tests.patch
+       "${FILESDIR}"/${P}-c23.patch
+       "${FILESDIR}"/${P}-fortify.patch
+)
+
+DOCS=( README )
+
+src_prepare() {
+       sed -i "/^prefix/s:=.*:=${EPREFIX}/usr:" Makefile || die
+       tc-export CC
+       default
+}

Reply via email to