libgo: make match.sh POSIX-shell compatible

Message ID 20220720063541.5546-1-soeren@soeren-tempel.net
State New
Headers
Series libgo: make match.sh POSIX-shell compatible |

Commit Message

Li, Pan2 via Gcc-patches July 20, 2022, 6:35 a.m. UTC
  From: Sören Tempel <soeren@soeren-tempel.net>

The `(( expression ))` syntax is a Bash extension and not supported by
POSIX shell [1]. However, the arithmetic expressions used by the
gobuild() function can also be expressed using arithmetic POSIX
expansions with `$(( expression ))` [2].

Contrary to the Bash extension, arithmetic expansion doesn't set
the return value if the expression is non-zero but instead just prints
the expression result. Hence, the expression also needs to be negated.
Without this patch, match.sh does currently not work correctly if
/bin/sh is not a symlink to Bash.

[1]: https://www.gnu.org/software/bash/manual/bash.html#Conditional-Constructs
[2]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04

Signed-off-by: Sören Tempel <soeren@soeren-tempel.net>
---
 libgo/match.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Ian Lance Taylor July 22, 2022, 6:57 p.m. UTC | #1
On Tue, Jul 19, 2022 at 11:35 PM <soeren@soeren-tempel.net> wrote:
>
> From: Sören Tempel <soeren@soeren-tempel.net>
>
> The `(( expression ))` syntax is a Bash extension and not supported by
> POSIX shell [1]. However, the arithmetic expressions used by the
> gobuild() function can also be expressed using arithmetic POSIX
> expansions with `$(( expression ))` [2].
>
> Contrary to the Bash extension, arithmetic expansion doesn't set
> the return value if the expression is non-zero but instead just prints
> the expression result. Hence, the expression also needs to be negated.
> Without this patch, match.sh does currently not work correctly if
> /bin/sh is not a symlink to Bash.
>
> [1]: https://www.gnu.org/software/bash/manual/bash.html#Conditional-Constructs
> [2]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04

Thanks.  Committed as follows, which also fixes the similar function
in the gotest script.

Ian
dbf607d2e92cbae9a7f7620b69b9272adbce6381
diff --git a/gcc/go/gofrontend/MERGE b/gcc/go/gofrontend/MERGE
index 5ea0406cd8e..2f2fafde1f1 100644
--- a/gcc/go/gofrontend/MERGE
+++ b/gcc/go/gofrontend/MERGE
@@ -1,4 +1,4 @@
-ff68b1a147eb60082fd60c198db0ef5477ade938
+a62f20ae78ddd41be682dde8cab075ca4f5dbb2a
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
diff --git a/libgo/match.sh b/libgo/match.sh
index 7ed587ff794..e5ed98de422 100755
--- a/libgo/match.sh
+++ b/libgo/match.sh
@@ -111,7 +111,7 @@ gobuild() {
     if test "$goarch" != "386"; then
 	line=$(echo "$line" | sed -e "s/\\(${wrap}\\)386\\(${wrap}\\)/\10\2/g")
     fi
-    (($line))
+    return $((!($line)))
 }
 
 matched=
diff --git a/libgo/testsuite/gotest b/libgo/testsuite/gotest
index 04e4267fbba..0a0a7e14d74 100755
--- a/libgo/testsuite/gotest
+++ b/libgo/testsuite/gotest
@@ -302,7 +302,7 @@ gobuild() {
     if test "$goarch" != "386"; then
 	line=$(echo "$line" | sed -e "s/\\(${wrap}\\)386\\(${wrap}\\)/\10\2/g")
     fi
-    (($line))
+    return $((!($line)))
 }
 
 case "x$gofiles" in
  

Patch

diff --git a/libgo/match.sh b/libgo/match.sh
index 7ed587ff794..9fbb498544c 100755
--- a/libgo/match.sh
+++ b/libgo/match.sh
@@ -111,7 +111,7 @@  gobuild() {
     if test "$goarch" != "386"; then
 	line=$(echo "$line" | sed -e "s/\\(${wrap}\\)386\\(${wrap}\\)/\10\2/g")
     fi
-    (($line))
+    return $((!(line)))
 }
 
 matched=