Bug #281834 - In getconfig(), do not allow definition of variables that have
authorZac Medico <zmedico@gentoo.org>
Fri, 28 Aug 2009 09:04:42 +0000 (09:04 -0000)
committerZac Medico <zmedico@gentoo.org>
Fri, 28 Aug 2009 09:04:42 +0000 (09:04 -0000)
invalid names according to shell standards (such as names containing hyphens).

svn path=/main/trunk/; revision=14167

pym/portage/util.py

index 6f11934e854c81b77cb1678f2299ecfc74ef8867..9f7c928288a1aef0a3f2c26e8f6917338042aeea 100644 (file)
@@ -16,6 +16,7 @@ import commands
 import codecs
 import errno
 import logging
+import re
 import shlex
 import stat
 import string
@@ -379,6 +380,8 @@ class _tolerant_shlex(shlex.shlex):
                                (self.infile, str(e)), noiselevel=-1)
                        return (newfile, StringIO())
 
+_invalid_var_name_re = re.compile(r'^\d|\W')
+
 def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True):
        if isinstance(expand, dict):
                # Some existing variable definitions have been
@@ -462,6 +465,16 @@ def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True):
                                        return mykeys
                        key = _unicode_decode(key)
                        val = _unicode_decode(val)
+
+                       if _invalid_var_name_re.search(key) is not None:
+                               if not tolerant:
+                                       raise Exception(_(
+                                               "ParseError: Invalid variable name '%s': line %s") % \
+                                               (key, lex.lineno - 1))
+                               writemsg(_("!!! Invalid variable name '%s': line %s in %s\n") \
+                                       % (key, lex.lineno - 1, mycfg), noiselevel=-1)
+                               continue
+
                        if expand:
                                mykeys[key] = varexpand(val, expand_map)
                                expand_map[key] = mykeys[key]