[recipes] Be more careful when parsing the unit entry text
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [recipes] Be more careful when parsing the unit entry text
- Date: Fri, 28 Apr 2017 01:48:24 +0000 (UTC)
commit 4761d9feac2da13be35e7d1b84b2a27e8236637d
Author: Matthias Clasen <mclasen redhat com>
Date: Thu Apr 27 21:44:22 2017 -0400
Be more careful when parsing the unit entry text
If the entry is empty, we were using random memory.
Be more careful here.
src/gr-ingredients-viewer-row.c | 56 +++++++++++++++++++++++++++++---------
1 files changed, 42 insertions(+), 14 deletions(-)
---
diff --git a/src/gr-ingredients-viewer-row.c b/src/gr-ingredients-viewer-row.c
index 6a52c13..0d97273 100644
--- a/src/gr-ingredients-viewer-row.c
+++ b/src/gr-ingredients-viewer-row.c
@@ -130,8 +130,14 @@ static void
update_unit (GrIngredientsViewerRow *row)
{
g_autofree char *tmp = NULL;
-
- tmp = g_strconcat (row->amount ? row->amount : "", " ", row->unit, NULL);
+ const char *amount;
+ const char *space;
+ const char *unit;
+
+ amount = row->amount ? row->amount : "";
+ space = amount[0] ? " " : "";
+ unit = row->unit ? row->unit : "";
+ tmp = g_strdup_printf ("%s%s%s", amount, space, unit);
gtk_label_set_label (GTK_LABEL (row->unit_label), tmp);
}
@@ -249,8 +255,15 @@ static void
edit_ingredient (GrIngredientsViewerRow *row)
{
g_autofree char *tmp = NULL;
+ const char *amount;
+ const char *space;
+ const char *unit;
+
+ amount = row->amount ? row->amount : "";
+ space = amount[0] ? " " : "";
+ unit = row->unit ? row->unit : "";
+ tmp = g_strdup_printf ("%s%s%s", amount, space, unit);
- tmp = g_strconcat (row->amount ? row->amount : "", " ", row->unit, NULL);
gtk_entry_set_text (GTK_ENTRY (row->unit_entry), tmp);
gtk_entry_set_text (GTK_ENTRY (row->ingredient_entry), row->ingredient);
@@ -261,13 +274,29 @@ edit_ingredient (GrIngredientsViewerRow *row)
}
static void
+parse_unit (const char *text,
+ char **amount,
+ char **unit)
+{
+ g_autofree char *tmp = NULL;
+ g_autofree char **strv = NULL;
+
+ tmp = g_strstrip (g_strdup (text));
+ strv = g_strsplit (tmp, " ", 2);
+
+ g_clear_pointer (amount, g_free);
+ g_clear_pointer (unit, g_free);
+
+ *amount = strv[0];
+ if (g_strv_length (strv) > 1)
+ *unit = strv[1];
+}
+
+static void
save_row (GrIngredientsViewerRow *row)
{
- g_autofree char *tmp = g_strstrip (g_strdup (gtk_entry_get_text (GTK_ENTRY (row->unit_entry))));
- char** strv = g_strsplit (tmp, " ", -1);
+ parse_unit (gtk_entry_get_text (GTK_ENTRY (row->unit_entry)), &row->amount, &row->unit);
- row->amount = strv[0];
- row->unit = strv[1];
row->ingredient = g_strdup (gtk_entry_get_text (GTK_ENTRY (row->ingredient_entry)));
update_unit (row);
gtk_label_set_label (GTK_LABEL (row->ingredient_label), row->ingredient);
@@ -464,20 +493,19 @@ prepend_amount (GtkTreeModel *model,
GtkTreeModelFilter *filter_model = GTK_TREE_MODEL_FILTER (model);
GtkTreeModel *child_model;
GtkTreeIter child_iter;
- g_autofree char *unit = NULL;
+ g_autofree char *amount = NULL;
g_autofree char *tmp = NULL;
- g_autofree char *text = NULL;
- g_auto(GStrv) strv = NULL;
+ g_autofree char *unit = NULL;
+ g_autofree char *new_unit = NULL;
- text = g_strstrip (g_strdup (gtk_entry_get_text (GTK_ENTRY (row->unit_entry))));
- strv = g_strsplit (text, " ", 2);
+ parse_unit (gtk_entry_get_text (GTK_ENTRY (row->unit_entry)), &amount, &unit);
child_model = gtk_tree_model_filter_get_model (filter_model);
gtk_tree_model_filter_convert_iter_to_child_iter (filter_model, &child_iter, iter);
- gtk_tree_model_get (child_model, &child_iter, column, &unit, -1);
+ gtk_tree_model_get (child_model, &child_iter, column, &new_unit, -1);
- tmp = g_strdup_printf ("%s %s", strv[0], unit);
+ tmp = g_strdup_printf ("%s %s", amount, new_unit);
g_value_set_string (value, tmp);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]