Skip to contentSkip to conversation

data: Escape back quotes in environment

PATCH In review Mathieu Dubois-Briand 2 participants 4 messages Nov 14, 2025

lore
Share & tools
lore
#0 lore raw

cover data: Escape back quotes in environment

Fixing devshell when some environment variables contain a back quote. On my side I had this issue using kitty terminal emulator, which sets the KITTY_PUBLIC_KEY environment variable [1]:
KITTY_PUBLIC_KEY=1:`TB}~6^vgiFD2v6d}JRgt!X|qx#0~mO?&N=A1jfi

Trying to use devshell failed silently, but the parse error is visible while launching the do_terminal script by hand:

tmp/work/qemux86_64-poky-linux/linux-yocto/6.16.11+git/temp/run.do_terminal.2972397: line 120: unexpected EOF while looking for matching ``'
WARNING: tmp/work/qemux86_64-poky-linux/linux-yocto/6.16.11+git/temp/run.do_terminal.2972397:119 exit 2 from 'export KITTY_PID="1985830"'
WARNING: Backtrace (BB generated script):

	#1: main, tmp/work/qemux86_64-poky-linux/linux-yocto/6.16.11+git/temp/run.do_terminal.2972397, line 119

Escaping back quotes allows to launch the devshell correctly.

Also adding a non-regression test. The whole bb.data.emit_var() function was not tested, I'm not sure if it was by choice or not, but I added some.

I tested the most basic code paths of bb.data.emit_var() but I'm not fully exhaustive. Notably, the return value remains untested as I was not able to figure out what it means and it appears to be unused.

[1]: https://sw.kovidgoyal.net/kitty/glossary/#envvar-KITTY_PUBLIC_KEY

---
Mathieu Dubois-Briand (2):
data: Escape back quotes in environment
tests: data: Add tests on emitting environment variables

 lib/bb/data.py       |   1 +
 lib/bb/tests/data.py | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+)

---
base-commit: ce90d59ff1c96d04856ca5e41a963af00d1783f1
change-id: 20251114-mathieu-back_quote_devshell-b1b19d3c4042

Best regards,

Signature · 1 line
Mathieu Dubois-Briand <[email protected]>
#1 lore raw

PATCH 1/2 data: Escape back quotes in environment

Escape back quotes in environment variables while generating environment files, as otherwise they will generate a parsing error:
unexpected EOF while looking for matching ``'

---
 lib/bb/data.py | 1 +
 1 file changed, 1 insertion(+)
lib/bb/data.py +1 -0
index f672a844518d..061e63386f04 100644
@@ -171,6 +171,7 @@ def emit_var(var, o=sys.__stdout__, d = init(), all=False):
     # if we're going to output this within doublequotes,
     # to a shell, we need to escape the quotes in the var
     alter = re.sub('"', '\\"', val)
+    alter = re.sub('`', '\\`', alter)
     alter = re.sub('\n', ' \\\n', alter)
     alter = re.sub('\\$', '\\\\$', alter)
     o.write('%s="%s"\n' % (varExpanded, alter))
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/2 tests: data: Add tests on emitting environment variables

In reply to Mathieu Dubois-Briand
---
 lib/bb/tests/data.py | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 105 insertions(+)
lib/bb/tests/data.py +105 -0
index a895f6a58efb..fd690a9e24fb 100644
@@ -11,6 +11,7 @@ import unittest
 import bb
 import bb.data
 import bb.parse
+import io
 import logging
 import os
 
@@ -617,4 +618,108 @@ class Serialize(unittest.TestCase):
         self.assertEqual(newd.getVar('HELLO'), 'world')
         self.assertEqual(newd.getVarFlag('HELLO', 'other'), 'planet')
 
+class EmitVar(unittest.TestCase):
+    def setUp(self):
+        self.d = bb.data.init()
+
+        self.d.setVar("foo", "value of foo")
+
+        self.d.setVar("foo_exported", "value of foo_exported")
+        self.d.setVarFlag("foo_exported", "export", "1")
+
+        self.d.setVar("foo_unexported", "value of foo_unexported")
+        self.d.setVarFlag("foo_unexported", "unexport", "1")
+
+        self.d.setVar("test_func", "echo test_func_l1\necho test_func_l2")
+        self.d.setVarFlag("test_func", "func", "1")
+
+        self.d.setVar("bad_chars", 'a"b\nc`d\ne$f')
+
+    @staticmethod
+    def get_output(out):
+        lines = [line for line in out.getvalue().splitlines()
+                 if len(line) == 0 or line[0] != '#'
+                 ]
+        out.truncate(0)
+        out.seek(0)
+        return lines
+
+    def test_simple_variable(self):
+        out = io.StringIO()
+
+        bb.data.emit_var('foo', out, self.d, all=True)
+        self.assertEqual(self.get_output(out), ['foo="value of foo"'])
+
+    def test_exported(self):
+        out = io.StringIO()
+
+        bb.data.emit_var('foo_exported', out, self.d)
+        self.assertEqual(self.get_output(out),
+                         ['export foo_exported="value of foo_exported"'])
+
+    def test_unexported(self):
+        out = io.StringIO()
+
+        bb.data.emit_var('foo_unexported', out, self.d)
+        self.assertEqual(self.get_output(out), ['unset foo_unexported'])
+
+    def test_function(self):
+        out = io.StringIO()
+
+        bb.data.emit_var('test_func', out, self.d)
+        self.assertEqual(self.get_output(out), ['test_func() {',
+                                                'echo test_func_l1',
+                                                'echo test_func_l2',
+                                                '}'])
+
+    def test_all(self):
+        out = io.StringIO()
+
+        bb.data.emit_var('foo', out, self.d, all=True)
+        self.assertEqual(self.get_output(out), ['foo="value of foo"'])
+
+        bb.data.emit_var('foo_exported', out, self.d, all=True)
+        self.assertEqual(self.get_output(out),
+                         ['export foo_exported="value of foo_exported"'])
+        bb.data.emit_var('foo_unexported', out, self.d, all=True)
+
+        self.assertEqual(self.get_output(out), ['unset foo_unexported'])
+
+        bb.data.emit_var('test_func', out, self.d, all=True)
+        self.assertEqual(self.get_output(out), ['test_func() {',
+                                                'echo test_func_l1',
+                                                'echo test_func_l2',
+                                                '}'])
+
+    def test_not_all(self):
+        out = io.StringIO()
+
+        bb.data.emit_var('foo', out, self.d)
+        self.assertEqual(self.get_output(out), [])
+
+        bb.data.emit_var('foo_exported', out, self.d)
+        self.assertEqual(self.get_output(out),
+                         ['export foo_exported="value of foo_exported"'])
+
+        bb.data.emit_var('foo_unexported', out, self.d)
+        self.assertEqual(self.get_output(out), ['unset foo_unexported'])
+
+        bb.data.emit_var('test_func', out, self.d)
+        self.assertEqual(self.get_output(out), ['test_func() {',
+                                                'echo test_func_l1',
+                                                'echo test_func_l2',
+                                                '}'])
+
+    def test_not_set(self):
+        out = io.StringIO()
+
+        bb.data.emit_var('random_name', out, self.d)
+        self.assertEqual(self.get_output(out), [])
+
+    def test_bad_chars(self):
+        out = io.StringIO()
 
