From cd1ab8d1a2bb253169731ab0ef960ee9b64a1f4e Mon Sep 17 00:00:00 2001
From: Tommy Olofsson <tommy.olofsson.90@gmail.com>
Date: Mon, 20 Jun 2016 13:37:52 +0200
Subject: [PATCH] Try to catch memory errors.

---
 lib/c/2014/labcomm2014_decoder.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/lib/c/2014/labcomm2014_decoder.c b/lib/c/2014/labcomm2014_decoder.c
index fe534f3..2a43982 100644
--- a/lib/c/2014/labcomm2014_decoder.c
+++ b/lib/c/2014/labcomm2014_decoder.c
@@ -158,6 +158,10 @@ static int decode_sample_def_or_ref(struct labcomm2014_decoder *d, int kind)
     goto out;
   }
   signature.name = labcomm2014_read_string(d->reader);
+  if (signature.name == NULL) {
+    result = -ENOMEM;
+    goto out;
+  }
   if (d->reader->error < 0) {
     result = d->reader->error;
     goto free_signature_name;
@@ -168,6 +172,10 @@ static int decode_sample_def_or_ref(struct labcomm2014_decoder *d, int kind)
     goto free_signature_name;
   }
   signature.signature = labcomm2014_memory_alloc(d->memory, 1,  signature.size);
+  if (signature.signature == NULL) {
+    result = -ENOMEM;
+    goto free_signature_name;
+  }
   if (d->reader->error < 0) {
     result = d->reader->error;
     goto free_signature_name;
@@ -253,6 +261,10 @@ static int decode_pragma(struct labcomm2014_decoder *d,
   char *pragma_type;
   int result;
   pragma_type = labcomm2014_read_string(d->reader);
+  if (pragma_type == NULL) {
+    result = -ENOMEM;
+    goto out;
+  }
   if (d->reader->error < 0) {
     result = d->reader->error;
     goto out;
@@ -346,7 +358,9 @@ static int do_decode_one(struct labcomm2014_decoder *d)
   }
   if (remote_index == LABCOMM_VERSION) {
     char *version = labcomm2014_read_string(d->reader);
-    if (d->reader->error < 0) {
+    if (version == NULL) {
+      result = -ENOMEM;
+    } else if (d->reader->error < 0) {
       result = d->reader->error;
     } else if (strcmp(version, CURRENT_VERSION) == 0) {
       result = LABCOMM_VERSION;
@@ -488,6 +502,7 @@ static void decode_raw_type_def(
   v.index = labcomm2014_read_packed32(r);
   if (r->error < 0) { goto out; }
   v.name  = labcomm2014_read_string(r);
+  if (v.name == NULL) { goto out; }
   if (r->error < 0) { goto free_name; }
   v.length = labcomm2014_read_packed32(r);
   if (r->error < 0) { goto free_name; }
-- 
GitLab