From 03e1bfd9a375d52236323978e151ed6fdc68a59a Mon Sep 17 00:00:00 2001 From: Furquan Shaikh Date: Wed, 6 Mar 2019 14:30:04 -0800 Subject: cbi: Do not add tags if data size is zero This change skips adding a tag to CBI if the size of its data is zero. Thus, callers of optional tags do not need to worry about checking if a tag should be added. It is taken care of within cbi_set_data. BUG=b:124282048 BRANCH=None TEST=Verified that MODEL_ID no longer shows up in cbi data. Change-Id: Id0c3da87a1479f5c52bd7b4f06840e5b4476b3d6 Signed-off-by: Furquan Shaikh Reviewed-on: https://chromium-review.googlesource.com/1506439 Commit-Ready: Furquan Shaikh Tested-by: Furquan Shaikh Reviewed-by: Daisuke Nojiri Reviewed-by: Jett Rink --- common/cbi.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/common/cbi.c b/common/cbi.c index e4dc001184..8517506eec 100644 --- a/common/cbi.c +++ b/common/cbi.c @@ -34,6 +34,14 @@ uint8_t *cbi_set_data(uint8_t *p, enum cbi_data_tag tag, const void *buf, int size) { struct cbi_data *d = (struct cbi_data *)p; + + /* + * If size of the data to be added is zero, then no need to add the tag + * as well. + */ + if (size == 0) + return p; + d->tag = tag; d->size = size; memcpy(d->value, buf, size); @@ -185,12 +193,13 @@ int cbi_set_board_info(enum cbi_data_tag tag, const uint8_t *buf, uint8_t size) } if (!d) { + uint8_t *p; /* Not found. Check if new item would fit */ if (sizeof(cbi) < head->total_size + sizeof(*d) + size) return EC_ERROR_OVERFLOW; /* Append new item */ - cbi_set_data(&cbi[head->total_size], tag, buf, size); - head->total_size += (sizeof(*d) + size); + p = cbi_set_data(&cbi[head->total_size], tag, buf, size); + head->total_size = p - cbi; } else { /* Overwrite existing item */ memcpy(d->value, buf, d->size); -- cgit v1.2.1