hal: Check NULL pointer

FILE pointer could be NULL if open failed, need check it before close it. And
it is good to set closed resource to NULL.

CRs-fixed: 2249886

Change-Id: Ieb9ecb032f061f0895fcf83df5baf042b346b985
diff --git a/qahw_api/test/qahw_multi_record_test.c b/qahw_api/test/qahw_multi_record_test.c
index 2d9ee6a..ed8fe40 100644
--- a/qahw_api/test/qahw_multi_record_test.c
+++ b/qahw_api/test/qahw_multi_record_test.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright (C) 2015 The Android Open Source Project *
@@ -362,7 +362,7 @@
       pthread_exit(0);
   }
 
-  FILE *fd_in_ts;
+  FILE *fd_in_ts = NULL;
   if (params->timestamp_mode) {
       if (*(params->timestamp_file_in))
           fd_in_ts = fopen(params->timestamp_file_in, "w+");
@@ -457,8 +457,10 @@
       }
       data_sz += buffer_size;
   }
-  if (params->timestamp_mode)
+  if  ((params->timestamp_mode) && fd_in_ts) {
       fclose(fd_in_ts);
+      fd_in_ts = NULL;
+  }
 
   /*Stopping sourcetracking thread*/
   sourcetrack_done = 1;
@@ -470,15 +472,18 @@
   fwrite(&hdr, 1, sizeof(hdr), fd);
   free(buffer);
   fclose(fd);
+  fd = NULL;
 
   /* capture latency kpis if required */
   if (kpi_mode) {
       tCold = tsColdF.tv_sec*1000 - tsColdI.tv_sec*1000 +
               tsColdF.tv_nsec/1000000 - tsColdI.tv_nsec/1000000;
 
-      fread((void *) latencyBuf, 100, 1, fdLatencyNode);
-      if (fdLatencyNode)
+      if (fdLatencyNode) {
+          fread((void *) latencyBuf, 100, 1, fdLatencyNode);
           fclose(fdLatencyNode);
+          fdLatencyNode = NULL;
+      }
       sscanf(latencyBuf, " %llu,%llu", &tsec, &tusec);
       tCont = ((uint64_t)tsCont.tv_sec)*1000 - tsec*1000 + ((uint64_t)tsCont.tv_nsec)/1000000 - tusec/1000;
       if (log_file != stdout) {
@@ -910,7 +915,10 @@
     fprintf(log_file, "\n ADL: Done with hal record test \n");
     if (log_file != stdout) {
         fprintf(stdout, "\n ADL: Done with hal record test \n");
-        fclose(log_file);
+        if (log_file) {
+          fclose(log_file);
+          log_file = NULL;
+        }
     }
     wakelock_acquired = request_wake_lock(wakelock_acquired, false);
     return 0;
diff --git a/qahw_api/test/trans_loopback_test.c b/qahw_api/test/trans_loopback_test.c
index 2cb2a29..f1d897a 100644
--- a/qahw_api/test/trans_loopback_test.c
+++ b/qahw_api/test/trans_loopback_test.c
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2017, The Linux Foundation. All rights reserved.
+* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
@@ -517,6 +517,11 @@
                     j++;
                 }
 
+                if (dev_path == NULL) {
+                    fprintf(log_file, "NULL dev_path!");
+                    continue;
+                }
+
                 if ((dev_path != NULL) && (switch_name != NULL))
                     fprintf(log_file,"devpath = %s, switch_name = %s \n",dev_path, switch_name);