Mirror of the lttng-dev mailing list
 help / color / mirror / Atom feed
From: jeremie.galarneau@efficios.com (Jérémie Galarneau)
Subject: [lttng-dev] [PATCH babeltrace 1/2] Fix ctf-writer: Quote strings provided as enumeration mappings
Date: Thu, 28 Nov 2013 15:46:40 -0500	[thread overview]
Message-ID: <1385671601-5884-1-git-send-email-jeremie.galarneau@efficios.com> (raw)

Signed-off-by: J?r?mie Galarneau <jeremie.galarneau at efficios.com>
---
 formats/ctf/writer/event-types.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/formats/ctf/writer/event-types.c b/formats/ctf/writer/event-types.c
index 4ed9fed..7f7fa76 100644
--- a/formats/ctf/writer/event-types.c
+++ b/formats/ctf/writer/event-types.c
@@ -33,6 +33,7 @@
 #include <babeltrace/endian.h>
 #include <float.h>
 #include <inttypes.h>
+#include <stdlib.h>
 
 struct range_overlap_query {
 	int64_t range_start, range_end;
@@ -385,6 +386,7 @@ int bt_ctf_field_type_enumeration_add_mapping(
 	struct enumeration_mapping *mapping;
 	struct bt_ctf_field_type_enumeration *enumeration;
 	struct range_overlap_query query;
+	char *escaped_string;
 
 	if (!type || (type->declaration->id != CTF_TYPE_ENUM) ||
 		type->frozen ||
@@ -393,12 +395,18 @@ int bt_ctf_field_type_enumeration_add_mapping(
 		goto end;
 	}
 
-	if (validate_identifier(string)) {
+	if (!string || strlen(string) == 0) {
 		ret = -1;
 		goto end;
 	}
 
-	mapping_name = g_quark_from_string(string);
+	escaped_string = g_strescape(string, NULL);
+	if (!escaped_string) {
+		ret = -1;
+		goto end;
+	}
+
+	mapping_name = g_quark_from_string(escaped_string);
 	query = (struct range_overlap_query) { .range_start = range_start,
 		.range_end = range_end,
 		.mapping_name = mapping_name,
@@ -410,18 +418,20 @@ int bt_ctf_field_type_enumeration_add_mapping(
 	g_ptr_array_foreach(enumeration->entries, check_ranges_overlap, &query);
 	if (query.overlaps) {
 		ret = -1;
-		goto end;
+		goto error_free;
 	}
 
 	mapping = g_new(struct enumeration_mapping, 1);
 	if (!mapping) {
 		ret = -1;
-		goto end;
+		goto error_free;
 	}
 
 	*mapping = (struct enumeration_mapping) {.range_start = range_start,
 		.range_end = range_end, .string = mapping_name};
 	g_ptr_array_add(enumeration->entries, mapping);
+error_free:
+	free(escaped_string);
 end:
 	return ret;
 }
@@ -1200,12 +1210,13 @@ int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *type,
 			enumeration->entries->pdata[entry];
 
 		if (mapping->range_start == mapping->range_end) {
-			g_string_append_printf(context->string, "%s = %" PRId64,
+			g_string_append_printf(context->string,
+				"\"%s\" = %" PRId64,
 				g_quark_to_string(mapping->string),
 				mapping->range_start);
 		} else {
 			g_string_append_printf(context->string,
-				"%s = %" PRId64 " ... %" PRId64,
+				"\"%s\" = %" PRId64 " ... %" PRId64,
 				g_quark_to_string(mapping->string),
 				mapping->range_start, mapping->range_end);
 		}
-- 
1.8.4.2




             reply	other threads:[~2013-11-28 20:46 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-28 20:46 Jérémie Galarneau [this message]
2013-11-28 20:46 ` [lttng-dev] [PATCH babeltrace 2/2] Test: Validate that enumeration mapping strings are properly escaped Jérémie Galarneau
2013-11-29  6:06   ` Mathieu Desnoyers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1385671601-5884-1-git-send-email-jeremie.galarneau@efficios.com \
    --to=jeremie.galarneau@efficios.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox