tests: fix leaks spotted by ASAN

Always allocate from open_blob_rw(), to simplify memory management.

The fixes are not exhaustive.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
main
Marc-André Lureau 2023-02-28 11:31:00 +04:00 committed by David Gibson
parent 6f8b28f496
commit 083ab26da8
10 changed files with 25 additions and 21 deletions

View File

@ -36,6 +36,7 @@ int main(int argc, char *argv[])

buf = xmalloc(SPACE);
CHECK(fdt_open_into(fdt, buf, SPACE));
free(fdt);
fdt = buf;

CHECK(fdt_appendprop(fdt, 0, "prop-bytes", bytes, sizeof(bytes)));

View File

@ -36,7 +36,7 @@ int main(int argc, char *argv[])
err = fdt_open_into(fdt, buf, 0x1000);
if (err)
FAIL("fdt_open_into(): %s", fdt_strerror(err));

free(fdt);
fdt = buf;

/* Set up */

View File

@ -32,6 +32,7 @@ static void *open_dt(char *path)
* Resize our DTs to 4k so that we have room to operate on
*/
CHECK(fdt_open_into(dt, copy, FDT_COPY_SIZE));
free(dt);

return copy;
}

View File

@ -18,15 +18,16 @@

int main(int argc, char *argv[])
{
void *fdt;
void *fdt, *blob;
int subnode1_offset, subnode2_offset, subsubnode2_offset;
int err;
int oldsize, delsize, newsize;

test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
blob = load_blob_arg(argc, argv);

fdt = open_blob_rw(fdt);
fdt = open_blob_rw(blob);
free(blob);

oldsize = fdt_totalsize(fdt);


View File

@ -18,16 +18,17 @@

int main(int argc, char *argv[])
{
void *fdt;
void *fdt, *blob;
const uint32_t *intp;
const char *strp;
int err, lenerr;
int oldsize, delsize, newsize;

test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
blob = load_blob_arg(argc, argv);

fdt = open_blob_rw(fdt);
fdt = open_blob_rw(blob);
free(blob);

oldsize = fdt_totalsize(fdt);

@ -73,5 +74,6 @@ int main(int argc, char *argv[])
if (newsize >= oldsize)
FAIL("Tree failed to shrink after deletions");

free(fdt);
PASS();
}

View File

@ -186,6 +186,7 @@ static void *open_dt(char *path)
* Resize our DTs to 4k so that we have room to operate on
*/
CHECK(fdt_open_into(dt, copy, FDT_COPY_SIZE));
free(dt);

return copy;
}

View File

@ -69,15 +69,17 @@ static void check_set_name(void *fdt, const char *path, const char *newname)

int main(int argc, char *argv[])
{
void *fdt;
void *fdt, *blob;

test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
fdt = open_blob_rw(fdt);
blob = load_blob_arg(argc, argv);
fdt = open_blob_rw(blob);
free(blob);

check_set_name(fdt, "/subnode@1", "subnode@17");
check_set_name(fdt, "/subnode@2/subsubnode@0", "fred@0");
check_set_name(fdt, "/subnode@17/subsubnode", "something@0");

free(fdt);
PASS();
}

View File

@ -36,6 +36,7 @@ int main(int argc, char *argv[])
if (err)
FAIL("fdt_open_into(): %s", fdt_strerror(err));

free(fdt);
fdt = buf;

intp = check_getprop_cell(fdt, 0, "prop-int", TEST_VALUE_1);

View File

@ -123,7 +123,7 @@ void vg_prepare_blob(void *fdt, size_t bufsize);
void *load_blob(const char *filename);
void *load_blob_arg(int argc, char *argv[]);
void save_blob(const char *filename, void *blob);
void *open_blob_rw(void *blob);
void *open_blob_rw(const void *blob);

#include "util.h"


View File

@ -340,19 +340,14 @@ void save_blob(const char *filename, void *fdt)
free(tmp);
}

void *open_blob_rw(void *blob)
void *open_blob_rw(const void *blob)
{
int err;
void *buf = blob;
void *buf;
int newsize = fdt_totalsize(blob) + 8;

err = fdt_open_into(blob, buf, fdt_totalsize(blob));
if (err == -FDT_ERR_NOSPACE) {
/* Ran out of space converting to v17 */
int newsize = fdt_totalsize(blob) + 8;

buf = xmalloc(newsize);
err = fdt_open_into(blob, buf, newsize);
}
buf = xmalloc(newsize);
err = fdt_open_into(blob, buf, newsize);
if (err)
FAIL("fdt_open_into(): %s", fdt_strerror(err));
return buf;