summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDong Du <Dd_nirvana@sjtu.edu.cn>2021-09-01 06:06:24 +0300
committerAnup Patel <anup@brainfault.org>2021-09-03 09:46:01 +0300
commitbd355213bfbb209c047e8cc0df56936f6705477f (patch)
tree464818a5df66b9652c1a1e730e2f9ee8b37f4859 /lib
parent1718b1642ee8c31eb2c49d78b5834e06f3324394 (diff)
downloadopensbi-bd355213bfbb209c047e8cc0df56936f6705477f.tar.xz
lib: sbi: Refine the way to construct platform features
sbi_platform_get_features_str() uses sbi_snprintf() to construct the features_str. However, it passes the wrong length value (i.e., the nfstr), which should be (nfstr-offset) as the starting point of str (i.e., features_str + offset) changes. This commit also checks the return value of snprintf, and handles the corner case that the string buffer is full. Signed-off-by: Dong Du <Dd_nirvana@sjtu.edu.cn> Reviewed-by: Atish Patra <atish.patra@wdc.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/sbi/sbi_platform.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/lib/sbi/sbi_platform.c b/lib/sbi/sbi_platform.c
index 99bd8f5..445a8c1 100644
--- a/lib/sbi/sbi_platform.c
+++ b/lib/sbi/sbi_platform.c
@@ -48,9 +48,18 @@ void sbi_platform_get_features_str(const struct sbi_platform *plat,
if (features & feat) {
temp = sbi_platform_feature_id2string(feat);
if (temp) {
- sbi_snprintf(features_str + offset, nfstr,
- "%s,", temp);
- offset = offset + sbi_strlen(temp) + 1;
+ int len = sbi_snprintf(features_str + offset,
+ nfstr - offset,
+ "%s,", temp);
+ if (len < 0)
+ break;
+
+ if (offset + len >= nfstr) {
+ /* No more space for features */
+ offset = nfstr;
+ break;
+ } else
+ offset = offset + len;
}
}
feat = feat << 1;