[Bug debuginfod/31562] New: profile.sh might fail with set -o pipefail

2024-03-26 Thread mark at klomp dot org
https://sourceware.org/bugzilla/show_bug.cgi?id=31562

Bug ID: 31562
   Summary: profile.sh might fail with set -o pipefail
   Product: elfutils
   Version: unspecified
Status: NEW
  Severity: normal
  Priority: P2
 Component: debuginfod
  Assignee: unassigned at sourceware dot org
  Reporter: mark at klomp dot org
CC: elfutils-devel at sourceware dot org
  Target Milestone: ---

With set -o pipefail profile.sh might fail on:

DEBUGINFOD_URLS=$(cat /dev/null "${prefix}/etc/debuginfod"/*.urls 2>/dev/null |
tr '\n' ' ')

This is because if there isn't an *.urls file the first command in the pipe
fails (the 2>/dev/null is there to hide that failure).

This can be fixed by adding something like || echo -n "" like:

DEBUGINFOD_URLS=$(cat /dev/null "${prefix}/etc/debuginfod"/*.urls 2>/dev/null |
tr '\n' ' ' || echo -n "")

This works because echo -n "" produces the empty string, so the next line will
still unset DEBUGINFOD_URLS in that case:

[ -n "$DEBUGINFOD_URLS" ] && export DEBUGINFOD_URLS || unset DEBUGINFOD_URLS

-- 
You are receiving this mail because:
You are on the CC list for the bug.

[Bug debuginfod/31562] profile.sh might fail with set -o pipefail

2024-03-26 Thread amerey at redhat dot com
https://sourceware.org/bugzilla/show_bug.cgi?id=31562

Aaron Merey  changed:

   What|Removed |Added

 CC||amerey at redhat dot com
   Assignee|unassigned at sourceware dot org   |amerey at redhat dot com

-- 
You are receiving this mail because:
You are on the CC list for the bug.

[PATCH] config: Make sure profile.sh succeeds with set -e and set -o pipefail

2024-03-26 Thread Mark Wielaard
profile.sh might fail with set -o pipefail because:

cat /dev/null "${prefix}/etc/debuginfod"/*.urls 2>/dev/null | tr '\n' ' '

might fail when there isn't an *.urls file the first command in the
pipe fails (the 2>/dev/null is there to hide that failure).

This can be fixed by adding || echo -n "" at the end.

This works because echo -n "" produces the empty string which is what
the script expects when the command would fail.

Also add a new testcase that runs profile.sh with bout  set -e
and set -o pipefail.

* config/profile.sh.in: Add || echo -n "" at end of pipe.
* tests/run-debuginfod-client-profile.sh: New test.
* tests/Makefile.am (TESTS): Add run-debuginfod-client-profile.sh.
(EXTRA_DIST): Likewise.

https://sourceware.org/bugzilla/show_bug.cgi?id=31562

Signed-off-by: Mark Wielaard 
---
 config/profile.sh.in   |  2 +-
 tests/Makefile.am  |  2 ++
 tests/run-debuginfod-client-profile.sh | 27 ++
 3 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100755 tests/run-debuginfod-client-profile.sh

diff --git a/config/profile.sh.in b/config/profile.sh.in
index 3f4397dcb44d..4488c66511a4 100644
--- a/config/profile.sh.in
+++ b/config/profile.sh.in
@@ -6,7 +6,7 @@
 
 if [ -z "$DEBUGINFOD_URLS" ]; then
 prefix="@prefix@"
-DEBUGINFOD_URLS=$(cat /dev/null "@sysconfdir@/debuginfod"/*.urls 
2>/dev/null | tr '\n' ' ')
+DEBUGINFOD_URLS=$(cat /dev/null "@sysconfdir@/debuginfod"/*.urls 
2>/dev/null | tr '\n' ' ' || echo -n "")
 [ -n "$DEBUGINFOD_URLS" ] && export DEBUGINFOD_URLS || unset 
DEBUGINFOD_URLS
 unset prefix
 fi
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9315ec3bbe4c..344d6706e16e 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -209,6 +209,7 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile 
test-nlist \
run-disasm-riscv64.sh \
run-pt_gnu_prop-tests.sh \
run-getphdrnum.sh run-test-includes.sh \
+   run-debuginfod-client-profile.sh \
leb128 read_unaligned \
msg_tst system-elf-libelf-test system-elf-gelf-test \
$(asm_TESTS) run-disasm-bpf.sh run-low_high_pc-dw-form-indirect.sh \
@@ -636,6 +637,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
 testfile_pt_gnu_prop.bz2 testfile_pt_gnu_prop32.bz2 \
 run-getphdrnum.sh testfile-phdrs.elf.bz2 \
 run-test-includes.sh run-low_high_pc-dw-form-indirect.sh \
+run-debuginfod-client-profile.sh \
 run-readelf-dw-form-indirect.sh testfile-dw-form-indirect.bz2 \
 run-nvidia-extended-linemap-libdw.sh 
run-nvidia-extended-linemap-readelf.sh \
 testfile_nvidia_linemap.bz2 \
diff --git a/tests/run-debuginfod-client-profile.sh 
b/tests/run-debuginfod-client-profile.sh
new file mode 100755
index ..7435ced83f15
--- /dev/null
+++ b/tests/run-debuginfod-client-profile.sh
@@ -0,0 +1,27 @@
+#! /bin/sh
+# Copyright (C) 2024 Mark J. Wielaard
+# This file is part of elfutils.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# elfutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see .
+
+. $srcdir/test-subr.sh
+
+# Make sure the profile.sh or profile.d/debuginfod.sh works even with
+# set -e (any command error is an error) and set -o pipefail (any error
+# in a pipe fails the whole pipe command).
+
+set -e
+set -o pipefail
+
+source ${abs_top_builddir}/config/profile.sh
-- 
2.39.3



[Bug debuginfod/31562] profile.sh might fail with set -o pipefail

2024-03-26 Thread mark at klomp dot org
https://sourceware.org/bugzilla/show_bug.cgi?id=31562

--- Comment #1 from Mark Wielaard  ---
Proposed patch:
https://inbox.sourceware.org/elfutils-devel/20240326204948.794765-1-m...@klomp.org/

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Re: [PATCH] config: Make sure profile.sh succeeds with set -e and set -o pipefail

2024-03-26 Thread Dmitry V. Levin
On Tue, Mar 26, 2024 at 09:49:48PM +0100, Mark Wielaard wrote:
> profile.sh might fail with set -o pipefail because:
> 
> cat /dev/null "${prefix}/etc/debuginfod"/*.urls 2>/dev/null | tr '\n' ' '
> 
> might fail when there isn't an *.urls file the first command in the
> pipe fails (the 2>/dev/null is there to hide that failure).
[...]
> -DEBUGINFOD_URLS=$(cat /dev/null "@sysconfdir@/debuginfod"/*.urls 
> 2>/dev/null | tr '\n' ' ')
> +DEBUGINFOD_URLS=$(cat /dev/null "@sysconfdir@/debuginfod"/*.urls 
> 2>/dev/null | tr '\n' ' ' || echo -n "")

The idiomatic expression in this case is ||:


-- 
ldv


[PATCH] config/profile.fish.in: Prevent bracketed variables and unmatched wildcard errors

2024-03-26 Thread Aaron Merey
Fish does not support bracketed variables in scripts.  Remove brackets
from the variable ${prefix} in profile.fish before installation to
prevent this error.

Fish also raises an error for unmatched wildcards, except for special
cases like the set command.  Use a wildcard to match .urls files using
the set command instead of cat to prevent an unmatched wildcard error
when no .urls files are found.

Signed-off-by: Aaron Merey 
---
 config/Makefile.am | 1 +
 config/profile.fish.in | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/config/Makefile.am b/config/Makefile.am
index ae14e625..fd41997f 100644
--- a/config/Makefile.am
+++ b/config/Makefile.am
@@ -41,6 +41,7 @@ pkgconfig_DATA += libdebuginfod.pc
 install-data-local:
$(INSTALL_DATA) profile.sh -D 
$(DESTDIR)$(sysconfdir)/profile.d/debuginfod.sh
$(INSTALL_DATA) profile.csh -D 
$(DESTDIR)$(sysconfdir)/profile.d/debuginfod.csh
+   sed -i 's/{prefix}/prefix/g' profile.fish
$(INSTALL_DATA) profile.fish -D 
$(DESTDIR)$(datadir)/fish/vendor_conf.d/debuginfod.fish
mkdir -p $(DESTDIR)$(sysconfdir)/debuginfod
if [ -n "@DEBUGINFOD_URLS@" ]; then \
diff --git a/config/profile.fish.in b/config/profile.fish.in
index 00e9ca59..c0a234db 100644
--- a/config/profile.fish.in
+++ b/config/profile.fish.in
@@ -7,7 +7,8 @@
 if not set --query DEBUGINFOD_URLS
 # Use local variables so we don't need to manually unset them
 set --local prefix "@prefix@"
-set --local DEBUGINFOD_URLS (cat /dev/null 
"@sysconfdir@/debuginfod"/*.urls 2>/dev/null | string replace '\n' ' ')
+set --local files "@sysconfdir@/debuginfod/"*.urls
+set --local DEBUGINFOD_URLS (cat /dev/null $files 2>/dev/null | string 
replace '\n' ' ')
 if test -n "$DEBUGINFOD_URLS"
 set --global --export DEBUGINFOD_URLS "$DEBUGINFOD_URLS"
 end
-- 
2.43.0