Subject: bug#47307: Emacs 27.2 RC2 build/check issues on macOS ARM64

From: Paul Eggert <[email-masked]>
Date: Sun, 21 Mar 2021 18:27:32 -0700

I configured Emacs 27.2 RC2 on macOS 11.2 ARM64 (arm-apple-darwin20.3.0) 
using "./configure --with-gnutls=ifavailable" (plain "./configure" 
failed, which was disconcerting). When I ran "make" the compiler complained:

editfns.c:2063:18: warning: unsequenced modification and access to 
'sa_avail' [-Wunsequenced]

This correctly diagnoses undefined behavior in that C function. I didn't 
investigate what happens on that platform is as it's easier to fix the 
problem on all platforms. I propose installing the attached 
obviously-safe patch into the emacs-27 branch.

There were several deprecation warnings when compiling but I assume we 
don't care about them in this older branch.

"make check" reported four unexpected failures. Perhaps a macOS expert 
could be persuaded to look into them. A tarball of the failed logs is 
attached.
From f85db942100f4a129d4b9efba5707b954cbf2121 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sun, 21 Mar 2021 18:08:13 -0700
Subject: [PATCH] Fix replace-buffer-contents undefined behavior

* src/editfns.c (Freplace_buffer_contents): Avoid undefined
behavior with competing side effects in parallel subexpressions.
Problem reported by Apple clang version 12.0.0 (clang-1200.0.32.29).
---
 src/editfns.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/editfns.c b/src/editfns.c
index 621e35171d..cd9633d4c6 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -2053,6 +2053,8 @@ DEFUN ("replace-buffer-contents", Freplace_buffer_contents,
      code.  */
   ptrdiff_t del_bytes = (size_t) size_a / CHAR_BIT + 1;
   ptrdiff_t ins_bytes = (size_t) size_b / CHAR_BIT + 1;
+  unsigned char *deletions = SAFE_ALLOCA (del_bytes);
+  unsigned char *insertions = SAFE_ALLOCA (ins_bytes);
   struct context ctx = {
     .buffer_a = a,
     .buffer_b = b,
@@ -2060,8 +2062,8 @@ DEFUN ("replace-buffer-contents", Freplace_buffer_contents,
     .beg_b = min_b,
     .a_unibyte = BUF_ZV (a) == BUF_ZV_BYTE (a),
     .b_unibyte = BUF_ZV (b) == BUF_ZV_BYTE (b),
-    .deletions = SAFE_ALLOCA (del_bytes),
-    .insertions = SAFE_ALLOCA (ins_bytes),
+    .deletions = deletions,
+    .insertions = insertions,
     .fdiag = buffer + size_b + 1,
     .bdiag = buffer + diags + size_b + 1,
     .heuristic = true,
-- 
2.27.0