public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Olivier Hainque <hainque@adacore.com>
To: Bernd Schmidt <bschmidt@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: prevent "undef var" errors on gcc --help or --version
Date: Tue, 12 Jan 2016 16:11:00 -0000	[thread overview]
Message-ID: <4776E0F5-184D-44AB-83D5-E80DE8A3B323@adacore.com> (raw)
In-Reply-To: <5693D3C3.4010908@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 1446 bytes --]

Hello Bernd,

Thanks for your feedback on this :-)

> On 11 Jan 2016, at 17:09, Bernd Schmidt <bschmidt@redhat.com> wrote:
> 
> On 01/08/2016 02:23 PM, Olivier Hainque wrote:
>> +  /* Undefined variable references in specs are harmless if
>> +     we're running for --help or --version alone, or together.  */
>> +  spec_undefvar_allowed =
>> +    (((print_version || print_help_list)
>> +      && decoded_options_count == 2)
>> +     ||
>> +     ((print_version && print_help_list)
>> +      && decoded_options_count == 3));
>> +
> 
> This doesn't follow the formatting rules.

Arg, indeed. Revised version attached.

> Also, there are a couple of other options that cause gcc to just print something and exit. Are these affected by missing env vars?

Some of these, for sure. For example, a common use case here is to
define a default --sysroot. We need this to be set properly for at
least --print-search-dirs and --print-prog-name, probably --print-file-name.

The print-multi family might be ok. It's heavily based on the presence
of other options on the command line, but maybe never depending on argument
values. I wasn't ready to bet though and opted for a conservative approach
first.

The attached patch is doing the same as the previous one, except more
explicitly and making it easier to adapt if deemed useful.

I could extract the decision code in a separate function if you prefer.

Olivier


[-- Attachment #2: spec-undef.diff --]
[-- Type: application/octet-stream, Size: 2763 bytes --]

diff --git a/gcc/gcc.c b/gcc/gcc.c
index 319a073..92fb051 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -3299,6 +3299,11 @@ int n_infiles;
 
 static int n_infiles_alloc;
 
+/* True if undefined environment variables encountered during spec processing
+   are ok to ignore, typically when we're running for --help or --version.  */
+
+static bool spec_undefvar_allowed;
+
 /* True if multiple input files are being compiled to a single
    assembly file.  */
 
@@ -4542,6 +4547,27 @@ process_command (unsigned int decoded_options_count,
       add_infile ("help-dummy", "c");
     }
 
+  /* Decide if undefined variable references are allowed in specs.  */
+  {
+    /* --version and --help alone or together are safe.  Note that -v would
+       make them unsafe, as they'd then be run for subprocesses as well, the
+       location of which might depend on variables possibly coming from
+       self-specs.  */
+
+    /* Count the number of options we have for which undefined variables
+       are harmless for sure, and check that nothing else is set.  */
+
+    unsigned n_varsafe_options = 0;
+
+    if (print_version)
+      n_varsafe_options++;
+
+    if (print_help_list)
+      n_varsafe_options++;
+
+    spec_undefvar_allowed = (decoded_options_count == n_varsafe_options + 1);
+  }
+
   alloc_switch ();
   switches[n_switches].part1 = 0;
   alloc_infile ();
@@ -9085,14 +9111,17 @@ print_multilib_info (void)
 \f
 /* getenv built-in spec function.
 
-   Returns the value of the environment variable given by its first
-   argument, concatenated with the second argument.  If the
-   environment variable is not defined, a fatal error is issued.  */
+   Returns the value of the environment variable given by its first argument,
+   concatenated with the second argument.  If the variable is not defined, a
+   fatal error is issued unless such undefs are internally allowed, in which
+   case the variable name is used as the variable value.  */
 
 static const char *
 getenv_spec_function (int argc, const char **argv)
 {
   const char *value;
+  const char *varname;
+
   char *result;
   char *ptr;
   size_t len;
@@ -9100,10 +9129,15 @@ getenv_spec_function (int argc, const char **argv)
   if (argc != 2)
     return NULL;
 
-  value = env.get (argv[0]);
+  varname = argv[0];
+  value = env.get (varname);
+
+  if (!value && spec_undefvar_allowed)
+    value = varname;
+
   if (!value)
     fatal_error (input_location,
-		 "environment variable %qs not defined", argv[0]);
+		 "environment variable %qs not defined", varname);
 
   /* We have to escape every character of the environment variable so
      they are not interpreted as active spec characters.  A

[-- Attachment #3: Type: text/plain, Size: 2 bytes --]




  reply	other threads:[~2016-01-12 16:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-08 13:23 Olivier Hainque
2016-01-11 16:09 ` Bernd Schmidt
2016-01-12 16:11   ` Olivier Hainque [this message]
2016-01-12 16:14     ` Bernd Schmidt
2016-01-12 16:37       ` Olivier Hainque

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4776E0F5-184D-44AB-83D5-E80DE8A3B323@adacore.com \
    --to=hainque@adacore.com \
    --cc=bschmidt@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).