rimage: Fix some memory leak error

Handle pointers and memory when error happens.

Signed-off-by: Pan Xiuli <xiuli.pan@linux.intel.com>
diff --git a/rimage/rimage.c b/rimage/rimage.c
index af57f5a..af4a7d0 100644
--- a/rimage/rimage.c
+++ b/rimage/rimage.c
@@ -208,10 +208,11 @@
 		goto out;
 	}
 
-	free(image->prg);
-	free(image->section);
-
 out:
+	if (image->prg)
+		free(image->prg);
+	if (image->section)
+		free(image->section);
 	return ret;
 }
 
@@ -283,6 +284,8 @@
 	if (image.in_fd == NULL) {
 		fprintf(stderr, "error: unable to open %s for reading %d\n",
 			image.in_file, errno);
+		ret = -EINVAL;
+		goto out;
 	}
 
 	/* open outfile for writing */
@@ -291,14 +294,19 @@
 	if (image.out_fd == NULL) {
 		fprintf(stderr, "error: unable to open %s for writing %d\n",
 			image.out_file, errno);
+		ret = -EINVAL;
+		goto out;
 	}
 
 	/* write data */
 	ret = write_elf_data(&image);
 
+out:
 	/* close files */
-	fclose(image.out_fd);
-	fclose(image.in_fd);
+	if (image.in_fd)
+		fclose(image.in_fd);
+	if (image.out_fd)
+		fclose(image.out_fd);
 
 	return ret;
 }