[systemd-commits] src/shared src/test
Lennart Poettering
lennart at kemper.freedesktop.org
Wed Sep 19 13:02:04 PDT 2012
src/shared/util.c | 40 +++++++++++++++++++++-------------------
src/test/test-env-replace.c | 16 ++++++++++++++++
2 files changed, 37 insertions(+), 19 deletions(-)
New commits:
commit 57f3067825d9361d7487f272bfaff3b36c684c62
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Sep 19 22:01:31 2012 +0200
util: fix bad strstrip() return value in normalize_env_assignment()
https://bugzilla.redhat.com/show_bug.cgi?id=858780
diff --git a/src/shared/util.c b/src/shared/util.c
index 59c1417..69c9437 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -3881,7 +3881,8 @@ char *unquote(const char *s, const char* quotes) {
/* This is rather stupid, simply removes the heading and
* trailing quotes if there is one. Doesn't care about
- * escaping or anything. */
+ * escaping or anything. We should make this smarter one
+ * day...*/
l = strlen(s);
if (l < 2)
@@ -3894,39 +3895,40 @@ char *unquote(const char *s, const char* quotes) {
}
char *normalize_env_assignment(const char *s) {
- char *name, *value, *p, *r;
+ _cleanup_free_ char *name = NULL, *value = NULL, *p = NULL;
+ char *eq, *r;
- p = strchr(s, '=');
+ eq = strchr(s, '=');
+ if (!eq) {
+ char *t;
- if (!p) {
- if (!(r = strdup(s)))
+ r = strdup(s);
+ if (!r)
return NULL;
- return strstrip(r);
+ t = strstrip(r);
+ if (t == r)
+ return r;
+
+ memmove(r, t, strlen(t) + 1);
+ return r;
}
- if (!(name = strndup(s, p - s)))
+ name = strndup(s, eq - s);
+ if (!name)
return NULL;
- if (!(p = strdup(p+1))) {
- free(name);
+ p = strdup(eq + 1);
+ if (!p)
return NULL;
- }
value = unquote(strstrip(p), QUOTES);
- free(p);
-
- if (!value) {
- free(name);
+ if (!value)
return NULL;
- }
- if (asprintf(&r, "%s=%s", name, value) < 0)
+ if (asprintf(&r, "%s=%s", strstrip(name), value) < 0)
r = NULL;
- free(value);
- free(name);
-
return r;
}
diff --git a/src/test/test-env-replace.c b/src/test/test-env-replace.c
index 4b6b884..cd596a6 100644
--- a/src/test/test-env-replace.c
+++ b/src/test/test-env-replace.c
@@ -104,6 +104,22 @@ int main(int argc, char *argv[]) {
printf("%s\n", t);
free(t);
+ t = normalize_env_assignment(" xyz ");
+ printf("<%s>\n", t);
+ free(t);
+
+ t = normalize_env_assignment(" xyz = bar ");
+ printf("<%s>\n", t);
+ free(t);
+
+ t = normalize_env_assignment(" xyz = 'bar ' ");
+ printf("<%s>\n", t);
+ free(t);
+
+ t = normalize_env_assignment(" ' xyz' = 'bar ' ");
+ printf("<%s>\n", t);
+ free(t);
+
a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF", NULL);
b = strv_new("FOO=KKK", "FOO=", "PIEP=", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL);
More information about the systemd-commits
mailing list