diff options
author | SzuWei Lin <szuweilin@google.com> | 2017-04-10 07:33:47 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-04-10 07:33:47 +0000 |
commit | d457af95a96d7b7bd1361c984bf5d2f525cbd72a (patch) | |
tree | 15b82b50d8419412e59559fb0f6c8a6e44b584d6 /tests | |
parent | a450c9d3bc2783d1cf3612e9725b313acef7b36a (diff) | |
parent | c5b29321f900f4c7a8d9afbfe73ebcbd315cb726 (diff) | |
download | libufdt-d457af95a96d7b7bd1361c984bf5d2f525cbd72a.tar.gz |
Fix memory and file leak am: 70107c8f5c am: 629a61c8df
am: c5b29321f9
Change-Id: I460aa5503eda111fdbdde35a1ab7b189badfa2b0
Diffstat (limited to 'tests')
-rw-r--r-- | tests/src/util.c | 34 | ||||
-rw-r--r-- | tests/src/util.h | 3 |
2 files changed, 24 insertions, 13 deletions
diff --git a/tests/src/util.c b/tests/src/util.c index 0d8040d..1c76773 100644 --- a/tests/src/util.c +++ b/tests/src/util.c @@ -6,27 +6,39 @@ #include "libfdt.h" #include "libufdt_sysdeps.h" - -char *load_file(const char *filename, size_t *pLen) { - FILE *fp = fopen(filename, "r"); - if (!fp) { - return NULL; - } - +static char *load_file_contents(FILE *fp, size_t *len_ptr) { // Gets the file size. fseek(fp, 0, SEEK_END); size_t len = ftell(fp); fseek(fp, 0, SEEK_SET); - char *buf = dto_malloc(len); + char *buf = malloc(len); + if (buf == NULL) { + return NULL; + } + if (fread(buf, len, 1, fp) != 1) { - dto_free(buf); + free(buf); return NULL; } - if (pLen) { - *pLen = len; + if (len_ptr) { + *len_ptr = len; + } + + return buf; +} + +char *load_file(const char *filename, size_t *len_ptr) { + FILE *fp = fopen(filename, "r"); + if (!fp) { + return NULL; } + + char *buf = load_file_contents(fp, len_ptr); + + fclose(fp); + return buf; } diff --git a/tests/src/util.h b/tests/src/util.h index 9df0ae7..3b68e14 100644 --- a/tests/src/util.h +++ b/tests/src/util.h @@ -3,8 +3,7 @@ #include <stdio.h> - -char *load_file(const char *filename, size_t *pLen); +char *load_file(const char *filename, size_t *len_ptr); int write_buf_to_file(const char *filename, const void *buf, size_t buf_size); int write_fdt_to_file(const char *filename, const void *fdt); |