1
0
Fork 0
mirror of https://github.com/tobast/libunwind-eh_elf.git synced 2024-05-18 19:25:16 +02:00

Fix memory leaks in unw_create_addr_space() wrong-endian error paths

Check the endianness earlier in unw_create_addr_space() on all
architectures to avoid leaking the dynamically allocated address space
struct.

This was already fixed for ARM in commit cf6a998796 ("Fix memory leak
in ARM unw_create_addr_space()"). Move the endianness check also on ARM
to avoid malloc() & free() in the error case.
This commit is contained in:
Tommi Rantala 2012-08-05 18:22:21 +03:00
parent ff0c6ccf6b
commit 5fef17c05d
6 changed files with 51 additions and 43 deletions

View file

@ -32,8 +32,16 @@ unw_create_addr_space (unw_accessors_t *a, int byte_order)
#ifdef UNW_LOCAL_ONLY
return NULL;
#else
unw_addr_space_t as = malloc (sizeof (*as));
unw_addr_space_t as;
/*
* ARM supports little-endian and big-endian.
*/
if (byte_order != 0 && byte_order != __LITTLE_ENDIAN
&& byte_order != __BIG_ENDIAN)
return NULL;
as = malloc (sizeof (*as));
if (!as)
return NULL;
@ -41,16 +49,6 @@ unw_create_addr_space (unw_accessors_t *a, int byte_order)
as->acc = *a;
/*
* ARM supports little-endian and big-endian.
*/
if (byte_order != 0 && byte_order != __LITTLE_ENDIAN
&& byte_order != __BIG_ENDIAN)
{
free(as);
return NULL;
}
/* Default to little-endian for ARM. */
if (byte_order == 0 || byte_order == __LITTLE_ENDIAN)
as->big_endian = 0;

View file

@ -33,8 +33,15 @@ unw_create_addr_space (unw_accessors_t *a, int byte_order)
#ifdef UNW_LOCAL_ONLY
return NULL;
#else
unw_addr_space_t as = malloc (sizeof (*as));
unw_addr_space_t as;
/*
* hppa supports only big-endian.
*/
if (byte_order != 0 && byte_order != __BIG_ENDIAN)
return NULL;
as = malloc (sizeof (*as));
if (!as)
return NULL;
@ -42,11 +49,6 @@ unw_create_addr_space (unw_accessors_t *a, int byte_order)
as->acc = *a;
/*
* hppa supports only big-endian.
*/
if (byte_order != 0 && byte_order != __BIG_ENDIAN)
return NULL;
return as;
#endif
}

View file

@ -32,14 +32,7 @@ unw_create_addr_space (unw_accessors_t *a, int byte_order)
#ifdef UNW_LOCAL_ONLY
return NULL;
#else
unw_addr_space_t as = malloc (sizeof (*as));
if (!as)
return NULL;
memset (as, 0, sizeof (*as));
as->acc = *a;
unw_addr_space_t as;
/*
* MIPS supports only big or little-endian, not weird stuff like
@ -50,6 +43,14 @@ unw_create_addr_space (unw_accessors_t *a, int byte_order)
&& byte_order != __BIG_ENDIAN)
return NULL;
as = malloc (sizeof (*as));
if (!as)
return NULL;
memset (as, 0, sizeof (*as));
as->acc = *a;
if (byte_order == 0)
/* use host default: */
as->big_endian = (__BYTE_ORDER == __BIG_ENDIAN);

View file

@ -35,8 +35,15 @@ unw_create_addr_space (unw_accessors_t *a, int byte_order)
#ifdef UNW_LOCAL_ONLY
return NULL;
#else
unw_addr_space_t as = malloc (sizeof (*as));
unw_addr_space_t as;
/*
* Linux ppc64 supports only big-endian.
*/
if (byte_order != 0 && byte_order != __BIG_ENDIAN)
return NULL;
as = malloc (sizeof (*as));
if (!as)
return NULL;
@ -44,11 +51,6 @@ unw_create_addr_space (unw_accessors_t *a, int byte_order)
as->acc = *a;
/*
* Linux ppc64 supports only big-endian.
*/
if (byte_order != 0 && byte_order != __BIG_ENDIAN)
return NULL;
return as;
#endif
}

View file

@ -37,8 +37,15 @@ unw_create_addr_space (unw_accessors_t *a, int byte_order)
#ifdef UNW_LOCAL_ONLY
return NULL;
#else
unw_addr_space_t as = malloc (sizeof (*as));
unw_addr_space_t as;
/*
* x86 supports only little-endian.
*/
if (byte_order != 0 && byte_order != __LITTLE_ENDIAN)
return NULL;
as = malloc (sizeof (*as));
if (!as)
return NULL;
@ -46,11 +53,6 @@ unw_create_addr_space (unw_accessors_t *a, int byte_order)
as->acc = *a;
/*
* x86 supports only little-endian.
*/
if (byte_order != 0 && byte_order != __LITTLE_ENDIAN)
return NULL;
return as;
#endif
}

View file

@ -3,6 +3,7 @@
Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.com>
This file is part of libunwind.
@ -39,8 +40,15 @@ unw_create_addr_space (unw_accessors_t *a, int byte_order)
#ifdef UNW_LOCAL_ONLY
return NULL;
#else
unw_addr_space_t as = malloc (sizeof (*as));
unw_addr_space_t as;
/*
* x86_64 supports only little-endian.
*/
if (byte_order != 0 && byte_order != __LITTLE_ENDIAN)
return NULL;
as = malloc (sizeof (*as));
if (!as)
return NULL;
@ -48,11 +56,6 @@ unw_create_addr_space (unw_accessors_t *a, int byte_order)
as->acc = *a;
/*
* x86_64 supports only little-endian.
*/
if (byte_order != 0 && byte_order != __LITTLE_ENDIAN)
return NULL;
return as;
#endif
}