Merge lp:~vicamo/upstart/xenial-escape-systemd-strings into lp:~ubuntu-core-dev/upstart/xenial

Proposed by You-Sheng Yang
Status: Merged
Merged at revision: 1632
Proposed branch: lp:~vicamo/upstart/xenial-escape-systemd-strings
Merge into: lp:~ubuntu-core-dev/upstart/xenial
Diff against target: 83 lines (+51/-4)
1 file modified
extra/upstart-local-bridge.c (+51/-4)
To merge this branch: bzr merge lp:~vicamo/upstart/xenial-escape-systemd-strings
Reviewer Review Type Date Requested Status
Łukasz Zemczak not an upstart maintainer Approve
Review via email: mp+307140@code.launchpad.net

Description of the change

upstart-local-bridge: escape strings for systemd

Android property names and values may contain invalid characters for systemd and breaks the mechanism here.

To post a comment you must log in.
Revision history for this message
Łukasz Zemczak (sil2100) wrote :

In overall the proposed change looks fine an seems to work reasonably well. The same could have been realized without the new hexchar() function, but having it makes things potentially slightly faster and simplified.
That being said, I do not know the upstart code-base that well, so maybe it would be nice if some other maintainer took a quick look as well.

One stylistic recommendation: in the inline comments, the if, else if and else parts could use { } even in the single-line conditional commands. It's much cleaner this way and follows the upstart code style better.

review: Approve (not an upstart maintainer)
Revision history for this message
Łukasz Zemczak (sil2100) :
1633. By You-Sheng Yang

Address stylistic review comments.

Revision history for this message
Dimitri John Ledkov (xnox) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'extra/upstart-local-bridge.c'
2--- extra/upstart-local-bridge.c 2015-01-16 23:55:17 +0000
3+++ extra/upstart-local-bridge.c 2016-10-05 09:27:10 +0000
4@@ -912,6 +912,47 @@
5
6 }
7
8+static char
9+hexchar (char c)
10+{
11+ static const char table[16] = "0123456789abcdef";
12+
13+ return table[c & 0xf];
14+}
15+
16+static char*
17+escape_string (const char *f)
18+{
19+ char *r = NULL;
20+ char *t = NULL;
21+
22+ nih_assert (f);
23+
24+ r = t = NIH_MUST (nih_alloc (NULL, strlen(f) * 4 + 1));
25+
26+#define VALID_CHARS \
27+ "0123456789" \
28+ "abcdefghijklmnopqrstuvwxyz" \
29+ "ABCDEFGHIJKLMNOPQRSTUVWXYX" \
30+ ":_."
31+ for (; *f; f++) {
32+ if (*f == '/') {
33+ *(t++) = '-';
34+ } else if (!strchr(VALID_CHARS, *f)) {
35+ *(t++) = '\\';
36+ *(t++) = 'x';
37+ *(t++) = hexchar(*f >> 4);
38+ *(t++) = hexchar(*f);
39+ } else {
40+ *(t++) = *f;
41+ }
42+ }
43+
44+ *t = '\0';
45+
46+ return r;
47+}
48+
49 static void
50 systemd_launch_instance (ClientConnection *client,
51 const char *pair,
52@@ -919,6 +960,8 @@
53 {
54 nih_local char *safe_pair = NULL;
55 nih_local char **key_value = NULL;
56+ nih_local char *escaped_key = NULL;
57+ nih_local char *escaped_value = NULL;
58 nih_local char *group_name = NULL;
59 nih_local char *unit_name = NULL;
60 nih_local char *job_name = NULL;
61@@ -933,14 +976,18 @@
62 /* Get key val from the key=val pair */
63 key_value = NIH_MUST (nih_str_split (NULL, safe_pair, "=", TRUE));
64
65+ /* Escape key/value for systemd */
66+ escaped_key = NIH_MUST (escape_string (key_value[0]));
67+ escaped_value = NIH_MUST (escape_string (key_value[1] ? key_value[1] : "(null)"));
68+
69 /* Construct systemd event@key=*.target group name */
70- group_name = NIH_MUST (nih_sprintf (NULL, "%s@%s=*.target",
71- event_name, key_value[0]));
72+ group_name = NIH_MUST (nih_sprintf (NULL, "%s@%s\\x3d*.target",
73+ event_name, escaped_key));
74
75 /* Construct systemd event@key=value.target unit name */
76 unit_name = NIH_MUST (nih_sprintf (NULL, "%s@%s\\x3d%s.target",
77- event_name, key_value[0],
78- key_value[1]));
79+ event_name, escaped_key,
80+ escaped_value));
81
82 /* Stop group */
83 int pid = -1;

Subscribers

People subscribed via source and target branches