diff --git a/gdb/common/environ.c b/gdb/common/environ.c
index 2d13957..c2d0d95 100644
--- a/gdb/common/environ.c
+++ b/gdb/common/environ.c
@@ -30,8 +30,6 @@ gdb_environ::operator= (gdb_environ &&e)
     return *this;
 
   m_environ_vector = std::move (e.m_environ_vector);
-  e.m_environ_vector.clear ();
-  e.m_environ_vector.push_back (NULL);
   return *this;
 }
 
@@ -42,15 +40,12 @@ gdb_environ gdb_environ::from_host_environ ()
   extern char **environ;
   gdb_environ e;
 
-  if (environ == NULL)
+  if (environ == NULL || environ[0] == NULL)
     return e;
 
   for (int i = 0; environ[i] != NULL; ++i)
-    {
-      /* Make sure we add the element before the last (NULL).  */
-      e.m_environ_vector.insert (e.m_environ_vector.end () - 1,
-				 xstrdup (environ[i]));
-    }
+    e.m_environ_vector.push_back (xstrdup (environ[i]));
+  e.m_environ_vector.push_back (NULL);
 
   return e;
 }
@@ -63,8 +58,6 @@ gdb_environ::clear ()
   for (char *v : m_environ_vector)
     xfree (v);
   m_environ_vector.clear ();
-  /* Always add the NULL element.  */
-  m_environ_vector.push_back (NULL);
 }
 
 /* Helper function to check if STRING contains an environment variable
@@ -99,10 +92,18 @@ gdb_environ::get (const char *var) const
 void
 gdb_environ::set (const char *var, const char *value)
 {
-  /* We have to unset the variable in the vector if it exists.  */
+  if (m_environ_vector.empty ())
+    {
+      m_environ_vector.push_back (concat (var, "=", value, NULL));
+      m_environ_vector.push_back (NULL);
+      return;
+    }
+
+  /* Unset the variable in the vector if it exists.  */
   unset (var);
 
-  /* Insert the element before the last one, which is always NULL.  */
+  /* Insert the element before the last one, which is the NULL
+     terminator.  */
   m_environ_vector.insert (m_environ_vector.end () - 1,
 			   concat (var, "=", value, NULL));
 }
@@ -112,10 +113,13 @@ gdb_environ::set (const char *var, const char *value)
 void
 gdb_environ::unset (const char *var)
 {
+  if (m_environ_vector.empty ())
+    return;
+
   size_t len = strlen (var);
 
-  /* We iterate until '.cend () - 1' because the last element is
-     always NULL.  */
+  /* We iterate until '.end () - 1' because the last element is the
+     NULL terminator.  */
   for (std::vector<char *>::iterator el = m_environ_vector.begin ();
        el != m_environ_vector.end () - 1;
        ++el)
@@ -127,10 +131,17 @@ gdb_environ::unset (const char *var)
       }
 }
 
+/* An empty envp.  This is what the envp() method returns when the
+   internal vector is empty.  */
+static const char *const empty_envp[1] = { NULL };
+
 /* See common/environ.h.  */
 
 char **
 gdb_environ::envp () const
 {
-  return const_cast<char **> (&m_environ_vector[0]);
+  const char *const *e = (m_environ_vector.empty ()
+			  ? empty_envp
+			  : &m_environ_vector[0]);
+  return const_cast<char **> (e);
 }
diff --git a/gdb/common/environ.h b/gdb/common/environ.h
index 0bbb191..1a5494b 100644
--- a/gdb/common/environ.h
+++ b/gdb/common/environ.h
@@ -25,14 +25,7 @@
 class gdb_environ
 {
 public:
-  /* Regular constructor and destructor.  */
-  gdb_environ ()
-  {
-    /* Make sure that the vector contains at least a NULL element.
-       If/when we add more variables to it, NULL will always be the
-       last element.  */
-    m_environ_vector.push_back (NULL);
-  }
+  gdb_environ () = default;
 
   ~gdb_environ ()
   {
@@ -42,12 +35,7 @@ public:
   /* Move constructor.  */
   gdb_environ (gdb_environ &&e)
     : m_environ_vector (std::move (e.m_environ_vector))
-  {
-    /* Make sure that the moved-from vector is left at a valid
-       state (only one NULL element).  */
-    e.m_environ_vector.clear ();
-    e.m_environ_vector.push_back (NULL);
-  }
+  {}
 
   /* Move assignment.  */
   gdb_environ &operator= (gdb_environ &&e);
@@ -74,7 +62,10 @@ public:
   char **envp () const;
 
 private:
-  /* A vector containing the environment variables.  */
+  /* A vector containing the environment variables.  An initially
+     empty envp is internally represented with an empty vector to
+     avoid having to push a NULL (which heap-allocates) on default
+     construction and on moved-from objects.  */
   std::vector<char *> m_environ_vector;
 };
 
