Skip to contentSkip to conversation

wic: fix parsing of 'bitbake -e' output

PATCH Superseded Ed Bartosh 2 participants 4 messages Dec 21, 2016

lore
Share & tools
lore

Ed Bartosh

author
#0 lore raw

PATCH 1/1 wic: fix parsing of 'bitbake -e' output

Current parsing code can wrongly interpret arbitrary lines that are of 'key=value' format as legitimate bitbake variables.

Implemented more strict parsing of key=value pairs using regular expressions.

---
 scripts/lib/wic/utils/oe/misc.py | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
scripts/lib/wic/utils/oe/misc.py +5 -7
index fe188c9..1dbbe92 100644
@@ -27,6 +27,7 @@
 """Miscellaneous functions."""
 
 import os
+import re
 from collections import defaultdict
 from distutils import spawn
 
@@ -155,14 +156,11 @@ class BitbakeVars(defaultdict):
         """
         if "=" not in line:
             return
-        try:
-            key, val = line.split("=")
-        except ValueError:
+        match = re.match("^(\w+)=(.+)", line)
+        if not match:
             return
-        key = key.strip()
-        val = val.strip()
-        if key.replace('_', '').isalnum():
-            self[image][key] = val.strip('"')
+        key, val = match.groups()
+        self[image][key] = val.strip('"')
 
     def get_var(self, var, image=None):
         """
Signature · 1 line
2.1.4
#1 lore raw
Quoted 24 lines · Ed Bartosh

Current parsing code can wrongly interpret arbitrary lines that are of 'key=value' format as legitimate bitbake variables.

Implemented more strict parsing of key=value pairs using regular expressions.

---
 scripts/lib/wic/utils/oe/misc.py | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
scripts/lib/wic/utils/oe/misc.py +1 -0
index fe188c9..1dbbe92 100644
@@ -27,6 +27,7 @@
 """Miscellaneous functions."""
 
 import os
+import re
 from collections import defaultdict
 from distutils import spawn
 

scripts/lib/wic/utils/oe/misc.py class BitbakeVars(defaultdict): · line 159

Quoted 7 lines
scripts/lib/wic/utils/oe/misc.py +1 -3
@@ -155,14 +156,11 @@ class BitbakeVars(defaultdict):
         """
         if "=" not in line:
             return
-        try:
-            key, val = line.split("=")
-        except ValueError:
+        match = re.match("^(\w+)=(.+)", line)

match = re.match(r"^(\w+)=(.+)", line)

I don't remember if regexps are cached when compiled in re.match(), but perhaps it would be better to re.compile(r"^(\w+)=(.+)") in BitbakeVars.__init__() and just use a cached object here?

Quoted 14 lines
scripts/lib/wic/utils/oe/misc.py +3 -5
+        if not match:
             return
-        key = key.strip()
-        val = val.strip()
-        if key.replace('_', '').isalnum():
-            self[image][key] = val.strip('"')
+        key, val = match.groups()
+        self[image][key] = val.strip('"')
 
     def get_var(self, var, image=None):
         """
--

2.1.4

Signature · 2 lines
Maciej Borzecki
RnDity
author
#2 lore raw

PATCH v2 1/1 wic: fix parsing of 'bitbake -e' output

Current parsing code can wrongly interpret arbitrary lines that are of 'key=value' format as legitimate bitbake variables.

Implemented more strict parsing of key=value pairs using regular expressions.

---
 scripts/lib/wic/utils/oe/misc.py | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
scripts/lib/wic/utils/oe/misc.py +6 -8
index fe188c9..2a2fcc9 100644
@@ -27,6 +27,7 @@
 """Miscellaneous functions."""
 
 import os
+import re
 from collections import defaultdict
 from distutils import spawn
 
@@ -148,21 +149,18 @@ class BitbakeVars(defaultdict):
         self.default_image = None
         self.vars_dir = None
 
-    def _parse_line(self, line, image):
+    def _parse_line(self, line, image, matcher=re.compile(r"^(\w+)=(.+)")):
         """
         Parse one line from bitbake -e output or from .env file.
         Put result key-value pair into the storage.
         """
         if "=" not in line:
             return
-        try:
-            key, val = line.split("=")
-        except ValueError:
+        match = matcher.match(line)
+        if not match:
             return
-        key = key.strip()
-        val = val.strip()
-        if key.replace('_', '').isalnum():
-            self[image][key] = val.strip('"')
+        key, val = match.groups()
+        self[image][key] = val.strip('"')
 
     def get_var(self, var, image=None):
         """
Signature · 1 line
2.1.4
#3 lore raw
Quoted 51 lines · Ed Bartosh

Current parsing code can wrongly interpret arbitrary lines that are of 'key=value' format as legitimate bitbake variables.

Implemented more strict parsing of key=value pairs using regular expressions.

---
 scripts/lib/wic/utils/oe/misc.py | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
scripts/lib/wic/utils/oe/misc.py +6 -9
index fe188c9..2a2fcc9 100644
@@ -27,6 +27,7 @@
 """Miscellaneous functions."""
 
 import os
+import re
 from collections import defaultdict
 from distutils import spawn
 
@@ -148,21 +149,18 @@ class BitbakeVars(defaultdict):
         self.default_image = None
         self.vars_dir = None
 
-    def _parse_line(self, line, image):
+    def _parse_line(self, line, image, matcher=re.compile(r"^(\w+)=(.+)")):
         """
         Parse one line from bitbake -e output or from .env file.
         Put result key-value pair into the storage.
         """
         if "=" not in line:
             return
-        try:
-            key, val = line.split("=")
-        except ValueError:
+        match = matcher.match(line)
+        if not match:
             return
-        key = key.strip()
-        val = val.strip()
-        if key.replace('_', '').isalnum():
-            self[image][key] = val.strip('"')
+        key, val = match.groups()
+        self[image][key] = val.strip('"')
 
     def get_var(self, var, image=None):
         """
--
Quoted 2 lines

2.1.4

Looks good to me.

Thanks for fixing this so quickly.

Signature · 2 lines
Maciej Borzecki
RnDity