From: Simon Marchi <simon.marchi@polymtl.ca>
To: Sergio Durigan Junior <sergiodj@redhat.com>
Cc: GDB Patches <gdb-patches@sourceware.org>,
Pedro Alves <palves@redhat.com>
Subject: Re: [PATCH v5] C++ify gdb/common/environ.c
Date: Sat, 17 Jun 2017 08:54:00 -0000 [thread overview]
Message-ID: <4e43c71a2ac4aa229bb262e18dec668c@polymtl.ca> (raw)
In-Reply-To: <20170616222315.12779-1-sergiodj@redhat.com>
On 2017-06-17 00:23, Sergio Durigan Junior wrote:
> void
> -set_in_environ (struct gdb_environ *e, const char *var, const char
> *value)
> +gdb_environ::set (const char *var, const char *value)
> {
> - int i;
> - int len = strlen (var);
> - char **vector = e->vector;
> - char *s;
> -
> - for (i = 0; (s = vector[i]) != NULL; i++)
> - if (strncmp (s, var, len) == 0 && s[len] == '=')
> - break;
> + /* We have to unset the variable in the vector if it exists. */
> + unset (var);
>
> - if (s == 0)
> - {
> - if (i == e->allocated)
> - {
> - e->allocated += 10;
> - vector = (char **) xrealloc ((char *) vector,
> - (e->allocated + 1) * sizeof (char *));
> - e->vector = vector;
> - }
> - vector[i + 1] = 0;
> - }
> - else
> - xfree (s);
> -
> - s = (char *) xmalloc (len + strlen (value) + 2);
> - strcpy (s, var);
> - strcat (s, "=");
> - strcat (s, value);
> - vector[i] = s;
> -
> - /* This used to handle setting the PATH and GNUTARGET variables
> - specially. The latter has been replaced by "set gnutarget"
> - (which has worked since GDB 4.11). The former affects searching
> - the PATH to find SHELL, and searching the PATH to find the
> - argument of "symbol-file" or "exec-file". Maybe we should have
> - some kind of "set exec-path" for that. But in any event, having
> - "set env" affect anything besides the inferior is a bad idea.
> - What if we want to change the environment we pass to the program
> - without afecting GDB's behavior? */
> -
> - return;
> + /* Insert the element before the last one, which is always NULL. */
> + m_environ_vector.insert (m_environ_vector.end () - 1,
> + concat (var, "=", value, NULL));
The breaks if we have just constructed an empty gdb_environ object, as
the vector is completely empty (no terminating NULL). So we'd need some
kind of check before that, if the vector is empty, add a NULL element...
I actually preferred the option of adding the NULL element to the vector
in the gdb_environ constructor, since it allows always having the vector
in a consistent state. I don't think that avoiding that heap allocation
is worth the complexity it adds to the code (unless we can prove
otherwise by memory usage profiling).
> +static void
> +run_tests ()
> +{
> + if (setenv ("GDB_SELFTEST_ENVIRON", "1", 1) != 0)
> + error ("Could not set environment variable for testing.");
> +
> + gdb_environ env;
> +
> + SELF_CHECK (env.envp ()[0] == NULL);
> +
> + SELF_CHECK (env.get ("PWD") == NULL);
If you add
env.set ("PWD", "/home");
you should see a crash.
Simon
next prev parent reply other threads:[~2017-06-17 8:54 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-13 4:05 [PATCH] " Sergio Durigan Junior
2017-04-15 18:51 ` [PATCH v2] " Sergio Durigan Junior
2017-04-15 21:22 ` Simon Marchi
2017-04-18 2:49 ` Sergio Durigan Junior
2017-04-16 5:09 ` Simon Marchi
2017-04-16 17:32 ` Sergio Durigan Junior
2017-04-18 3:03 ` [PATCH v3] " Sergio Durigan Junior
2017-04-19 4:56 ` Simon Marchi
2017-04-19 16:30 ` Pedro Alves
2017-04-19 18:14 ` Pedro Alves
2017-05-01 2:22 ` Sergio Durigan Junior
2017-05-04 15:30 ` Pedro Alves
2017-06-14 19:22 ` [PATCH v4] " Sergio Durigan Junior
2017-06-16 15:45 ` Pedro Alves
2017-06-16 18:01 ` Sergio Durigan Junior
2017-06-16 18:23 ` Pedro Alves
2017-06-16 21:59 ` Sergio Durigan Junior
2017-06-16 22:23 ` [PATCH v5] " Sergio Durigan Junior
2017-06-17 8:54 ` Simon Marchi [this message]
2017-06-19 4:19 ` Sergio Durigan Junior
2017-06-19 13:40 ` Pedro Alves
2017-06-19 16:19 ` Sergio Durigan Junior
2017-06-19 12:13 ` Pedro Alves
2017-06-20 14:02 ` Pedro Alves
2017-06-19 4:36 ` [PATCH v6] " Sergio Durigan Junior
2017-06-19 4:51 ` Sergio Durigan Junior
2017-06-19 7:18 ` Simon Marchi
2017-06-19 14:26 ` Pedro Alves
2017-06-19 15:30 ` Simon Marchi
2017-06-19 15:44 ` Pedro Alves
2017-06-19 15:47 ` Pedro Alves
2017-06-19 16:26 ` Simon Marchi
2017-06-19 16:55 ` Pedro Alves
2017-06-19 17:59 ` Sergio Durigan Junior
2017-06-19 18:09 ` Pedro Alves
2017-06-19 18:23 ` Sergio Durigan Junior
2017-06-19 18:36 ` Pedro Alves
2017-06-19 18:38 ` Pedro Alves
2017-06-19 14:26 ` Pedro Alves
2017-06-19 16:13 ` Sergio Durigan Junior
2017-06-19 16:38 ` Pedro Alves
2017-06-19 16:46 ` Sergio Durigan Junior
2017-06-19 18:27 ` [PATCH v7] " Sergio Durigan Junior
2017-06-20 3:27 ` [PATCH v8] " Sergio Durigan Junior
2017-06-20 12:13 ` Pedro Alves
2017-06-20 12:46 ` Simon Marchi
2017-06-20 13:00 ` Sergio Durigan Junior
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=4e43c71a2ac4aa229bb262e18dec668c@polymtl.ca \
--to=simon.marchi@polymtl.ca \
--cc=gdb-patches@sourceware.org \
--cc=palves@redhat.com \
--cc=sergiodj@redhat.com \
/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).