Subject: bug#75451: scratch/igc: Enable CHECK_STRUCTS

From: Stefan Kangas <[email-masked]>
Date: Sun, 19 Jan 2025 20:01:31 -0600

Paul Eggert <[email-masked]> writes:

> Yes, inserting 'default: emacs_abort ();' switches from static to
> dynamic checking, which is less reliable. Although 'default: emacs_abort
> ();' is sometimes the best one can do, it's better to avoid it when
> possible.
>
> I use 'default: eassume (false);' more often than 'default: emacs_abort
> ();', though for a completely different purpose: to tell GCC something
> that it's not smart enough to figure out on its own, so that GCC can do
> better static checking elsewhere.

Thanks, that's helpful.  Based on the above, I came up with the attached
patch for pdumper.c.

But could you explain your last paragraph above?  In my attached patch,
there is one removal of 'default: eassume(false)'; how can I know if
it's worth keeping or not?
From 737a5ad20adeee0a919adecf57f2ba52d81f0d78 Mon Sep 17 00:00:00 2001
From: Stefan Kangas <[email-masked]>
Date: Mon, 20 Jan 2025 02:47:30 +0100
Subject: [PATCH] Prefer static switch-case checks in pdumper

* src/pdumper.c (dump_fwd, dump_symbol, dump_drain_cold_data)
(decode_emacs_reloc, dump_do_fixup, dump_anonymous_allocate_w32)
(dump_anonymous_allocate_posix, dump_map_file_w32, dump_map_file_posix)
(dump_do_emacs_relocation): Remove default clauses to switch from
dynamic to static checking.
---
 src/pdumper.c | 23 +----------------------
 1 file changed, 1 insertion(+), 22 deletions(-)

diff --git a/src/pdumper.c b/src/pdumper.c
index e83c7bcf9a1..86576af9d8a 100644
--- a/src/pdumper.c
+++ b/src/pdumper.c
@@ -2390,8 +2390,6 @@ dump_fwd (struct dump_context *ctx, lispfwd fwd)
     case Lisp_Fwd_Kboard_Obj:
       offset = dump_fwd_kboard_obj (ctx, p);
       break;
-    default:
-      emacs_abort ();
     }
 
   return offset;
@@ -2523,8 +2521,6 @@ dump_symbol (struct dump_context *ctx,
     case SYMBOL_FORWARDED:
       dump_field_fixup_later (ctx, &out, symbol, &symbol->u.s.val.fwd);
       break;
-    default:
-      emacs_abort ();
     }
   dump_field_lv (ctx, &out, symbol, &symbol->u.s.function, WEIGHT_NORMAL);
   dump_field_lv (ctx, &out, symbol, &symbol->u.s.plist, WEIGHT_NORMAL);
@@ -3601,8 +3597,6 @@ dump_drain_cold_data (struct dump_context *ctx)
 	  dump_cold_native_subr (ctx, data);
 	  break;
 #endif
-        default:
-          emacs_abort ();
         }
     }
 
@@ -3815,7 +3809,7 @@ decode_emacs_reloc (struct dump_context *ctx, Lisp_Object lreloc)
   int type = XFIXNUM (dump_pop (&lreloc));
   reloc.emacs_offset = dump_off_from_lisp (dump_pop (&lreloc));
   dump_check_emacs_off (reloc.emacs_offset);
-  switch (type)
+  switch ((enum emacs_reloc_type) type)
     {
     case RELOC_EMACS_COPY_FROM_DUMP:
       {
@@ -3882,8 +3876,6 @@ decode_emacs_reloc (struct dump_context *ctx, Lisp_Object lreloc)
           }
       }
       break;
-    default:
-      eassume (!"not reached");
     }
 
   /* We should have consumed the whole relocation descriptor.  */
@@ -4067,8 +4059,6 @@ dump_do_fixup (struct dump_context *ctx,
         do_write = false;
         break;
       }
-    default:
-      emacs_abort ();
     }
   if (do_write)
     dump_write (ctx, &dump_value, sizeof (dump_value));
@@ -4527,8 +4517,6 @@ dump_anonymous_allocate_w32 (void *base,
       mem_type = MEM_COMMIT;
       mem_prot = PAGE_READWRITE;
       break;
-    default:
-      emacs_abort ();
     }
 
   ret = VirtualAlloc (base, size, mem_type, mem_prot);
@@ -4567,8 +4555,6 @@ dump_anonymous_allocate_posix (void *base,
     case DUMP_MEMORY_ACCESS_READWRITE:
       mem_prot = PROT_READ | PROT_WRITE;
       break;
-    default:
-      emacs_abort ();
     }
 
   int mem_flags = MAP_PRIVATE | MAP_ANONYMOUS;
@@ -4661,7 +4647,6 @@ dump_map_file_w32 (void *base, int fd, off_t offset, size_t size,
     case DUMP_MEMORY_ACCESS_READWRITE:
       protect = PAGE_WRITECOPY;	/* for Windows 9X */
       break;
-    default:
     case DUMP_MEMORY_ACCESS_NONE:
     case DUMP_MEMORY_ACCESS_READ:
       protect = PAGE_READONLY;
@@ -4689,8 +4674,6 @@ dump_map_file_w32 (void *base, int fd, off_t offset, size_t size,
     case DUMP_MEMORY_ACCESS_READWRITE:
       map_access = FILE_MAP_COPY;
       break;
-    default:
-      emacs_abort ();
     }
 
   ret = MapViewOfFileEx (section,
@@ -4733,8 +4716,6 @@ dump_map_file_posix (void *base, int fd, off_t offset, size_t size,
       mem_prot = PROT_READ | PROT_WRITE;
       mem_flags = MAP_PRIVATE;
       break;
-    default:
-      emacs_abort ();
     }
 
   if (base)
@@ -5601,8 +5582,6 @@ dump_do_emacs_relocation (const uintptr_t dump_base,
         memcpy (emacs_ptr_at (reloc.emacs_offset), &lv, sizeof (lv));
         break;
       }
-    default:
-      fatal ("unrecognied relocation type %d", (int) reloc.type);
     }
 }
 
-- 
2.48.0