diff options
author | SzuWei Lin <szuweilin@google.com> | 2017-04-10 07:38:18 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-04-10 07:38:18 +0000 |
commit | aeaf88f5d00a63e616eb4584072bd1e2eff9df75 (patch) | |
tree | f41478bf6ee9d7d38527220b8ad5fbc4cde8a07a /tests | |
parent | 9dbd31040d911090979907d6ed44d8c94088773e (diff) | |
parent | d457af95a96d7b7bd1361c984bf5d2f525cbd72a (diff) | |
download | libufdt-aeaf88f5d00a63e616eb4584072bd1e2eff9df75.tar.gz |
Fix memory and file leak am: 70107c8f5c am: 629a61c8df am: c5b29321f9
am: d457af95a9
Change-Id: I5dcc01c9855b5345b78b76120d3651587324ecb3
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); |