Add safe_fopen safe_fclose to check realpath and remove useless code 65/170165/5
authorJiyong Min <jiyong.min@samsung.com>
Wed, 14 Feb 2018 02:02:19 +0000 (11:02 +0900)
committerJiyong Min <jiyong.min@samsung.com>
Mon, 19 Feb 2018 04:30:32 +0000 (13:30 +0900)
Change-Id: Ib1d743adcb196e77d6b9409e2572d6f54762aef4

bmp/CMakeLists.txt
bmp/mm_util_bmp.c
common/include/mm_util_private.h
common/mm_util_private.c [new file with mode: 0755]
jpeg/mm_util_jpeg.c
packaging/libmm-utility.spec
png/CMakeLists.txt
png/mm_util_png.c

index 6b01e6e..27e0aff 100755 (executable)
@@ -39,7 +39,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=${LIB_INSTALL_DIR}")
 
 aux_source_directory(. SOURCES)
 ADD_LIBRARY(${fw_name} SHARED ${SOURCES})
-TARGET_LINK_LIBRARIES(${fw_name} ${${fw_name}_LDFLAGS})
+TARGET_LINK_LIBRARIES(${fw_name} ${${fw_name}_LDFLAGS} mmutil_common)
 SET_TARGET_PROPERTIES(${fw_name}
        PROPERTIES
        VERSION ${VERSION}
index 64d2ed5..cffb2b5 100755 (executable)
@@ -44,15 +44,14 @@ static unsigned char *__load_file(const char *path, size_t * data_size)
        size_t size;
        size_t n;
 
-       fd = fopen(path, "rb");
-       if (!fd) {
-               mm_util_error("file open failed");
+       if (MM_UTIL_ERROR_NONE != mm_util_safe_fopen(path, "rb", &fd)) {
+               mm_util_error("mm_util_safe_fopen failed");
                return NULL;
        }
 
        if (stat(path, &sb)) {
                mm_util_error("file stat failed");
-               fclose(fd);
+               mm_util_safe_fclose(fd);
                return NULL;
        }
        size = sb.st_size;
@@ -60,7 +59,7 @@ static unsigned char *__load_file(const char *path, size_t * data_size)
        buffer = calloc(1, size);
        if (!buffer) {
                mm_util_error("Unable to allocate %lld bytes", (long long)size);
-               fclose(fd);
+               mm_util_safe_fclose(fd);
                return NULL;
        }
 
@@ -68,11 +67,11 @@ static unsigned char *__load_file(const char *path, size_t * data_size)
        if (n != size) {
                mm_util_error("file read failed");
                MMUTIL_SAFE_FREE(buffer);
-               fclose(fd);
+               mm_util_safe_fclose(fd);
                return NULL;
        }
 
-       fclose(fd);
+       mm_util_safe_fclose(fd);
        *data_size = size;
        return buffer;
 }
index d6b1de7..1056926 100755 (executable)
@@ -23,6 +23,7 @@
 #define __MM_UTIL_PRIVATE_H__
 
 #include <glib.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include "mm_util_debug.h"
 #include "mm_util_type.h"
@@ -46,6 +47,9 @@ typedef struct {
        size_t size;
 } color_image_data_s;
 
+int mm_util_safe_fopen(const char *path, const char *mode, FILE **fp);
+void mm_util_safe_fclose(FILE *fp);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/common/mm_util_private.c b/common/mm_util_private.c
new file mode 100755 (executable)
index 0000000..b1085d6
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * libmm-utility
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: YoungHun Kim <yh8004.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <stdio.h>
+
+#include "mm_util_private.h"
+
+int mm_util_safe_fopen(const char *path, const char *mode, FILE **fp)
+{
+       char *_realpath = NULL;
+
+       /* The input path was wrong, it returned no such file. */
+       mm_util_retvm_if((!MMUTIL_STRING_VALID(path)), MM_UTIL_ERROR_NO_SUCH_FILE, "invalid path");
+       mm_util_retvm_if((!MMUTIL_STRING_VALID(mode)), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid mode");
+       mm_util_retvm_if((fp == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid FILE*");
+
+       /*
+        * If the mode has writing mode('w', 'w+', 'r+', 'a', 'a+'),
+        * the symbolic link should be checked with realpath.
+        */
+       if ((g_strrstr(mode, "w") != NULL) || (g_strrstr(mode, "r+") != NULL) || (g_strrstr(mode, "a") != NULL)) {
+               if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
+                       _realpath = realpath(path, NULL);
+                       if (_realpath == NULL) {
+                               mm_util_sec_debug("realpath failed [%s]", path);
+                               mm_util_stderror("realpath failed");
+                               return MM_UTIL_ERROR_NO_SUCH_FILE;
+                       }
+                       if (strncmp(path, _realpath, strlen(path))) {
+                               mm_util_error("file is symbolic link");
+                               MMUTIL_SAFE_FREE(_realpath);
+                               return MM_UTIL_ERROR_NO_SUCH_FILE;
+                       }
+
+                       MMUTIL_SAFE_FREE(_realpath);
+               } else {
+                       mm_util_sec_debug("file(%s) will be created", path);
+               }
+       }
+
+       if ((*fp = fopen(path, mode)) == NULL) {
+               mm_util_sec_debug("fopen failed [%s]", path);
+               mm_util_stderror("file open failed");
+               return MM_UTIL_ERROR_NO_SUCH_FILE;
+       }
+
+       return MM_UTIL_ERROR_NONE;
+}
+
+void mm_util_safe_fclose(FILE *fp)
+{
+       if (fp)
+               fclose(fp);
+}
index 6e75076..4f4cf2b 100755 (executable)
@@ -97,19 +97,6 @@ static void __initBuf(unsigned char *buf, int w, int h, int pf, int flags)
        }
 }
 
-#if 0
-static void mm_image_codec_writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, char *filename)
-{
-       FILE *file = fopen(filename, "wb");
-       if (!file || fwrite(jpegBuf, jpegSize, 1, file) != 1) {
-               mm_util_error("ERROR: Could not write to %s", filename);
-               mm_util_stderror("ERROR: Could not write");
-       }
-
-       if (file) fclose(file);
-}
-#endif
-
 static void __mm_decode_libjpeg_turbo_decompress(tjhandle handle, unsigned char *jpegBuf, unsigned long jpegSize, int TD_BU, mm_util_jpeg_yuv_data* decoded_data, mm_util_color_format_e input_fmt)
 {
        int _hdrw = 0, _hdrh = 0, _hdrsubsamp = -1;
@@ -215,24 +202,23 @@ static int __mm_image_decode_from_jpeg_file_with_libjpeg_turbo(mm_util_jpeg_yuv_
        int TD_BU = 0;
        size_t read_size = 0;
 
-       void *src = fopen(pFileName, "rb");
-       if (src == NULL) {
-               mm_util_error("Error [%s] failed", pFileName);
-               mm_util_stderror("Error file open failed");
-               return MM_UTIL_ERROR_NO_SUCH_FILE;
+       iErrorCode = mm_util_safe_fopen(pFileName, "rb", &src);
+       if (iErrorCode == MM_UTIL_ERROR_NONE) {
+               mm_util_error("mm_util_safe_fopen failed (%d)", iErrorCode);
+               return iErrorCode;
        }
        fseek(src, 0, SEEK_END);
        jpegSize = ftell(src);
        rewind(src);
 
        if ((dhandle = tjInitDecompress()) == NULL) {
-               fclose(src);
+               mm_util_safe_fclose(src);
                mm_util_error("dhandle=tjInitDecompress()) is NULL");
                return MM_UTIL_ERROR_INVALID_PARAMETER;
        }
        srcBuf = (unsigned char *) calloc(1, sizeof(char) * jpegSize);
        if (srcBuf == NULL) {
-               fclose(src);
+               mm_util_safe_fclose(src);
                tjDestroy(dhandle);
                mm_util_error("srcBuf is NULL");
                return MM_UTIL_ERROR_OUT_OF_MEMORY;
@@ -240,7 +226,7 @@ static int __mm_image_decode_from_jpeg_file_with_libjpeg_turbo(mm_util_jpeg_yuv_
 
        read_size = fread(srcBuf, 1, jpegSize, src);
        if (read_size <= 0) {
-               fclose(src);
+               mm_util_safe_fclose(src);
                tjDestroy(dhandle);
                tjFree(srcBuf);
                mm_util_error("Read from src failed");
@@ -249,7 +235,7 @@ static int __mm_image_decode_from_jpeg_file_with_libjpeg_turbo(mm_util_jpeg_yuv_
 
        mm_util_debug("srcBuf[0]: 0x%2x, srcBuf[1]: 0x%2x, jpegSize:%d", srcBuf[0], srcBuf[1], jpegSize);
        __mm_decode_libjpeg_turbo_decompress(dhandle, srcBuf, jpegSize, TD_BU, decoded_data, input_fmt);
-       fclose(src);
+       mm_util_safe_fclose(src);
        mm_util_debug("fclose");
 
        if (dhandle)
@@ -273,7 +259,7 @@ static int __mm_image_decode_from_jpeg_memory_with_libjpeg_turbo(mm_util_jpeg_yu
        }
        srcBuf = (unsigned char *) calloc(1, sizeof(char) * size);
        if (srcBuf == NULL) {
-               fclose(src);
+               mm_util_safe_fclose(src);
                tjDestroy(dhandle);
                mm_util_error("srcBuf is NULL");
                return MM_UTIL_ERROR_OUT_OF_MEMORY;
@@ -343,23 +329,24 @@ static int __mm_image_encode_to_jpeg_file_with_libjpeg_turbo(const char *filenam
        unsigned char *dstBuf = NULL;
        unsigned long size = 0;
        int TD_BU = 0;
-       FILE *fout = fopen(filename, "w+");
-       if (fout == NULL) {
-               mm_util_error("FILE OPEN FAIL [%s] failed", filename);
-               mm_util_stderror("FILE OPEN FAIL");
-               return MM_UTIL_ERROR_NO_SUCH_FILE;
+       FILE *fout = NULL;
+
+       iErrorCode = mm_util_safe_fopen(filename, "w+", &fout);
+       if (iErrorCode != MM_UTIL_ERROR_NONE) {
+               mm_util_error("mm_util_safe_fopen failed (%d)", iErrorCode);
+               return iErrorCode;
        }
        mm_util_debug("fmt: %d", fmt);
 
        size = TJBUFSIZE(width, height);
        if ((dstBuf = (unsigned char *)tjAlloc(size)) == NULL) {
-               fclose(fout);
+               mm_util_safe_fclose(fout);
                mm_util_error("dstBuf is NULL");
                return MM_UTIL_ERROR_OUT_OF_MEMORY;
        }
 
        if ((chandle = tjInitCompress()) == NULL) {
-               fclose(fout);
+               mm_util_safe_fclose(fout);
                tjFree(dstBuf);
                mm_util_error("dstBuf is NULL");
                return MM_UTIL_ERROR_INVALID_PARAMETER;
@@ -367,7 +354,7 @@ static int __mm_image_encode_to_jpeg_file_with_libjpeg_turbo(const char *filenam
        _mm_encode_libjpeg_turbo_compress(chandle, src, &dstBuf, &size, width, height, quality, TD_BU, fmt);
        mm_util_debug("dstBuf: %p\t size: %u", dstBuf, size);
        fwrite(dstBuf, 1, size, fout);
-       fclose(fout);
+       mm_util_safe_fclose(fout);
        if (chandle)
                tjDestroy(chandle);
        if (dstBuf)
@@ -455,7 +442,6 @@ static void __my_error_exit(j_common_ptr cinfo)
 static int __mm_image_encode_to_jpeg_file_with_libjpeg(const char *pFileName, void *rawdata, int width, int height, mm_util_color_format_e fmt, int quality)
 {
        int iErrorCode = MM_UTIL_ERROR_NONE;
-       char *realPATH = NULL;
 
        struct jpeg_compress_struct cinfo;
        struct jpeg_error_mgr jerr;
@@ -464,10 +450,8 @@ static int __mm_image_encode_to_jpeg_file_with_libjpeg(const char *pFileName, vo
 
        JSAMPROW y[16], cb[16], cr[16]; /* y[2][5] = color sample of row 2 and pixel column 5; (one plane) */
        JSAMPARRAY data[3]; /* t[0][2][5] = color sample 0 of row 2 and column 5 */
-       if (!pFileName) {
-               mm_util_error("pFileName");
-               return MM_UTIL_ERROR_NO_SUCH_FILE;
-       }
+
+       mm_util_retvm_if(!MMUTIL_STRING_VALID(pFileName), MM_UTIL_ERROR_NO_SUCH_FILE, "invalid path");
 
        data[0] = y;
        data[1] = cb;
@@ -479,33 +463,15 @@ static int __mm_image_encode_to_jpeg_file_with_libjpeg(const char *pFileName, vo
 
        jpeg_create_compress(&cinfo);
 
-       if (g_file_test(pFileName, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
-               realPATH = realpath(pFileName, NULL);
-               if (realPATH == NULL) {
-                       mm_util_error("realpath failed [%s]", pFileName);
-                       mm_util_stderror("realpath failed");
-                       return MM_UTIL_ERROR_NO_SUCH_FILE;
-               }
-               if (!strncmp(pFileName, realPATH, strlen(pFileName))) {
-                       if ((fpWriter = fopen(pFileName, "wb")) == NULL) {
-                               mm_util_error("[infile] file open [%s] failed", pFileName);
-                               mm_util_stderror("file open failed");
-                               MMUTIL_SAFE_FREE(realPATH);
-                               return MM_UTIL_ERROR_INVALID_OPERATION;
-                       }
-               } else {
-                       mm_util_error("[infile] file [%s] is symlink", pFileName);
-                       MMUTIL_SAFE_FREE(realPATH);
-                       return MM_UTIL_ERROR_NO_SUCH_FILE;
-               }
+       iErrorCode = mm_util_safe_fopen(pFileName, "wb", &fpWriter);
+       if (iErrorCode != MM_UTIL_ERROR_NONE) {
+               mm_util_error("mm_util_safe_fopen failed (%d)", iErrorCode);
+               jpeg_finish_compress(&cinfo);
+               mm_util_debug("jpeg_finish_compress");
 
-               MMUTIL_SAFE_FREE(realPATH);
-       } else {
-               if ((fpWriter = fopen(pFileName, "wb")) == NULL) {
-                       mm_util_error("[infile] file open [%s] failed", pFileName);
-                       mm_util_stderror("file open failed");
-                       return MM_UTIL_ERROR_INVALID_OPERATION;
-               }
+               jpeg_destroy_compress(&cinfo);
+               mm_util_debug("jpeg_destroy_compress");
+               return iErrorCode;
        }
 
        jpeg_stdio_dest(&cinfo, fpWriter);
@@ -551,7 +517,7 @@ static int __mm_image_encode_to_jpeg_file_with_libjpeg(const char *pFileName, vo
                                memset(large_rect, 0x10, width);
                        } else {
                                MMUTIL_SAFE_FREE(small_rect);
-                               fclose(fpWriter);
+                               mm_util_safe_fclose(fpWriter);
                                mm_util_error("large rectangle memory");
                                return MM_UTIL_ERROR_INVALID_PARAMETER;
                        }
@@ -559,7 +525,7 @@ static int __mm_image_encode_to_jpeg_file_with_libjpeg(const char *pFileName, vo
                                memset(small_rect, 0x80, width);
                        } else {
                                MMUTIL_SAFE_FREE(large_rect);
-                               fclose(fpWriter);
+                               mm_util_safe_fclose(fpWriter);
                                mm_util_error("small rectangle memory");
                                return MM_UTIL_ERROR_INVALID_PARAMETER;
                        }
@@ -664,12 +630,12 @@ static int __mm_image_encode_to_jpeg_file_with_libjpeg(const char *pFileName, vo
                mm_util_debug("jpeg_destroy_compress");
        } else {
                mm_util_error("We can't encode the IMAGE format");
-               fclose(fpWriter);
+               mm_util_safe_fclose(fpWriter);
                return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
        }
        fsync((int)(fpWriter->_fileno));
        mm_util_debug("[fsync] FILE");
-       fclose(fpWriter);
+       mm_util_safe_fclose(fpWriter);
        return iErrorCode;
 }
 
@@ -879,10 +845,8 @@ static int __mm_image_decode_from_jpeg_file_with_libjpeg(mm_util_jpeg_yuv_data *
 
        mm_util_fenter();
 
-       if (!pFileName) {
-               mm_util_error("pFileName");
-               return MM_UTIL_ERROR_NO_SUCH_FILE;
-       }
+       mm_util_retvm_if(!MMUTIL_STRING_VALID(pFileName), MM_UTIL_ERROR_NO_SUCH_FILE, "invalid path");
+
        if (decoded_data == NULL) {
                mm_util_error("decoded_data");
                return MM_UTIL_ERROR_INVALID_PARAMETER;
@@ -890,12 +854,12 @@ static int __mm_image_decode_from_jpeg_file_with_libjpeg(mm_util_jpeg_yuv_data *
        if (decoded_data)
                decoded_data->data = NULL;
 
-       infile = fopen(pFileName, "rb");
-       if (infile == NULL) {
-               mm_util_error("[infile] file open [%s]", pFileName);
-               mm_util_stderror("file open failed");
-               return MM_UTIL_ERROR_NO_SUCH_FILE;
+       iErrorCode = mm_util_safe_fopen(pFileName, "rb", &infile);
+       if (iErrorCode != MM_UTIL_ERROR_NONE) {
+               mm_util_error("mm_util_safe_fopen failed (%d)", iErrorCode);
+               return iErrorCode;
        }
+
        mm_util_debug("infile");
        /* allocate and initialize JPEG decompression object   We set up the normal JPEG error routines, then override error_exit. */
        dinfo.err = jpeg_std_error(&jerr.pub);
@@ -909,7 +873,7 @@ static int __mm_image_decode_from_jpeg_file_with_libjpeg(mm_util_jpeg_yuv_data *
                mm_util_debug("setjmp");
                jpeg_destroy_decompress(&dinfo);
                MMUTIL_SAFE_FREE(decoded_data->data);
-               fclose(infile);
+               mm_util_safe_fclose(infile);
                mm_util_fleave();
                return MM_UTIL_ERROR_INVALID_PARAMETER;
        }
@@ -996,7 +960,7 @@ static int __mm_image_decode_from_jpeg_file_with_libjpeg(mm_util_jpeg_yuv_data *
                jpeg_finish_decompress(&dinfo);
                jpeg_destroy_decompress(&dinfo);
                mm_util_error("[%d] We can't decode the IMAGE format", input_fmt);
-               fclose(infile);
+               mm_util_safe_fclose(infile);
                return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
        }
 
@@ -1007,7 +971,7 @@ static int __mm_image_decode_from_jpeg_file_with_libjpeg(mm_util_jpeg_yuv_data *
                jpeg_finish_decompress(&dinfo);
                jpeg_destroy_decompress(&dinfo);
                mm_util_error("decoded_data->data is NULL");
-               fclose(infile);
+               mm_util_safe_fclose(infile);
                return MM_UTIL_ERROR_OUT_OF_MEMORY;
        }
        mm_util_debug("decoded_data->data");
@@ -1052,7 +1016,7 @@ static int __mm_image_decode_from_jpeg_file_with_libjpeg(mm_util_jpeg_yuv_data *
        /* Release JPEG decompression object */
        jpeg_destroy_decompress(&dinfo);
 
-       fclose(infile);
+       mm_util_safe_fclose(infile);
 
        mm_util_fleave();
 
@@ -1369,7 +1333,13 @@ int mm_util_decode_from_jpeg_file(mm_util_jpeg_yuv_data *decoded, const char *fi
                return MM_UTIL_ERROR_INVALID_PARAMETER;
        }
 
-       FILE *fp = fopen(filename, "rb");
+       FILE *fp = NULL;
+       ret = mm_util_safe_fopen(filename, "rb", &fp);
+       if (ret != MM_UTIL_ERROR_NONE) {
+               mm_util_error("mm_util_safe_fopen failed (%d)", ret);
+               return ret;
+       }
+
        unsigned char magic[2] = {0};
        size_t read_size = 0;
        if (fp) {
@@ -1378,7 +1348,7 @@ int mm_util_decode_from_jpeg_file(mm_util_jpeg_yuv_data *decoded, const char *fi
                        mm_util_debug("Success fread");
 
                mm_util_debug("%x %x", magic[0], magic[1]);
-               fclose(fp);
+               mm_util_safe_fclose(fp);
        } else {
                mm_util_error("[infile] file open [%s]", filename);
                mm_util_stderror("file open failed");
@@ -1526,7 +1496,13 @@ int mm_util_decode_from_jpeg_file_with_downscale(mm_util_jpeg_yuv_data *decoded,
                return MM_UTIL_ERROR_INVALID_PARAMETER;
        }
 
-       FILE *fp = fopen(filename, "rb");
+       FILE *fp = NULL;
+       ret = mm_util_safe_fopen(filename, "rb", &fp);
+       if (ret != MM_UTIL_ERROR_NONE) {
+               mm_util_error("mm_util_safe_fopen failed (%d)", ret);
+               return ret;
+       }
+
        unsigned char magic[2] = {0};
        size_t read_size = 0;
        if (fp) {
@@ -1535,7 +1511,7 @@ int mm_util_decode_from_jpeg_file_with_downscale(mm_util_jpeg_yuv_data *decoded,
                        mm_util_debug("Success fread");
 
                mm_util_debug("%x %x", magic[0], magic[1]);
-               fclose(fp);
+               mm_util_safe_fclose(fp);
        } else {
                mm_util_error("[infile] file open [%s]", filename);
                mm_util_stderror("file open failed");
index c29832b..87e9cc9 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       libmm-utility
 Summary:    Multimedia Framework Utility Library
-Version:    0.1.12
+Version:    0.1.13
 Release:    0
 Group:      System/Libraries
 License:    Apache-2.0
index 74f4c88..1a1288b 100755 (executable)
@@ -39,7 +39,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=${LIB_INSTALL_DIR}")
 
 aux_source_directory(. SOURCES)
 ADD_LIBRARY(${fw_name} SHARED ${SOURCES})
-TARGET_LINK_LIBRARIES(${fw_name} ${${fw_name}_LDFLAGS})
+TARGET_LINK_LIBRARIES(${fw_name} ${${fw_name}_LDFLAGS} mmutil_common)
 SET_TARGET_PROPERTIES(${fw_name}
        PROPERTIES
        VERSION ${VERSION}
index 3a0b57a..8d3d43a 100755 (executable)
@@ -126,16 +126,14 @@ static int __read_png(mm_util_png_data *decoded, FILE * fp, void *memory)
 
        if (png_ptr == NULL) {
                mm_util_error("could not create read struct");
-               if (fp)
-                       fclose(fp);
+               mm_util_safe_fclose(fp);
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
 
        info_ptr = png_create_info_struct(png_ptr);
        if (info_ptr == NULL) {
                mm_util_error("could not create info struct");
-               if (fp)
-                       fclose(fp);
+               mm_util_safe_fclose(fp);
                png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
@@ -148,8 +146,7 @@ static int __read_png(mm_util_png_data *decoded, FILE * fp, void *memory)
                        mm_util_debug("free data");
                }
                png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
-               if (fp)
-                       fclose(fp);
+               mm_util_safe_fclose(fp);
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
 
@@ -184,8 +181,7 @@ static int __read_png(mm_util_png_data *decoded, FILE * fp, void *memory)
 
        png_read_end(png_ptr, info_ptr);
        png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
-       if (fp)
-               fclose(fp);
+       mm_util_safe_fclose(fp);
 
        return MM_UTIL_ERROR_NONE;
 }
@@ -230,16 +226,14 @@ static int __read_png_progressive(mm_util_png_data *decoded, FILE * fp, void *me
 
        if (png_ptr == NULL) {
                mm_util_error("could not create read struct");
-               if (fp)
-                       fclose(fp);
+               mm_util_safe_fclose(fp);
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
 
        info_ptr = png_create_info_struct(png_ptr);
        if (info_ptr == NULL) {
                mm_util_error("could not create info struct");
-               if (fp)
-                       fclose(fp);
+               mm_util_safe_fclose(fp);
                png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
@@ -252,8 +246,7 @@ static int __read_png_progressive(mm_util_png_data *decoded, FILE * fp, void *me
                        mm_util_debug("free data");
                }
                png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
-               if (fp)
-                       fclose(fp);
+               mm_util_safe_fclose(fp);
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
 
@@ -285,8 +278,7 @@ static int __read_png_progressive(mm_util_png_data *decoded, FILE * fp, void *me
        };
 
        png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
-       if (fp)
-               fclose(fp);
+       mm_util_safe_fclose(fp);
 
        return MM_UTIL_ERROR_NONE;
 }
@@ -296,9 +288,11 @@ int mm_util_decode_from_png_file(mm_util_png_data *decoded, const char *fpath)
        int ret = MM_UTIL_ERROR_NONE;
        FILE *fp;
 
-       mm_util_debug("mm_util_decode_from_png");
-       if ((fp = fopen(fpath, "r")) == NULL)
-               return MM_UTIL_ERROR_NO_SUCH_FILE;
+       ret = mm_util_safe_fopen(fpath, "r", &fp);
+       if (ret != MM_UTIL_ERROR_NONE) {
+               mm_util_error("mm_util_safe_fopen failed (%d)", ret);
+               return ret;
+       }
 
        if (fp) {
                if (decoded->png.progressive)
@@ -363,8 +357,7 @@ int write_png(void **data, mm_util_png_data *encoded, FILE *fp)
 
        if (png_ptr == NULL) {
                mm_util_error("could not create write struct");
-               if (fp)
-                       fclose(fp);
+               mm_util_safe_fclose(fp);
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
 
@@ -372,8 +365,7 @@ int write_png(void **data, mm_util_png_data *encoded, FILE *fp)
        if (info_ptr == NULL) {
                mm_util_error("could not create info struct");
                png_destroy_write_struct(&png_ptr, png_infopp_NULL);
-               if (fp)
-                       fclose(fp);
+               mm_util_safe_fclose(fp);
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
 
@@ -387,8 +379,7 @@ int write_png(void **data, mm_util_png_data *encoded, FILE *fp)
                if (row_pointers)
                        png_free(png_ptr, row_pointers);
                png_destroy_write_struct(&png_ptr, &info_ptr);
-               if (fp)
-                       fclose(fp);
+               mm_util_safe_fclose(fp);
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
 
@@ -419,8 +410,7 @@ int write_png(void **data, mm_util_png_data *encoded, FILE *fp)
 
        png_free(png_ptr, row_pointers);
        png_destroy_write_struct(&png_ptr, &info_ptr);
-       if (fp)
-               fclose(fp);
+       mm_util_safe_fclose(fp);
 
        return MM_UTIL_ERROR_NONE;
 }
@@ -428,42 +418,16 @@ int write_png(void **data, mm_util_png_data *encoded, FILE *fp)
 int mm_util_encode_to_png_file(void **data, mm_util_png_data *encoded, const char *fpath)
 {
        int ret = MM_UTIL_ERROR_NONE;
-       char *realPATH = NULL;
        FILE *fp;
 
        mm_util_debug("mm_util_encode_to_png");
 
-       if (fpath == NULL) {
-               mm_util_error("Invalid parameter");
-               return MM_UTIL_ERROR_NO_SUCH_FILE;
-       }
-
-       if (g_file_test(fpath, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
-               realPATH = realpath(fpath, NULL);
-               if (realPATH == NULL) {
-                       mm_util_error("realpath failed [%s]", fpath);
-                       mm_util_stderror("realpath failed");
-                       return MM_UTIL_ERROR_NO_SUCH_FILE;
-               }
+       mm_util_retvm_if(!MMUTIL_STRING_VALID(fpath), MM_UTIL_ERROR_NO_SUCH_FILE, "invalid path");
 
-               if (!strncmp(fpath, realPATH, strlen(fpath))) {
-                       if ((fp = fopen(fpath, "w")) == NULL) {
-                               mm_util_stderror("file open failed");
-                               MMUTIL_SAFE_FREE(realPATH);
-                               return MM_UTIL_ERROR_INVALID_OPERATION;
-                       }
-               } else {
-                       mm_util_error("file is symbolic link");
-                       MMUTIL_SAFE_FREE(realPATH);
-                       return MM_UTIL_ERROR_NO_SUCH_FILE;
-               }
-               MMUTIL_SAFE_FREE(realPATH);
-       } else {
-               if ((fp = fopen(fpath, "w")) == NULL) {
-                       mm_util_error("[infile] file open [%s] failed", fpath);
-                       mm_util_stderror("file open failed");
-                       return MM_UTIL_ERROR_INVALID_OPERATION;
-               }
+       ret = mm_util_safe_fopen(fpath, "w", &fp);
+       if (ret != MM_UTIL_ERROR_NONE) {
+               mm_util_error("mm_util_safe_fopen failed (%d)", ret);
+               return ret;
        }
 
        ret = write_png(data, encoded, fp);