Skip to content

Commit

Permalink
lib: sbi: Refine the way to construct platform features
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Ddnirvana authored and avpatel committed Sep 3, 2021
1 parent 1718b16 commit bd35521
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/sbi/sbi_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit bd35521

Please sign in to comment.