From 41e86e4d51c3bfd585456537eb5e1ccb1c4aaf91 Mon Sep 17 00:00:00 2001
From: Mikhail Lappo <mikhail.lappo@esrlabs.com>
Date: Wed, 22 Mar 2017 22:14:24 +0100
Subject: [PATCH] Possible null-pointer dereference

In unit test if_fail check for nullptr
is always located after dereferencing this
null pointer. This patch introduces correct
order: first check, then use
---
 test/unit/mdns/test_mdns.c | 42 +++++++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 21 deletions(-)

diff --git a/test/unit/mdns/test_mdns.c b/test/unit/mdns/test_mdns.c
index ca9be64e..63851637 100644
--- a/test/unit/mdns/test_mdns.c
+++ b/test/unit/mdns/test_mdns.c
@@ -45,8 +45,8 @@ START_TEST(readname_basic)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
   offset = mdns_readname(p, 0, &domain);
   pbuf_free(p);
   fail_unless(offset == sizeof(data));
@@ -64,8 +64,8 @@ START_TEST(readname_anydata)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
   offset = mdns_readname(p, 0, &domain);
   pbuf_free(p);
   fail_unless(offset == sizeof(data));
@@ -83,8 +83,8 @@ START_TEST(readname_short_buf)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
   offset = mdns_readname(p, 0, &domain);
   pbuf_free(p);
   fail_unless(offset == MDNS_READNAME_ERROR);
@@ -108,8 +108,8 @@ START_TEST(readname_long_label)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
   offset = mdns_readname(p, 0, &domain);
   pbuf_free(p);
   fail_unless(offset == MDNS_READNAME_ERROR);
@@ -164,8 +164,8 @@ START_TEST(readname_overflow)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
   offset = mdns_readname(p, 0, &domain);
   pbuf_free(p);
   fail_unless(offset == MDNS_READNAME_ERROR);
@@ -189,8 +189,8 @@ START_TEST(readname_jump_earlier)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
   offset = mdns_readname(p, 20, &domain);
   pbuf_free(p);
   fail_unless(offset == sizeof(data));
@@ -218,8 +218,8 @@ START_TEST(readname_jump_earlier_jump)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
   offset = mdns_readname(p, 0x18, &domain);
   pbuf_free(p);
   fail_unless(offset == sizeof(data));
@@ -252,8 +252,8 @@ START_TEST(readname_jump_maxdepth)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
   offset = mdns_readname(p, 0x30, &domain);
   pbuf_free(p);
   fail_unless(offset == sizeof(data));
@@ -278,8 +278,8 @@ START_TEST(readname_jump_later)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
   offset = mdns_readname(p, 0, &domain);
   pbuf_free(p);
   fail_unless(offset == 13);
@@ -300,8 +300,8 @@ START_TEST(readname_half_jump)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
   offset = mdns_readname(p, 0, &domain);
   pbuf_free(p);
   fail_unless(offset == MDNS_READNAME_ERROR);
@@ -319,8 +319,8 @@ START_TEST(readname_jump_toolong)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
   offset = mdns_readname(p, 0, &domain);
   pbuf_free(p);
   fail_unless(offset == MDNS_READNAME_ERROR);
@@ -339,8 +339,8 @@ START_TEST(readname_jump_loop_label)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
   offset = mdns_readname(p, 10, &domain);
   pbuf_free(p);
   fail_unless(offset == MDNS_READNAME_ERROR);
@@ -359,8 +359,8 @@ START_TEST(readname_jump_loop_jump)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
   offset = mdns_readname(p, 10, &domain);
   pbuf_free(p);
   fail_unless(offset == MDNS_READNAME_ERROR);
@@ -603,8 +603,8 @@ START_TEST(compress_full_match)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
 
   memset(&domain, 0, sizeof(domain));
   res = mdns_domain_add_label(&domain, "foobar", 6);
@@ -638,8 +638,8 @@ START_TEST(compress_full_match_subset)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
 
   memset(&domain, 0, sizeof(domain));
   res = mdns_domain_add_label(&domain, "foobar", 6);
@@ -675,8 +675,8 @@ START_TEST(compress_full_match_jump)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
 
   memset(&domain, 0, sizeof(domain));
   res = mdns_domain_add_label(&domain, "foobar", 6);
@@ -710,8 +710,8 @@ START_TEST(compress_no_match)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
 
   memset(&domain, 0, sizeof(domain));
   res = mdns_domain_add_label(&domain, "foobar", 6);
@@ -744,8 +744,8 @@ START_TEST(compress_2nd_label)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
 
   memset(&domain, 0, sizeof(domain));
   res = mdns_domain_add_label(&domain, "lwip", 4);
@@ -779,8 +779,8 @@ START_TEST(compress_2nd_label_short)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
 
   memset(&domain, 0, sizeof(domain));
   res = mdns_domain_add_label(&domain, "foobar", 6);
@@ -816,8 +816,8 @@ START_TEST(compress_jump_to_jump)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
 
   memset(&domain, 0, sizeof(domain));
   res = mdns_domain_add_label(&domain, "foobar", 6);
@@ -856,8 +856,8 @@ START_TEST(compress_long_match)
   LWIP_UNUSED_ARG(_i);
 
   p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
-  p->payload = (void *)(size_t)data;
   fail_if(p == NULL);
+  p->payload = (void *)(size_t)data;
 
   memset(&domain, 0, sizeof(domain));
   res = mdns_domain_add_label(&domain, "foobar", 6);
-- 
2.12.0

