From 37f3758a250d4c590ffac671f100d9b5ec73b417 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Marek=20Beh=C3=BAn?= <marek.behun@nic.cz>
Date: Fri, 22 Oct 2021 15:47:25 +0200
Subject: [PATCH] env: Use static_assert() to check if default_environment is
 too large
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Check sizeof(default_environment) against ENV_SIZE in a static_assert()
instead of runtime.
Only check if !USE_HOSTCC (for in fw_env tool ENV_SIZE expands to a
variable, and cannot be checked statically) nad
!DEFAULT_ENV_INSTANCE_EMBEDDED, for in that case the default_environment
variable is not set.

Signed-off-by: Marek Behún <marek.behun@nic.cz>
Reviewed-by: Simon Glass <sjg@chromium.org>
---
 env/common.c          | 5 -----
 include/env_default.h | 6 ++++++
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/env/common.c b/env/common.c
index 664d2e688e..99729ca002 100644
--- a/env/common.c
+++ b/env/common.c
@@ -247,11 +247,6 @@ char *env_get_default(const char *name)
 
 void env_set_default(const char *s, int flags)
 {
-	if (sizeof(default_environment) > ENV_SIZE) {
-		puts("*** Error - default environment is too large\n\n");
-		return;
-	}
-
 	if (s) {
 		if ((flags & H_INTERACTIVE) == 0) {
 			printf("*** Warning - %s, "
diff --git a/include/env_default.h b/include/env_default.h
index a6724719ec..23430dc70d 100644
--- a/include/env_default.h
+++ b/include/env_default.h
@@ -121,3 +121,9 @@ const char default_environment[] = {
 	}
 #endif
 };
+
+#if !defined(USE_HOSTCC) && !defined(DEFAULT_ENV_INSTANCE_EMBEDDED)
+#include <env_internal.h>
+static_assert(sizeof(default_environment) <= ENV_SIZE,
+	      "Default environment is too large");
+#endif
-- 
2.39.5