+        bb.data.emit_var('bad_chars', out, self.d, all=True)
+        self.assertEqual(self.get_output(out), ['bad_chars="a\\"b \\',
+                                                'c\\`d \\',
+                                                'e\\$f"'])
Signature · 1 line
2.47.3
#3 lore raw
In reply to Mathieu Dubois-Briand
Quoted 15 lines · Mathieu Dubois-Briand via lists.openembedded.org

Fixing devshell when some environment variables contain a back quote. On my side I had this issue using kitty terminal emulator, which sets the KITTY_PUBLIC_KEY environment variable [1]:
KITTY_PUBLIC_KEY=1:`TB}~6^vgiFD2v6d}JRgt!X|qx#0~mO?&N=A1jfi

Trying to use devshell failed silently, but the parse error is visible while launching the do_terminal script by hand:

tmp/work/qemux86_64-poky-linux/linux-yocto/6.16.11+git/temp/run.do_terminal.2972397: line 120: unexpected EOF while looking for matching ``'
WARNING: tmp/work/qemux86_64-poky-linux/linux-yocto/6.16.11+git/temp/run.do_terminal.2972397:119 exit 2 from 'export KITTY_PID="1985830"'
WARNING: Backtrace (BB generated script):

	#1: main, tmp/work/qemux86_64-poky-linux/linux-yocto/6.16.11+git/temp/run.do_terminal.2972397, line 119

Escaping back quotes allows to launch the devshell correctly.

Quoted 7 lines

Also adding a non-regression test. The whole bb.data.emit_var() function was not tested, I'm not sure if it was by choice or not, but I added some.

I tested the most basic code paths of bb.data.emit_var() but I'm not fully exhaustive. Notably, the return value remains untested as I was not able to figure out what it means and it appears to be unused.

Tests are good thanks. They're not present as the test suite didn't exist when that code was written!

Cheers,

Richard