Subject: bug#53136: 28.0.90; segfault in lock_file

From: Paul Eggert <[email-masked]>
Date: Mon, 10 Jan 2022 15:11:04 -0800

On 1/9/22 04:03, Eli Zaretskii wrote:

> Paul, could you please take a look?

The problem (as Po deduced) seems to be that Haiku errno values are 
negative by default.

However, I see some problems with the recently installed fix.

1. It uses BE_USE_POSITIVE_POSIX_ERRORS but Haiku spells it 
B_USE_POSITIVE_POSIX_ERRORS.

2. There's no need to replace "if (err < 0)" with "if (err == -1 || err 
== -2)", as this replacement is not needed given the "#if !defined HAIKU 
..." stuff.

3. More importantly, I'm sure there are other places where Emacs assumes 
that errno values are positive. I doubt whether it's reasonable to 
expect Emacs developers to remember and work around this Haiku 
incompatibility, every time they call a function that sets errno or 
returns an errno value. Instead, we should arrange for Haiku builds to 
use positive errno values, they way errno behaves on GNU and other 
POSIX-compatible hosts. This should be a much more maintainable solution.

Proposed patch attached.

From 026aef868ad242c3ccaf0d542ab81b621a26e0a9 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Mon, 10 Jan 2022 15:07:51 -0800
Subject: [PATCH] Improve port to Haiku errno values

* configure.ac (B_USE_POSITIVE_POSIX_ERRORS): Define on Haiku.
* src/filelock.c (lock_if_free): Revert recent changes, since
defining B_USE_POSITIVE_POSIX_ERRORS should fix the general problem.
---
 configure.ac   | 2 ++
 src/filelock.c | 9 +--------
 2 files changed, 3 insertions(+), 8 deletions(-)

diff --git a/configure.ac b/configure.ac
index baf8c8018e..c5d97890c7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -5672,6 +5672,8 @@ AC_DEFUN
 
   haiku)
     AC_DEFINE(HAIKU, [], [Define if the system is Haiku.])
+    AC_DEFINE([B_USE_POSITIVE_POSIX_ERRORS], [],
+      [Define to cause Haiku to use POSIX-compatible errno values.])
     ;;
 esac
 
diff --git a/src/filelock.c b/src/filelock.c
index c3927f58fa..a213c2b3ca 100644
--- a/src/filelock.c
+++ b/src/filelock.c
@@ -608,7 +608,7 @@ lock_if_free (lock_info_type *clasher, char *lfname)
       err = current_lock_owner (clasher, lfname);
       if (err != 0)
 	{
-	  if (err == -1 || err == -2)
+	  if (err < 0)
 	    return -2 - err; /* We locked it, or someone else has it.  */
 	  break; /* current_lock_owner returned strange error.  */
 	}
@@ -616,14 +616,7 @@ lock_if_free (lock_info_type *clasher, char *lfname)
       /* We deleted a stale lock; try again to lock the file.  */
     }
 
-#if !defined HAIKU \
-  || defined BE_USE_POSITIVE_POSIX_ERRORS
   return err;
-#else
-  /* On Haiku, POSIX errno values are negative by default, but this
-     code's callers assume that all errno values are positive.  */
-  return -err;
-#endif
 }
 
 static Lisp_Object
-- 
2.32.0