snprintf() returns the would-have-written length on truncation, not the
actual length written. Hence, check for truncation and return -ENOMEM.
Signed-off-by: Christian Storm <
christi...@siemens.com>
---
src/uboot_env.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/src/uboot_env.c b/src/uboot_env.c
index 30c9cbb..2a8b76e 100644
--- a/src/uboot_env.c
+++ b/src/uboot_env.c
@@ -571,17 +571,28 @@ int libuboot_env_store(struct uboot_ctx *ctx)
*/
if (saveflags) {
bool first = true;
+ int len;
size = (ctx->size - offsetdata) - (buf - data);
- buf += snprintf(buf, size, ".flags=");
+ len = snprintf(buf, size, ".flags=");
+ if (len >= size) {
+ free(image);
+ return -ENOMEM;
+ }
+ buf += len;
LIST_FOREACH(entry, &ctx->varlist, next) {
size = (ctx->size - offsetdata) - (buf - data);
if (entry->type || entry->access) {
- buf += snprintf(buf, size, "%s%s:%c%c",
+ len = snprintf(buf, size, "%s%s:%c%c",
first ? "" : ",",
entry->name,
attr_tostring(entry->type),
access_tostring(entry->access));
+ if (len >= size) {
+ free(image);
+ return -ENOMEM;
+ }
+ buf += len;
first = false;
}
}
--
2.55.0