[PATCH] crypto: gracefully handle gmime errors

Jan Malakhovski oxij at oxij.org
Thu Aug 31 06:31:33 PDT 2017


Before: dies by SIGSEGV when gmime produces an error.
After: doesn't print NULL mime nodes.
---
 mime-node.c    |  5 +++++
 notmuch-show.c | 18 +++++++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/mime-node.c b/mime-node.c
index 24d73afa..f27aeb9a 100644
--- a/mime-node.c
+++ b/mime-node.c
@@ -317,7 +317,10 @@ mime_node_child (mime_node_t *parent, int child)
 	INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
 			g_type_name (G_OBJECT_TYPE (parent->part)));
     }
+
     node = _mime_node_create (parent, sub);
+    if (!node)
+	return NULL;
 
     if (child == parent->next_child && parent->next_part_num != -1) {
 	/* We're traversing in depth-first order.  Record the child's
@@ -354,6 +357,8 @@ _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
     *n -= 1;
     for (i = 0; i < node->nchildren; i++) {
 	mime_node_t *child = mime_node_child (node, i);
+	if (!child)
+	    return NULL;
 	mime_node_t *ret = _mime_node_seek_dfs_walk (child, n);
 	if (ret)
 	    return ret;
diff --git a/notmuch-show.c b/notmuch-show.c
index cdcc2a98..2c591e37 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -567,8 +567,11 @@ format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
 	}
     }
 
-    for (i = 0; i < node->nchildren; i++)
-	format_part_text (ctx, sp, mime_node_child (node, i), indent, params);
+    for (i = 0; i < node->nchildren; i++) {
+	mime_node_t * child = mime_node_child (node, i);
+	if (child)
+	    format_part_text (ctx, sp, child, indent, params);
+    }
 
     if (GMIME_IS_MESSAGE (node->part))
 	g_mime_stream_printf (stream, "\fbody}\n");
@@ -619,7 +622,9 @@ format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node,
 	if (output_body) {
 	    sp->map_key (sp, "body");
 	    sp->begin_list (sp);
-	    format_part_sprinter (ctx, sp, mime_node_child (node, 0), TRUE, include_html);
+	    mime_node_t * child = mime_node_child (node, 0);
+	    if (child)
+		format_part_sprinter (ctx, sp, child, TRUE, include_html);
 	    sp->end (sp);
 	}
 	sp->end (sp);
@@ -720,8 +725,11 @@ format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node,
 	nclose = 3;
     }
 
-    for (i = 0; i < node->nchildren; i++)
-	format_part_sprinter (ctx, sp, mime_node_child (node, i), TRUE, include_html);
+    for (i = 0; i < node->nchildren; i++) {
+	mime_node_t * child = mime_node_child (node, i);
+	if (child)
+	    format_part_sprinter (ctx, sp, child, TRUE, include_html);
+    }
 
     /* Close content structures */
     for (i = 0; i < nclose; i++)
-- 
2.14.1



More information about the notmuch mailing list