Fix double reporting when the last command of a function fails

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh
index 8b8e3dd..cb3c8f2 100755
--- a/tests/scripts/all.sh
+++ b/tests/scripts/all.sh
@@ -480,6 +480,11 @@
     failure_count=0 # Number of failed components
     last_failure_status=0 # Last failure status in this component
 
+    # See err_trap
+    previous_failure_status=0
+    previous_failed_command=
+    previous_failure_funcall_depth=0
+
     start_red=
     end_color=
     if [ -t 1 ]; then
@@ -517,6 +522,21 @@
         last_failure_status=$?
         failed_command=$BASH_COMMAND
 
+        if [[ $last_failure_status -eq $previous_failure_status &&
+              "$failed_command" == "$previous_failed_command" &&
+              ${#FUNCNAME[@]} == $((previous_failure_funcall_depth - 1)) ]]
+        then
+            # The same command failed twice in a row, but this time one level
+            # less deep in the function call stack. This happens when the last
+            # command of a function returns a nonzero status, and the function
+            # returns that same status. Ignore the second failure.
+            previous_failure_funcall_depth=${#FUNCNAME[@]}
+            return
+        fi
+        previous_failure_status=$last_failure_status
+        previous_failed_command=$failed_command
+        previous_failure_funcall_depth=${#FUNCNAME[@]}
+
         text="$current_section: $failed_command -> $last_failure_status"
         echo "${start_red}^^^^$text^^^^${end_color}" >&2
         echo "$text" >>"$failure_summary_file"