commit 6fc6e15790df2e2a4ae0e8cd1a53f8d9266afa3b
Author: Dmitry Goncharov <dgoncharov@users.sf.net>
Date:   Mon Apr 24 21:57:35 2023 -0400

    [SV 64107] Disable builtins immediately on -R or -r.
    
    Disable builtin variables and rules immediately, when -R or -r is added to
    MAKEFLAGS.
    
    * src/main.c (disable_builtins): Add new function disable_builtins.
    (main): Call disable_builtins.
    (reset_makeflags): Call disable_builtins.
    * tests/scripts/options/dash-r: Add tests.
    * tests/scripts/variables/MAKEFLAGS: Update tests.

diff --git a/src/main.c b/src/main.c
index 6a94cfdb..8215ed78 100644
--- a/src/main.c
+++ b/src/main.c
@@ -101,6 +101,7 @@ static void decode_switches (int argc, const char **argv,
                              enum variable_origin origin);
 static void decode_env_switches (const char *envar, size_t len,
                                  enum variable_origin origin);
+static void disable_builtins ();
 static char *quote_for_env (char *out, const char *in);
 static void initialize_global_hash_tables (void);
 
@@ -180,6 +181,8 @@ int question_flag = 0;
 
 int no_builtin_rules_flag = 0;
 int no_builtin_variables_flag = 0;
+static int old_builtin_rules_flag;
+static int old_builtin_variables_flag;
 
 /* Nonzero means all variables are automatically exported.  */
 
@@ -2052,9 +2055,9 @@ main (int argc, char **argv, char **envp)
     }
 
   {
-    int old_builtin_rules_flag = no_builtin_rules_flag;
-    int old_builtin_variables_flag = no_builtin_variables_flag;
     int old_arg_job_slots = arg_job_slots;
+    old_builtin_rules_flag = no_builtin_rules_flag;
+    old_builtin_variables_flag = no_builtin_variables_flag;
 
     /* Read all the makefiles.  */
     read_files = read_all_makefiles (makefiles == 0 ? 0 : makefiles->list);
@@ -2100,24 +2103,7 @@ main (int argc, char **argv, char **envp)
     make_sync.syncout = syncing;
     OUTPUT_SET (&make_sync);
 
-    /* If -R was given, set -r too (doesn't make sense otherwise!)  */
-    if (no_builtin_variables_flag)
-      no_builtin_rules_flag = 1;
-
-    /* If we've disabled builtin rules, get rid of them.  */
-    if (no_builtin_rules_flag && ! old_builtin_rules_flag)
-      {
-        if (suffix_file->builtin)
-          {
-            free_dep_chain (suffix_file->deps);
-            suffix_file->deps = 0;
-          }
-        define_variable_cname ("SUFFIXES", "", o_default, 0);
-      }
-
-    /* If we've disabled builtin variables, get rid of them.  */
-    if (no_builtin_variables_flag && ! old_builtin_variables_flag)
-      undefine_default_variables ();
+    disable_builtins ();
   }
 
 #if MK_OS_W32
@@ -3094,6 +3080,7 @@ reset_makeflags (enum variable_origin origin)
 {
   decode_env_switches (STRING_SIZE_TUPLE(MAKEFLAGS_NAME), origin);
   construct_include_path (include_dirs ? include_dirs->list : NULL);
+  disable_builtins ();
   define_makeflags (rebuilding_makefiles);
 }
 
@@ -3451,6 +3438,37 @@ quote_for_env (char *out, const char *in)
   return out;
 }
 
+/* Disable builtin variables and rules, if -R or -r is specified.
+ * This function is called at parse time whenever MAKEFLAGS is modified and
+ * also when the parsing phase is over.  */
+
+static
+void disable_builtins ()
+{
+    /* If -R was given, set -r too (doesn't make sense otherwise!)  */
+    if (no_builtin_variables_flag)
+      no_builtin_rules_flag = 1;
+
+    /* If we've disabled builtin rules, get rid of them.  */
+    if (no_builtin_rules_flag && ! old_builtin_rules_flag)
+      {
+        old_builtin_rules_flag = 1;
+        if (suffix_file->builtin)
+          {
+            free_dep_chain (suffix_file->deps);
+            suffix_file->deps = 0;
+          }
+        define_variable_cname ("SUFFIXES", "", o_default, 0);
+      }
+
+    /* If we've disabled builtin variables, get rid of them.  */
+    if (no_builtin_variables_flag && ! old_builtin_variables_flag)
+      {
+        old_builtin_variables_flag = 1;
+        undefine_default_variables ();
+      }
+}
+
 /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
    command switches. Always include options with args.
    Don't include options with the 'no_makefile' flag set if MAKEFILE.  */
