[U-Boot] [PATCH 8/9] env_nand.c: print error message and fail gracefully

Scott Wood scottwood at freescale.com
Fri Jan 15 23:28:48 CET 2010


On Tue, Jan 12, 2010 at 09:44:00PM -0700, John Rigby wrote:
> From: Wolfgang Denk <wd at denx.de>
> 
> env_nand.c would crash silently if a malloc() for the environment
> buffers failed; make it print an error message and fail gracefully,
> i. e. use the default environment then.
> 
> Signed-off-by: Wolfgang Denk <wd at denx.de>
> ---
>  common/env_nand.c |    9 +++++++++
>  1 files changed, 9 insertions(+), 0 deletions(-)
> 
> diff --git a/common/env_nand.c b/common/env_nand.c
> index ca631af..a5166cb 100644
> --- a/common/env_nand.c
> +++ b/common/env_nand.c
> @@ -298,6 +298,15 @@ void env_relocate_spec (void)
>  	tmp_env1 = (env_t *) malloc(CONFIG_ENV_SIZE);
>  	tmp_env2 = (env_t *) malloc(CONFIG_ENV_SIZE);
>  
> +	if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) {
> +		puts("Can't allocate buffers for environment\n");
> +		if (tmp_env1)
> +			free (tmp_env1);
> +		if (tmp_env2)
> +			free (tmp_env2);

No need to check for NULL; free(NULL) is a no-op.

I'm also a bit concerned about bloat if we put a unique string at every
potential allocation failure.

-Scott


More information about the U-Boot mailing list